Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python one button macro for games

Here is my Python 2.7.13 script that is basically a so called "one key macro" for a video game. Based on information captured from a part of the game screen it presses the right key combinations instead of the player. So the player is spamming the f key and the script presses other keys alongside f.

It is working as is, however at random times (1-5 minutes after start) the script just stops or something similar. I can see in windows task manager that the script is running, however nothing happens when I press the f key.

At the beggining I was wrote the code a little bit more unoptimised and it took a screenshot more than once / keypress. The script "froze" more often back then.

Can this be beacause of too many screenshots? Or did I messed up somewhere?

import pyautogui, pythoncom, pyHook

# Determine if an ability is ready or not

def ready(x,y, im):
    if (im.getpixel((x,y)) != (0,0,0)):
        return True
    else:
        return False

def ability1(im):
    return (ready(17, 16, im) or ready(35, 16, im))


def ability2(im):
    return ready(134, 9, im)

# Listen for keypress   
def OnKeyboardEvent(event):
    im = pyautogui.screenshot(region=(249, 770, 194, 26))

    if (event.KeyID == 70): # Pressed the "f" key
        if (ability1(im)):
            pyautogui.hotkey('shift','4')
            return True

        if (ability2(im)):
            pyautogui.press('-')
            return True           

        pyautogui.press('1')
        return True

# create a hook manager
hm = pyHook.HookManager()

# watch for all mouse events
hm.KeyDown = OnKeyboardEvent

# set the hook
hm.HookKeyboard()

# wait forever
pythoncom.PumpMessages()
like image 664
Norbi Avatar asked Feb 15 '26 05:02

Norbi


1 Answers

If you're running this on live servers, this could be due to increased checks/interference from Warden.

I advise against using such automation on live servers, could lead to a ban if noticed.

like image 103
Nathanyel Avatar answered Feb 17 '26 19:02

Nathanyel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!