Jump to content

PixelSearch dilemma. please help


Recommended Posts

Hello everybody,

First and foremost, I'd like to introduce myself. My real name is Will and I'm glad to have finally signed up for these wonderful forums. (I don't care if you call me by my alias or my real name.) I'm quite pleased to finally mess around with AutoItv3, and I'm eager to use it as a stepping stone to further my unseasoned programming knowledge.

Now I have been messing around with AutoItv3 for quite a while now (and I must say it's quite incredible). The only real projects I've done are concerned with manipulation of flash-based games. So far, I have made a script to autonously play the "Defend Your Castle" game. (http://www.xgenstudios.com/castle/) I'm currently working on trying to autonomously play the "Smack the Rabbit!" flash game. You basically have a hammer in your hand and you try to hit rabbits running around.

In order for me to be able hit the rabbits, I have decided to use a PixelSearch to search an area of the flash game and look for a dark color on a rabbit; and finally hit it with a left mouse click.

Rabbit looks like this:

Posted Image

I'm looking for the best, most efficient way to locate the dark colored eyes and then click on it. Note: I've noticed that the very darkest part of the eye varies in color from rabbit to rabbit, but usually an F is every other letter in the hex code.

This is what my script first looked like and then I realized that it would be a pain in the butt to put 14 If statements in my loop.

;
; AutoIt Version: 3.0
; Language:       English
; Platform:       Win9x/NT
; Author:         Jonathan Bennett (jon@hiddensoft.com)
;
;
;
; Script Author:  Will G. (AKA m0rphling)
; Script Function:
;   Bunny squisher
;

;=====> The Terminator
HotKeySet("{ESC}", "Terminate")

Func Terminate()
    Exit 
EndFunc

;=====> Activate browser window and ask to proceed
WinActivate("**** ****** Forums - powered by vBulletin - Mozilla Firefox")

If WinActive("**** ****** Forums - powered by vBulletin - Mozilla Firefox") Then
    $answer = MsgBox(36, "Is it okay to start?", "Proceed?")
Else
    Exit
EndIf

If $answer = 7 Then
    MsgBox(0, "Cya sucker!", "OK.  Bye!")
    Exit
EndIf

;=====> The delays for mouse dragging and/or clicking
Opt("MouseClickDelay", 1)

;=====> Sanity check
$duh = 0

;=====> The magical bunny squisher machine
If $duh < 1 Then
    Do  
        $newcolor = PixelGetColor(894, 460)

        $bunnycoord1 = PixelSearch(285, 425, 825, 815, 0x0F0F0F, 0, 1)
        If Not @error Then
            MouseClick("left", $bunnycoord1[0], $bunnycoord1[1], 1, 0)
        EndIf
    
        $bunnycoord2 = PixelSearch(285, 425, 825, 815, 0x1F1F1F, 0, 1)
        If Not @error Then
            MouseClick("left", $bunnycoord2[0], $bunnycoord2[1], 1, 0)
        EndIf
     
        $bunnycoord3 = PixelSearch(285, 425, 825, 815, 0x2F2F2F, 0, 1)
        If Not @error Then
            MouseClick("left", $bunnycoord3[0], $bunnycoord3[1], 1, 0)
        EndIf
     
        $bunnycoord4 = PixelSearch(285, 425, 825, 815, 0x3F3F3F, 0, 1)
        If Not @error Then
            MouseClick("left", $bunnycoord4[0], $bunnycoord4[1], 1, 0)
        EndIf
     
        If $newcolor <> 10145434 Then
            Exit
        EndIf
      
    Until $duh > 1
EndIf

I would like to have something in my Do...Until loop like

$bunnycoord = PixelSearch(285, 425, 825, 815, $MULTIHEXVARIABLE, 0, 1)
        If Not @error Then
            MouseClick("left", $bunnycoord[0], $bunnycoord[1], 1, 0)
        EndIf

Where $MULTIHEXVARIABLE is a variable or something that will represent all 14 hex colors that I need. I'm not experienced with arrays, but I have a hunch that an array is what I might need.

So please, as you can tell, I'm trying to be informative as possible so that any of you knowledgable and benevolent people can help me. I am grateful for any help that is provided to me. Thank £xEæand happy scripting! :D

Edited by m0rphling
Link to comment
Share on other sites

Warning, I'm still learning as well so this is untested.

$HEXVARIABLE = "0x0F0F0F|0x1F1F1F|0x2F2F2F|0x3F3F3F"
$MULTIHEXVARIABLE = StringSplit($HEXVARIABLE,"|")

For $hexvar = 1 to Ubound($MULTIHEXVARIABLE)-1
$bunnycoord = PixelSearch(285, 425, 825, 815, $MULTIHEXVARIABLE[$hexvar], 0, 1)
       If Not @error Then
           MouseClick("left", $bunnycoord[0], $bunnycoord[1], 1, 0)
       EndIf
Next
Edited by CyberGlitch
Link to comment
Share on other sites

It still hasn't worked out for me. Well, it somewhat works; however, it only really seems to be clicking at where the mouse pointer is and then moving left two pixels and clicking again. This is how I have it set up:

;=====> Sanity check
$duh = 0

;=====> Color variables
$HEXVARIABLE = "0x0F0F0F|0x1F1F1F|0x2F2F2F|0x5F5F5F|0X2E2E2E"
$MULTIHEXVARIABLE = StringSplit($HEXVARIABLE,"|")

;=====> The magical bunny squisher machine
If $duh < 1 Then
    Do   
        For $hexvar = 1 To Ubound($MULTIHEXVARIABLE) -1
            $bunnycoord = PixelSearch(285, 425, 825, 815, $MULTIHEXVARIABLE[$hexvar], 0, 2)
            If Not @error Then
                MouseClick("left", $bunnycoord[0], $bunnycoord[1], 1, 0)
            EndIf
        Next    
    
        $newcolor = PixelGetColor(894, 460)
    
        If $newcolor <> 10145434 Then
            ExitLoop
            Exit
        EndIf      
    Until $duh > 1
EndIf

I think the problem might be in the "UBound($MULTIHEXVARIABLE) -1"

The syntax is:

For <variable> = <start> To <stop> [Step <stepval>]
    statements
    ...
Next

But <stop> is a numeric value that should count down by 1.

I have a hex value there, but it's treated more like a string when I want to put it into the PixelSearch as the coordinates.

Link to comment
Share on other sites

Ubound gives the upper bounder of the array. In your example the upper bounder of $MULTIHEXVARIABLE is 6, including 0, only 5 of those are actual data, 0 will tell you how many variables there are in the array. So we want it to only count the 5 hense the ubound -1. This tells your For loop to start at 1 and stop at 5, or how every many different colors you add to $MULTIHEXVARIABLE.

Something else to try is from the looks of it you want the script to continue once it clicks the cursor correct? To make this happen you need to add an exit loop command after the click. Also might want to add a small delay so the loops don't hong the CPU.

A tip that I've learn is to add MsgBox(0,"","Area 1") in different parts of the script to debug what's happening and when it starts to break down. Just change what the msg says so you know where the script breaks.

Don't have the site your trying this for so can't test it obviously.

;=====> Sanity check
$duh = 0

;=====> Color variables
$HEXVARIABLE = "0x0F0F0F|0x1F1F1F|0x2F2F2F|0x5F5F5F|0X2E2E2E"
$MULTIHEXVARIABLE = StringSplit($HEXVARIABLE,"|")

;=====> The magical bunny squisher machine
If $duh < 1 Then
   Do   
       For $hexvar = 1 To Ubound($MULTIHEXVARIABLE) -1
           $bunnycoord = PixelSearch(285, 425, 825, 815, $MULTIHEXVARIABLE[$hexvar], 0, 2)
           If Not @error Then
               MouseClick("left", $bunnycoord[0], $bunnycoord[1], 1, 0)
               Sleep(300)
               ExitLoop
           EndIf
       Next    
   
       $newcolor = PixelGetColor(894, 460)
   
       If $newcolor <> 10145434 Then
           ExitLoop
           Exit
       EndIf      
   Until $duh > 1
EndIf
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...