I want to set up a simple macro in a game, but he has to press the keys until he sees "test.png". If he sees it, he should make a certain key combination and keep pressing the keys again, but he keeps pressing the keys without seeing "test.png".
import pyautogui
import time
play = pyautogui.locateOnScreen("test.png")
while True:
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=1512,y=929)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=865,y=488)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=909,y=753)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=1647,y=933)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=1781,y=946)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=1289,y=511)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=695,y=546)
>Solution :
It seems like you want to create a macro in a game using the PyAutoGUI library in Python. The macro should keep pressing certain keys until a specific image ("test.png") appears on the screen. Once the image is detected, a specific key combination should be executed.
Your current code has the right structure, but it’s missing the necessary steps to update the play variable inside the loop. You need to call pyautogui.locateOnScreen("test.png") within each iteration to check if the image is present.
Here’s an updated version of your code that should work:
import pyautogui
import time
while True:
play = pyautogui.locateOnScreen("test.png")
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=1512, y=929)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=865, y=488)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=909, y=753)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=1647, y=933)
if play:
pyautogui.click(x=1182, y=919)
time.sleep(5)
else:
pyautogui.click(x=1781, y=946)
In this code, the play variable is updated inside the while loop, allowing the program to detect the image in each iteration. If the image is found, it executes the corresponding actions. Otherwise, it performs the default actions specified in the else clauses.
Remember to replace the coordinates and add your specific key combinations based on your game’s requirements.