'Unexpected errors happen on working script

I'm relatively new to python and only recently tried some small scripts with it that can be used for my needs (just simply interact and automate with some parts of the game I'm playing). Some of the import libraries I have tried is pyautogui and keyboard..etc and for the first two particular libraries, I have noticed that sometime it generated unexpected behavior on my end despite been proven that it worked fine many times before and need to restart the whole computer or need to wait at least a few days to have the script run back properly on it own. A few example that I have is:

  1. while True: if keyboard.is_pressed('`') or keyboard.is_pressed('-'):
  • which would generally start my code right after pressing the required button but sometime it would not work and refused to elaborate if there's an app open in full screen ( which I found weird because, just like any other scripts, it worked like a bazillion times before and only refused to work randomly)

    1.        if (pyautogui.locateOnScreen('GasWealthy.png', confidence =0.9,grayscale = True,region=( 16 , 14 , 189 , 159 )) != None or pyautogui.locateOnScreen('GasJackpot.png', confidence =0.9,grayscale = True,region=( 16 , 14 , 189 , 159 )) )and val == 2:
                 print('Found The RICH DUDE √√√  ***Press "`" next time if you want to continue finding')
                 playsound.playsound('C:/Users/duong/OneDrive/Desktop/Color recognize/CheckMark.mp3')
                 break
             if (pyautogui.locateOnScreen('MinWealthy.png', confidence =0.9,grayscale = True,region=( 16 , 14 , 189 , 159 )) != None or pyautogui.locateOnScreen('MinJackpot.png', confidence =0.9,grayscale = True,region=( 16 , 14 , 189 , 159 )) )and val == 1:
                 print('Found The RICH DUDE √√√  ***Press "`" next time if you want to continue finding')
                 playsound.playsound('C:/Users/duong/OneDrive/Desktop/Color recognize/CheckMark.mp3')
                 break
      
  • I put this right behind the keyboard.is_pressed condition above, (the game I'm playing is like clash of clan, find a wealthy target, beat them, and get the resources so the program would simply ping me once it found one). Problem with this is, just about 30 minutes ago , I encountered a problem where it would work find for the first if when I state val ==1 but refused to recognized anything if val ==2 despite 2 images been carefully considered and like example one, been proven a lot of time before and I'm sure if I reset the computer, it should work fine once again.

-My question is, why does this happen and can it be corrected? Cause it's quite inconvenient to keep reset my laptop whenever it happens since I normally put it in sleep mode and would only manually reset it once it a while since I have a hefty tabs of chrome open in background and do it over and over again is just not ideal!

Edit: the same problem happened this time, along with (0, 'SetCursorPos', 'No error message is available') restart the computer simply fix everything but I still can't understand why it happened, here's the block of code, debug show problem occurred at first line , win32api.SetCursorPos. Thank you!

def click(x,y):
win32api.SetCursorPos ((x,y)) #cursor location to click
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0,0)
time.sleep(0.01)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source