Trying to hit random keys in order. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 02:28

Greetings again,

Some time ago I tried to make a macro for a game that consisted of hitting arrows that appeared on the screen randomly inside a box following the order, but I didn't succeed.

Any idea how to do it? For example the first time can be: up, up, down, right, left, down and the second time can be down, up, right, left, up, down

Is there any way to create a script that checks a certain section of the screen (for example, check an image) and press a key depending on the image?
Last edited by Anonplayer on 12 Apr 2021, 18:09, edited 1 time in total.
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 06:07

Yes, you can use ImageSearch to search for a match to an image file. Random can generate random numbers for you.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 14:45

The problem is that the order of the arrows changes continuously, I need the keys to be pressed in the order they appear (from left to right)

How could I do that?
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 15:23

You have four images, one for each direction. Search for each image. When you find a match, you press the corresponding key, and move to the next set of coordinates for the next search. To get the process working, start with a single arrow.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 16:09

So it would be something like this:

Code: Select all

ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\up.png
if (ErrorLevel = 0)
{
     Send, {up}
     }
if (ErrorLevel = 1)
{
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\down.png
   Send, {down}
   }
if (ErrorLevel = 2)
{
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\left.png
   Send, {left}
   }
if (ErrorLevel = 3)
{
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\right.png
   Send, {right}
}
And after that I should move to the next coordinate and repeat the same code right?

I'm sorry if i'm so noob in this.
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 16:35

That is the idea. Possibly like this:

Code: Select all

For index, direction in ["Up", "Down", "Left", "Right"] {
 ImageSearch,,, 0, 0, 1440, 900, C:\My Images\%direction%.png
 If !ErrorLevel {
  Send {%direction%}
  Break
 }
}
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 18:02

Thank you!

So I should write that code once for each arrow to press according to its order, right? In total it would be a sequence of 5
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 18:15

You could do that, or make a Loop, with adjustments of coordinates. As usual, there is more than one way.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 18:24

It sounds complicated about the loop, I have been testing and there is no way. Could you give me an example if it's not too much trouble? And again, thank you.

The idea is also that it repeats continuously until I press a certain key.
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 18:59

This is just a demo. Adjust as needed.

Code: Select all

width := 30, height := 40, x := 100, y1 := 100, y2 := y1 + height - 1
While !done {
 Sleep, 500
 x1 := x + (A_Index - 1) * width, x2 := x + A_Index * width - 1
 For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, x1, y1, x2, y2, C:\My Images\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Break
  }
 }
}
ExitApp
F3::done := True
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 19:22

I'm trying but it doesn't seem to work, I must be doing something wrong.

My screen has a resolution of 1440x900.

In X1 and Y1 I am putting the coordinates of the upper left corner of where the arrow should be and in X2 and Y2 I am putting the coordinates of the lower right corner of where the arrow should be.

I press ctrl h but for some reason it doesn't detect the image on the screen.

The code has been like this:

Code: Select all

^h::
width := 30, height := 40, x := 100, y1 := 100, y2 := y1 + 

height - 1
While !done {
 Sleep, 500
 x1 := x + (A_Index - 1) * width, x2 := x + A_Index * 

width - 1
 For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, 354, 354, 384, 375, C:\Users\Anon\Pictures\focus\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Break
  }
 }
}
In the game it looks like this.
image.png
image.png (12.82 KiB) Viewed 795 times
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 19:34

Don't break the lines.

Since your script does not work, break it down into smaller parts. Start with a single ImageSearch-- just one line. Get it working. You can then add the multi-image search. After that works, add the While loop.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 20:01

Okay I did it, seems it's working now with this code. Another problem is that the box where the arrows are located changes position after completing each sequence, así que no termina de funcionar bien.

Code: Select all

^h::
For index, direction in ["Up", "Down", "Left", "Right"] {
 ImageSearch,,, 337, 328, 377, 361, C:\Users\Anon\Downloads\Arrows\%direction%.png
 If !ErrorLevel {
  Send {%direction%}
Break
 }
}
For index, direction in ["Up", "Down", "Left", "Right"] {
 ImageSearch,,, 373, 325, 410, 367, C:\Users\Anon\Downloads\Arrows\%direction%.png
 If !ErrorLevel {
  Send {%direction%}
Break
 }
}
For index, direction in ["Up", "Down", "Left", "Right"] {
 ImageSearch,,, 405, 327, 441, 368, C:\Users\Anon\Downloads\Arrows\%direction%.png
 If !ErrorLevel {
  Send {%direction%}
Break
 }
}
For index, direction in ["Up", "Down", "Left", "Right"] {
 ImageSearch,,, 439, 328, 471, 368, C:\Users\Anon\Downloads\Arrows\%direction%.png
 If !ErrorLevel {
  Send {%direction%}
Break
 }
}
For index, direction in ["Up", "Down", "Left", "Right"] {
 ImageSearch,,, 370, 332, 507, 367, C:\Users\Anon\Downloads\Arrows\%direction%.png
 If !ErrorLevel {
  Send {%direction%}
Break
 }
}
Now I need the script to be able to press the keys in the order they appear on the screen. However, once a sequence of arrows is completed, a new one appears whose order is different from the previous one.
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 20:26

It looks like you have the basic routine working. You can now adjust according to wherever you need to look for the arrows. If you describe the complete process in words, translating it into AHK will be easier for you. If you cannot describe it, then you cannot translate it! Best of luck.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 20:41

Is there a way that the script can search the images from left to right? That would save me a lot of time and everything would be easier and I would not have to go selecting coordinate by coordinate.
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 21:39

You can search the entire window if you like. When a match is found, you can adjust the coordinates as needed.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 21:50

The search only give me the x1 and y1, how can I do to get the x2 and y2?

The code that i'm using is

Code: Select all

^h::
CoordMode Pixel  ; Interprets the coordinates below as relative to the screen rather than the active window.
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\Users\Anon\Downloads\Arrows\up.png
if (ErrorLevel = 2)
    MsgBox Could not conduct the search.
else if (ErrorLevel = 1)
    MsgBox  could not be found on the screen.
else
    MsgBox found at %FoundX%x%FoundY% %FoundX2%x%FoundY2%.
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 21:58

You are searching for an image that you already have, so your image has a known width and height. You can compute the other coordinates from it.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

Post by Anonplayer » 11 Apr 2021, 22:20

I really appreciate your help, but I think I'm going to continue with problems.

Suppose I manage to get the x1, y1, x2, y2 coordinates of all the arrows in one of the sequences. Well, I turn on the script and everything perfect, it manages to complete the sequence.

After that first sequence, the arrows change their position in the window, but suppose I also have their coordinates, as well as the other multiple random locations that it moves to after each sequence of arrows.

How do I make the script automatically search and detect those sequences in the window? I'm gonna show you an example:


First seq.
image.png
image.png (4.59 KiB) Viewed 715 times
Second seq.
image.png
image.png (4.88 KiB) Viewed 715 times
As you can see, in the second sequence the arrows no longer have the same order and the box that contains them has moved slightly in position. Basically I need an script that hit the arrows from left to right following the order and knowing that the coordinates vary after each sequence.
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

Post by mikeyww » 11 Apr 2021, 22:30

1. Search the entire window for the first image. The leftmost image will then be found.
2. At that point, you search to the right for each of the next four images-- each time, searching to the right of the previous image.
3. Go back to step #1 for each set.

With each search, you are searching for any of the four possible images.
Post Reply

Return to “Gaming Help (v1)”