First of all, I am insanely new to the python and probably my code will hurt your eyes. I am sorry. My purpose on the code is simply making a filtering system with pyautogui and my first loop works fine but I needed to create a second loop due to too many statically nested blocks. I am ending the first loop with simply changing it to False but then when I wanna start again it fails and just finishes the task. I dont know how to fix and if anyone even can help me due to how chaotic the code is.
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
I expected first loop would end and then 2nd loop would start, which it did, and then 2nd loop would stop and the first one would start again until the 'EmptyBloodweb.png' is detected. Instead first loop is stopping 2nd loop is starting and then 2nd loop is stopping and thats all.
It's hard to tell what the actual logic you're going for is, but if my crystal ball and educated guess is right, you want to try and click those things in sequence, starting again from the first one if a click fails (by way of not finding the thing)?
If that's the case, I'd refactor this to something like the below.
The idea is that find_and_click
tries to do the finding-and-clicking, returning False if it couldn't find the thing (and it will happily raise exceptions if the actual move-to/mouse-down/mouse-up sequence fails).
Then, the two loops in main()
are restarted using continue
if any click fails (the helper function returns False
).
If the two loops are dependent, you could extract them into two functions of the same sort: if any step fails, retry the other loop, and so on.
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
break
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
break
if __name__ == "__main__":
main()
If the idea is to just click these things in order, waiting forever until they appear, then this simplifies to
import time
from itertools import count
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 wait_for_find_and_click(png, grayscale=False):
for attempt in count(1):
print(f"Attempt {attempt} to find {png}...")
if find_and_click(png, grayscale):
break
time.sleep(1)
def main():
while True:
wait_for_find_and_click("Yellow_FL.png")
wait_for_find_and_click("Green_FL.png", grayscale=True)
wait_for_find_and_click("Purple_FL.png", grayscale=True)
wait_for_find_and_click("Brown_Battery.png", grayscale=True)
wait_for_find_and_click("green_battery.png")
wait_for_find_and_click("Purple_Lens.png", grayscale=True)
wait_for_find_and_click("yellow_amp.png")
wait_for_find_and_click("Styptic.png")
wait_for_find_and_click("yellow_lens.png")
wait_for_find_and_click("Syringe.png", grayscale=True)
wait_for_find_and_click("AutoBloodweb.png", grayscale=True)
if __name__ == "__main__":
main()