Pixel-based scan for a game AI taking a huge amount of time

124 views Asked by At

I'm trying to get all pixels in a 382x336 px area of my screen, detect certain colors and click the last detected one of them (for a game).

My problem is that my scan takes a huge amount of time to cover that area, making it impossible for my AI to keep up with the game.

Are there better methods to achieve my goal? Or is there a way I could optimize my code to run faster?

Main code:

Dim LFE, LNE As Point
            For YI As Integer = 264 To 600 Step 1
                P.Y = YI
                LFE = New Point(0, 0)
                LNE = New Point(0, 0)
                For XI As Integer = 493 To 875 Step 1
                    P.X = XI
                    If GetPoint(P) = "ffef3031" OrElse GetPoint(P) = "fff0d381" OrElse GetPoint(P) = "ff19a3ff" OrElse GetPoint(P) = "ff795f14" Then
                        LNE = P
                    ElseIf GetPoint(P) = "ffee1600" Then
                        LFE = P
                    End If
                Next
            Next
            If LFE = New Point(0, 0) Then
                If Not LNE = New Point(0, 0) Then
                    Cursor.Position = LNE
                End If
            Else
                Cursor.Position = LFE
End If

GetPoint function:

Function GetPoint(ByVal Pnt As Point) As String
        Dim a As New Drawing.Bitmap(1, 1)
        Dim b As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(a)
        b.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), a.Size)
        Dim c As Drawing.Color = a.GetPixel(0, 0)
        PictureBox1.BackColor = c
        Return PictureBox1.BackColor.Name
End Function
0

There are 0 answers