I want to automate the process of clicking a few buttons in a game, so I wrote a script that looks for the first button and clicks it, then clicks the other two.
import pyautogui as pag
import time
while True:
if Click("Replay") == True:
time.sleep(1)
Click("Continue")
time.sleep(1)
Click("Repeat")
time.sleep(1)
else:
time.sleep(5)
The Click() function basically takes a string input and turns it into input.png, matches it to a png i have of the buttons, and clicks it. If it doesn't find it, it just says it couldnt find it.
Heres that in case you need it:
def Click(image):
image = str(image)
location = pag.locateOnScreen(image ".png")
if location != None:
point = pag.center (location)
coordsX, coordsY = point
pag.click(coordsX, coordsY)
print("Clicked %s" % (image))
return True
else:
print("Did not find %s Button" % (image))
My end goal was to make it into an executable so I could send it to people and they could use it, but I don't want it to waste a lot of memory on the computers it runs on. I also don't want to increase the wait time on it, and I believe I saw somewhere it clears itself after 2GB or something, but I'm not that good at coding and I dont want it using that much ram in the first place. Is there anything I can do to either A. clear it at a lower threshold, or B. make it not use that much ram in the first place?
CodePudding user response:
I did something similar before, using pyautogui. I didn't see any performance issue when running similar code even after a few weeks.
You might want to try that out first as I doubt it will use that many RAM.
CodePudding user response:
A few optimizations/cleanups to your code:
- You don't need the else for
time.sleep(5). - Consider adding a
return Falseto yourClick()function's else clause? - Pyautogui's website says that the
locateOnScreenfunction will raise an error now, not returnNone, so consider using a try-except block. (If it doesn't return an exception for you, refer to the accepted answer to this question.) - Pyautogui allows clicking a Point object, so use this instead:
def Click(image):
image = str(image)
try:
pag.click(pag.locateCenterOnScreen(image ".png"))
print("Clicked %s" % (image))
return True
except pag.ImageNotFoundException:
print("Did not find %s Button" % (image))
return False
I've run this code,and it uses almost no RAM for me.
