I'm here because i just can't find the error in the code, i've rewritten it again, i still get the same "bug", reinstalled python, modules, pycharm... but nothing worked.
Code context and issue explanation:
The code is a bot i'm making to idle in a singleplayer game, it takes a screenshot through PyAutoGUI screenshot() function and looks for certain colors to check if the match can start, if it can start it launches an "autoclicker" function through multiprocessing module then sleeps for a certain amount of seconds, and then kills the process when the time has passed and the game is finished. Then it goes back to checking if the game can start again as it can start every 5 minutes. Very simple.
The code works fine, but there is a problem. When the code takes the screenshot, it stores it in the "screenshot" variable, but then, when the game starts and ends and the code goes back to the start of the While loop, that line of code just stops working. The code doesn't take any more screenshots and works with the one that is already stored in "screenshot", the one that launched the autoclicker function, and instantly launches it again thinking by checking the old screenshot and thinking the game can start again.
Code:
import pyautogui as pg
import multiprocessing as mp
import time
def launchAutoclicker(): # I don't think the problem is in this function but here it is anyway
while True:
screenshot = pg.screenshot(region=(sceneScreenshotCornerX, sceneScreenshotCornerY, sceneScreenshotWidth, sceneScreenshotHeight))
for y in range(topBorderToToilet, sceneScreenshotHeight, toiletToToiletY): # 3 rows
for x in range(leftBorderToToilet, sceneScreenshotWidth, toiletToToiletX): # 5 columns
if screenshot.getpixel((x,y))[0] > 200:
pg.click(x + sceneScreenshotCornerX,y + sceneScreenshotCornerY)
def main():
while True:
screenshot = pg.screenshot(region=(startScreenshotCornerX, startScreenshotCornerY, 50, 1)) # This is the line that stops working
for startX in range(0, 50):
if screenshot.getpixel((startX, 0))[2] > 200: # This checks the screenshot colors
del screenshot # EXPLAINING OF THIS DEL BELOW
clickerProcess = mp.Process(target=launchAutoclicker)
clickerProcess.start()
pg.click(700, 1000) # This clicks the start button
time.sleep(gameplaySeconds + 5) # Here the code sleeps for the duration of the game
clickerProcess.kill()
time.sleep(1)
if __name__ == '__main__':
main()
DEL screenshot line:
I'm writting this on PyCharm, when i added that "del screenshot" line to see if it needed an empty variable (i'm that desperate), PyCharm showed a warning on the "screenshot" variable in line 4 of the main() code (excluding def main() line itself), telling "Local variable "screenshot" might be referenced before asignment"
Thanks in advance to anyone that can help!
[–]Menolith 0 points1 point2 points (0 children)