Script clicker when an image is found

Ask gaming related questions (AHK v1.1 and older)
User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Script clicker when an image is found

Post by Tibro » 20 Oct 2022, 18:14

Good afternoon.
I want to write a script that will work in a mini-game like "Flappy Bird"
Let's say fish "X" swims and there is a hook "Y". The hook moves only when the LMB is held down and goes out to the standard position if the LMB is released.
Need a script.
So that when a picture "X" is found on the screen, the script clicks on the mouse (which constantly goes up) and holds the picture "X" at the position of the picture "Y"

But at the very beginning I faced a similar problem.
The script does not want to react to the PNG that I cut out.
Tried different sizes. It does not even react to PNG not in the game itself, but on the folder in which it is located. But it reacts to the screen made in the browser.

Code: Select all

F2::

 imgFound := false
 while(imgFound = false)
 {
   
  ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, test.png
  if (x тут скобки угловые "")

  {
   click, %x%, %y%
   Sleep 100
   click, %x%, %y%
   Sleep 100
   click, %x%, %y%
   MsgBox Изображение найдено в координатах x=%x%, y=%y%.
   imgFound := true
  }
 }
return
Why doesn't he react?

What am I doing wrong?
If you help me in writing or just to understand why it does not work, then I will be very grateful!
Attachments
13.png
13.png (43.63 KiB) Viewed 898 times
12.png
12.png (729.15 KiB) Viewed 898 times
11.png
11.png (709.96 KiB) Viewed 898 times

User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Script clicker when an image is found

Post by boiler » 21 Oct 2022, 06:06

I’m not sure why you have this line like this:

Code: Select all

  if (x тут скобки угловые "")

It apparently should be:

Code: Select all

  if (x != "")

That should work, but I would go with this instead:

Code: Select all

  if !ErrorLevel

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 21 Oct 2022, 08:36

boiler wrote:
21 Oct 2022, 06:06
I’m not sure why you have this line like this:

Code: Select all

  if (x тут скобки угловые "")

It apparently should be:

Code: Select all

  if (x != "")

That should work, but I would go with this instead:

Code: Select all

  if !ErrorLevel
It works, completely, but not on this img and not in the game. If a
Run the script in the browser, for example, with another png, then it works fully, but if I run it in the game with this png, then it does not work at all

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 21 Oct 2022, 10:07

boiler wrote:
21 Oct 2022, 06:06
I’m not sure why you have this line like this:

Code: Select all

  if (x тут скобки угловые "")

It apparently should be:

Code: Select all

  if (x != "")

That should work, but I would go with this instead:

Code: Select all

  if !ErrorLevel
You didn't help.
I wrote earlier that the script does not work only in the game.

It works in the browser, outside the browser, but not in the game.
And do not work with the PNG file that I cut from the game. It works with other PNG files from the browser, but not from the game.

User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Script clicker when an image is found

Post by boiler » 21 Oct 2022, 11:01

Tibro wrote: You didn't help.
I wrote earlier that the script does not work only in the game.
OK. You include a line like if (x тут скобки угловые "") and expect me not to point it out? If it works, it's despite that line, not because it does anything useful. Good luck.

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 21 Oct 2022, 13:01

boiler wrote:
21 Oct 2022, 11:01
Tibro wrote: You didn't help.
I wrote earlier that the script does not work only in the game.
OK. You include a line like if (x тут скобки угловые "") and expect me not to point it out? If it works, it's despite that line, not because it does anything useful. Good luck.
I took into account your remark and I am grateful for it. I fixed it and did as you said. I appreciate it and want to write correctly right away, so I am very grateful.
But we are talking about the problem of the performance of this script
I need to understand why it doesn't work in general

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 21 Oct 2022, 13:03

Code: Select all

F2::

 imgFound := false
 while(imgFound = false)
 {
   
  ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, test.png
    ;if (x != "")
     if !ErrorLevel
  {
   click, %x%, %y%
   Sleep 100
   click, %x%, %y%
   Sleep 100
   click, %x%, %y%
   MsgBox Screen Coords x=%x%, y=%y%.
   imgFound := true
  }
 }
return
 

User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Script clicker when an image is found

Post by boiler » 21 Oct 2022, 16:01

Can you post the test.png image?

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 22 Oct 2022, 05:10

boiler wrote:
21 Oct 2022, 16:01
Can you post the test.png image?
Attachments
test.png
test.png (43.63 KiB) Viewed 798 times

User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Script clicker when an image is found

Post by boiler » 22 Oct 2022, 07:06

That's the problem. There are a couple issues with your reference image. It's way too big. You need to crop it down to only the part that you're searching for. And you can't use a transparent background. If you want it to treat the parts around it as transparent (so it will find it no matter what background is around it), then you have to make that a certain color and use the *Trans option in ImageSearch.

Here is how you would crop it so that it doesn't include any background (don't include any edges that could have the background blended in):
test_cropped.png
test_cropped.png (1.03 KiB) Viewed 792 times

If you want to include pretty much the whole fish, then you need it to ignore the background, and you do that by using a trans color that you will tell ImageSearch to ignore. And you have to color over edges too since those would be partially shown with the background blended in and you want it to ignore those too. You want just the part that would be pure opaque colors that are the same every time, so you have to liberally color over all the edge pixels. And when you color it, you can't use a brush tool that has blended edges. It needs to be a pencil tool that paints only pixels of the pure color. And the color can't merely look like the color (like any red). It has to be the exact numerical value that you're going to put in your ImageSearch command (such FF0000 to match "Red" or the value itself) Like this:
test_red.png
test_red.png (2.44 KiB) Viewed 792 times

And your command would look like this:

Code: Select all

ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, *TransRed test_red.png ; or the equivalent *TransFF0000
And you might want to specify some allowable variation in the color values:

Code: Select all

ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, *10 *TransRed test_red.png

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 23 Oct 2022, 13:30

boiler wrote:
22 Oct 2022, 07:06
That's the problem. There are a couple issues with your reference image. It's way too big. You need to crop it down to only the part that you're searching for. And you can't use a transparent background. If you want it to treat the parts around it as transparent (so it will find it no matter what background is around it), then you have to make that a certain color and use the *Trans option in ImageSearch.

Here is how you would crop it so that it doesn't include any background (don't include any edges that could have the background blended in):
test_cropped.png


If you want to include pretty much the whole fish, then you need it to ignore the background, and you do that by using a trans color that you will tell ImageSearch to ignore. And you have to color over edges too since those would be partially shown with the background blended in and you want it to ignore those too. You want just the part that would be pure opaque colors that are the same every time, so you have to liberally color over all the edge pixels. And when you color it, you can't use a brush tool that has blended edges. It needs to be a pencil tool that paints only pixels of the pure color. And the color can't merely look like the color (like any red). It has to be the exact numerical value that you're going to put in your ImageSearch command (such FF0000 to match "Red" or the value itself) Like this:
test_red.png


And your command would look like this:

Code: Select all

ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, *TransRed test_red.png ; or the equivalent *TransFF0000
And you might want to specify some allowable variation in the color values:

Code: Select all

ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, *10 *TransRed test_red.png
Thank you very much! This will come in handy in the future, but not worked id game) shit(

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 23 Oct 2022, 13:31

boiler wrote:
22 Oct 2022, 07:06
Outside of the game it works great. I can make a video for you to better understand.
Last edited by gregster on 23 Oct 2022, 13:33, edited 1 time in total.
Reason: Removed repeated long quote.

User avatar
Tibro
Posts: 14
Joined: 20 Oct 2022, 17:38

Re: Script clicker when an image is found

Post by Tibro » 27 Oct 2022, 20:59

I decided to try using windowed mode - it did not help

Post Reply

Return to “Gaming Help (v1)”