Skip to content

Instantly share code, notes, and snippets.

@divadsn
Created October 8, 2019 22:32
Show Gist options
  • Save divadsn/7d8b8644dca90caafaf23fbce7869a41 to your computer and use it in GitHub Desktop.
Save divadsn/7d8b8644dca90caafaf23fbce7869a41 to your computer and use it in GitHub Desktop.
Basically I had too much time again with Python...
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))}")
@divadsn
Copy link
Author

divadsn commented Oct 8, 2019

Average FPS should be 13.76, otherwise it may press the key too early or too late.

@divadsn
Copy link
Author

divadsn commented Oct 8, 2019

My screen as a reference for the game window
grafik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment