Jump to content
Sign in to follow this  
exolon

Searching for a series of pixels

Recommended Posts

exolon

Hi,

I've been trying to make a simple bot to play the simple dodge-em-up Japanese game "TKKN". The game simply involves using the arrow keys to dodge a continuous barrage of bullets in space.

The problem is that the ship sprite doesn't seem to have any unique colours (due to lots of random background star colours), so a simple pixelsearch will turn up false matches.

To get around this, I've tried a horrible hack by first pixelsearching for a known colour in the sprite, then using PixelGetColor to check the next two pixels (i.e. I know that the sprite should have a purple pixel at position (x, y), and a red pixel at (x+1, y), and a white pixel at (x+2, y).

If not, then I recursively check the rest of the current line, then loop and search from the next line.

It probably works, but it's far too slow on my machine to ever be useful - it takes over a second and often over three seconds to find the sprite, by which time the game is over.

So I guess what's needed is a proper, fast function to search for an array of horizontal or vertical pixels rather than a single one. Has anyone had more success at this kind of thing?

Code in question:

Func FindShip($searchX, $searchY, $searchEndX, $searchEndY)
    ; x = l
    ; y = t
    ; endx = r
    ; endy = b
    ;ConsoleWrite("searching ..." & $searchX & ", " & $searchY)
    While $searchY <= $searchEndY
        ; find first pixel
        $foundPos = PixelSearch($searchX, $searchY, $searchEndX, $searchEndY, 0xFF00FF)
        If @error = 1 Then
            ;ConsoleWrite("not found anywhere in bounds " & $searchX & "," & $searchY & "," & $searchEndX & "," & $searchEndY & "!" & @CRLF)
            Return False
        EndIf
        ;ConsoleWrite("possible: " & $foundPos[0] & "," & $foundPos[1] & @CRLF)
        ; check second and third pixels for match
        If (PixelGetColor($foundPos[0]+1, $foundPos[1]) = 0xFF0000) And (PixelGetColor($foundPos[0]+2, $foundPos[1]) = 0xFFFFFF) Then
            ConsoleWrite("found at: " & $foundPos[0] & "," & $foundPos[1] & @CRLF)
            Return $foundPos
        Else
            ; check for more matches on this line...
            $moreMatches = FindShip($foundPos[0]+6, $foundPos[1], $searchEndX, $foundPos[1])
            If $moreMatches Then
                ConsoleWrite("recursively found at " & $moreMatches & @CRLF)
                Return $moreMatches
            EndIf
            ;ConsoleWrite("nope: next pixel colour = " & PixelGetColor($foundPos[0]+1, $foundPos[1], $hwnd) & @CRLF)
            ; else search next line...
            $searchY = $foundPos[1]+1
        EndIf
    WEnd
EndFunc

Share this post


Link to post
Share on other sites
bo8ster

have you searched the forum? Many have posted pixel issues.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
exolon

have you searched the forum? Many have posted pixel issues.

Hi Bo8ster,

Yes I searched and found many mentions of PixelSearch, but none talking about the need to search for more than a single pixel. If there's no library function that could do it, I think I'll have to write it in C and link it in somehow, if there's a FFI mechanism for AutoIt. Could be messy :)

Share this post


Link to post
Share on other sites
bo8ster

I don't know the answer to your questions but i'm sure it has come up before. There are a few PixelSearch experts here but most get what they need and then leave.

Hope you find what you need in their posts.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×