I am trying to click on a certain colored pixel when it appears in a defined screenshot and then retake the screenshot and scan for more certain colored pixels and click one once it finds one and so on. I think my code is taking the screen shot and then scanning the pixels for the color and once it finds the color it clicks it, but it doesn't retake a screenshot, instead, it continues to scan that screenshot for more pixels with matching color and clicking them. How do I break the for loop that scans the pixels once one is found and clicked and then start the process over at retaking the screenshot? My code is below, thank you!
from pyautogui import * import pyautogui import time import keyboard import random import win32api, win32con time.sleep(2) def click(x,y): win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) #Color of enemy: (254, 118, 136) while keyboard.is_pressed('q') == False: range1 = 250 range2 = 258 range3 = 114 range4 = 125 range5 = 122 range6 = 148 pic = pyautogui.screenshot() width, height = pic.size pic = pyautogui.screenshot(region=(((0.5*width)-100),(0.5*height-100),200,200)) width, height = pic.size for x in range(0,width,1): for y in range(0,height,1): r,g,b = pic.getpixel((x,y)) if (((r in range(range1,range2))and(g in range(range3,range4)))): #and(b in range(range5,range6)))): moveTo(x+width,y+height) click(x+width,y+height) time.sleep(0.1) break