Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Basic automation python

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading