首先,我对Python非常陌生,可能我的代码会伤害你的眼睛。对不起。 我的代码目的只是使用 pyautogui 制作一个过滤系统,我的第一个循环工作正常,但由于静态嵌套块太多,我需要创建第二个循环。我通过简单地将其更改为 False 来结束第一个循环,但是当我想再次开始时,它会失败并刚刚完成任务。由于代码非常混乱,我不知道如何修复,也不知道是否有人可以帮助我。
import pyautogui
import time
import keyboard
should_restart = True
time.sleep(1)
while should_restart:
try:
loop2 = False
yellow = pyautogui.locateCenterOnScreen('Yellow_FL.png', confidence=0.7)
pyautogui.moveTo(yellow)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found1")
continue
except:
try:
green = pyautogui.locateCenterOnScreen('Green_FL.png', confidence=0.7, grayscale=True)
pyautogui.moveTo(green)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found2")
continue
except:
try:
purple = pyautogui.locateCenterOnScreen('Purple_FL.png', confidence=0.7, grayscale=True)
pyautogui.moveTo(purple)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found3")
continue
except:
try:
b_battery = pyautogui.locateCenterOnScreen('Brown_Battery.png', confidence=0.7, grayscale=True)
pyautogui.moveTo(b_battery)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found4")
continue
except:
try:
g_battery = pyautogui.locateCenterOnScreen('green_battery.png', confidence=0.7)
pyautogui.moveTo(g_battery)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found5")
continue
except:
try:
P_Lens = pyautogui.locateCenterOnScreen('Purple_Lens.png', confidence=0.7, grayscale=True)
pyautogui.moveTo(P_Lens)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
time.sleep(0.5)
print("Found7")
continue
except:
try:
amp = pyautogui.locateCenterOnScreen('yellow_amp.png', confidence=0.7)
pyautogui.moveTo(amp)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
time.sleep(0.5)
print("Found8")
continue
except:
try:
styptic = pyautogui.locateCenterOnScreen('Styptic.png', confidence=0.7)
pyautogui.moveTo(styptic)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
time.sleep(0.5)
print("Found9")
continue
except:
should_restart = False
loop2 = True
while loop2:
try:
lens_y = pyautogui.locateCenterOnScreen('yellow_lens.png', confidence=0.7)
pyautogui.moveTo(lens_y)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found10")
continue
except:
try:
Syringe = pyautogui.locateCenterOnScreen('Syringe.png', confidence=0.7, grayscale=True)
pyautogui.moveTo(Syringe)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found11")
continue
except:
try:
Auto = pyautogui.locateCenterOnScreen('AutoBloodweb.png', confidence=0.7, grayscale=True)
pyautogui.moveTo(Auto)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
print("Found12")
time.sleep(4)
should_restart = True
time.sleep(1)
loop2 = False
except:
try:
Empty = pyautogui.locateCenterOnScreen('EmptyBloo2aaadweb.png', confidence=0.7)
break
except:
pass
我预计第一个循环将结束,然后第二个循环将开始,它确实做到了,然后第二个循环将停止,第一个循环将再次开始,直到检测到“EmptyBloodweb.png”。 相反,第一个循环正在停止,第二个循环正在启动,然后第二个循环正在停止,仅此而已。
很难说出你想要的实际逻辑是什么,但如果我的水晶球和有根据的猜测是正确的,你想尝试按顺序单击这些东西,如果单击失败,则从第一个重新开始(通过找不到东西的方法)?
如果是这样的话,我会将其重构为如下所示。
这个想法是
find_and_click
尝试进行查找并单击,如果找不到该东西则返回 False (如果实际的移动到/鼠标按下/鼠标向上顺序,它会很高兴地引发异常失败)。
然后,如果任何点击失败,
main()
中的两个循环将使用continue
重新启动(辅助函数返回False
)。
如果两个循环是相关的,您可以将它们提取到相同类型的两个函数中:如果任何步骤失败,则重试另一个循环,依此类推。
import time
import pyautogui
def find_and_click(png, grayscale=False):
print(f"Looking for {png}...")
try:
obj = pyautogui.locateCenterOnScreen(png, confidence=0.7, grayscale=grayscale)
except Exception as e:
print(f"Could not find {png}: {e}")
return False
pyautogui.moveTo(obj)
time.sleep(0.1)
pyautogui.mouseDown()
time.sleep(0.5)
pyautogui.mouseUp()
pyautogui.moveTo(1200, 600)
return True
def main():
while True:
print("Starting loop 1...")
if not find_and_click("Yellow_FL.png"):
continue
if not find_and_click("Green_FL.png", grayscale=True):
continue
if not find_and_click("Purple_FL.png", grayscale=True):
continue
if not find_and_click("Brown_Battery.png", grayscale=True):
continue
if not find_and_click("green_battery.png"):
continue
if not find_and_click("Purple_Lens.png", grayscale=True):
continue
if not find_and_click("yellow_amp.png"):
continue
if not find_and_click("Styptic.png"):
continue
while True:
print("Starting loop 2...")
if not find_and_click("yellow_lens.png"):
continue
if not find_and_click("Syringe.png", grayscale=True):
continue
if not find_and_click("AutoBloodweb.png", grayscale=True):
continue
if __name__ == "__main__":
main()