我试图在某个彩色像素出现在定义的屏幕截图中时单击它,然后重新截取屏幕截图并扫描更多特定彩色像素,并在找到一个像素后单击一个像素,依此类推。我认为我的代码正在截屏然后扫描颜色的像素,一旦找到颜色就点击它,但它不会重新截屏,而是继续扫描该屏幕截图以获得更多具有匹配颜色的像素并点击它们。一旦找到并单击像素,如何打破扫描像素的 for 循环,然后重新开始截取屏幕截图的过程?我的代码如下,谢谢!
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
繁星点点滴滴
相关分类