Created
October 8, 2019 22:32
-
-
Save divadsn/7d8b8644dca90caafaf23fbce7869a41 to your computer and use it in GitHub Desktop.
Basically I had too much time again with Python...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from mss import mss | |
from PIL import Image | |
from pynput.keyboard import Key, Controller | |
keyboard = Controller() | |
sct = mss() | |
# check for any "brown" pixel | |
def check_tree(pixels, x, y): | |
for off in range(100): | |
if pixels[x, y + off] == (161, 116, 56): | |
return True | |
return False | |
# game window | |
monitor = {"top": 560, "left": 824, "width": 272, "height": 170} | |
is_left = False | |
# FPS counter | |
frames = 0 | |
start_time = time.time() | |
while True: | |
sct_img = sct.grab(monitor) | |
# Create an Image | |
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX") | |
pixels = img.load() | |
if check_tree(pixels, 65, 70): | |
is_left = True | |
elif check_tree(pixels, 190, 70): | |
is_left = False | |
if is_left: | |
keyboard.press(Key.right) | |
keyboard.release(Key.right) | |
else: | |
keyboard.press(Key.left) | |
keyboard.release(Key.left) | |
# below 0.07 was overkill | |
time.sleep(0.07) | |
frames += 1 | |
print(f"FPS: {float(frames / (time.time() - start_time))}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Average FPS should be
13.76
, otherwise it may press the key too early or too late.