0

I've found this code at stackoverflow

color = (0, 137, 241)
s = pyautogui.screenshot()
for x in range(s.width):
    for y in range(s.height):
        if s.getpixel((x, y)) == color:
            pyautogui.click(x, y)  # do something here
            break

I'm creating some bot for a game that waits for its turn, picks up a spell and then clicks on monsters tile. The problem is that I want to click only once on the tile of the color = (0, 137, 241). Right now it searches for all of them in for loop. How to specify x and y (width and height) to make it pyautogui.click(x, y) only once at described colors RGB?

Thanks a lot for help!

  • If I understand correctly, you want to break out of both the inner and the outer loop. Please see [How to break out of multiple loops?](https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops). – mportes Jun 11 '20 at 10:17
  • Kind thats whatsup, but the thing is its always searching for multiple colors, how to make it search only one then make some actions on it? Its searching for all color = (0, 137, 241) on the screen, I would like to deal with the first it finds – ASAP NAWS Jun 11 '20 at 10:26

1 Answers1

0

The key here is to break both for loops once it met your condition. The problem with your code is it only stops on the current column of pixels that met your condition then continue to search on other columns of pixels because that initial loop hasn't been broken.

color = (0, 137, 241)
s = pyautogui.screenshot()
for x in range(s.width):
    for y in range(s.height):
        if s.getpixel((x, y)) == color:
            pyautogui.click(x, y)  # do something here
            break
    else # <---this is the key. 
        continue
    break