lumberjackBot/lumberjackBot.py

78 lines
2.8 KiB
Python
Raw Normal View History

2016-12-07 00:46:29 +08:00
import pyautogui
import time
2021-02-26 21:18:55 +08:00
from PIL import ImageGrab
2021-10-06 21:14:43 +08:00
import threading
2016-12-07 00:46:29 +08:00
2021-02-26 21:28:07 +08:00
# monkey patch
pyautogui.pyscreeze.ImageGrab = ImageGrab
pyautogui.pyscreeze.screenshot = pyautogui.pyscreeze._screenshot_win32
2021-10-06 21:14:43 +08:00
def pyautogui_typewrite(btn, sleepintv=0.050*2, typeintv=0.030):
threading.Thread(target=pyautogui.typewrite, args=[btn, typeintv]).start()
time.sleep(sleepintv)
2016-12-07 00:46:29 +08:00
class lumberjackBot():
2016-12-08 23:12:27 +08:00
__author__ = "EnriqueMoran"
2021-02-26 21:18:55 +08:00
def __init__(self, x, y):
self.branch_width = 60
2021-10-06 21:14:43 +08:00
self.vertical_branch_distance = 120
2021-12-15 17:31:19 +08:00
self.to_match = (161, 116, 56)
2021-10-06 21:14:43 +08:00
self.speed = 0.078
2021-02-26 21:18:55 +08:00
self.sleep = 0.083
2021-10-06 21:14:43 +08:00
self.sleep_min = 0.045*2
self.sleep_max = 0.050*2
2016-12-08 23:12:27 +08:00
2021-02-26 21:18:55 +08:00
self.xl = x - self.branch_width
self.xr = x + self.branch_width
self.y = y
2016-12-08 23:12:27 +08:00
2021-02-26 21:18:55 +08:00
def get_pixel(self):
scrshot = ImageGrab.grab()
m = lambda x: scrshot.getpixel(x) == self.to_match
match_l = any(filter(m, [(self.xl, self.y-offset) for offset in range(self.vertical_branch_distance)]))
match_r = any(filter(m, [(self.xr, self.y-offset) for offset in range(self.vertical_branch_distance)]))
return (match_l, match_r)
2016-12-12 23:44:03 +08:00
2016-12-08 23:12:27 +08:00
def play(self):
2021-02-26 21:18:55 +08:00
last = [True]*5
2021-10-06 21:14:43 +08:00
pyautogui_typewrite(['right', 'right'], self.speed)
while True:
2021-02-26 21:18:55 +08:00
match_l, match_r = self.get_pixel()
if match_l:
print('left match')
2021-10-06 21:14:43 +08:00
pyautogui_typewrite(['right', 'right'], self.speed)
2021-02-26 21:18:55 +08:00
elif match_r:
print('right match')
2021-10-06 21:14:43 +08:00
pyautogui_typewrite(['left', 'left'], self.speed)
2021-02-26 21:18:55 +08:00
else:
if not last[-1]:
2021-10-06 21:14:43 +08:00
pyautogui_typewrite(['right', 'right'], self.speed)
2021-02-26 21:18:55 +08:00
print('!! no match')
last.append(match_l or match_r)
while len(last) > 5:
del last[0]
stutter = len(list(filter(lambda x: not x, last)))
if stutter == 0 and self.sleep > self.sleep_min:
self.sleep -= 0.001
print('dec sleep', self.sleep)
elif stutter > 1 and self.sleep < self.sleep_max:
self.sleep += 0.001
print('inc sleep', self.sleep)
if not any(filter(lambda x: x, last)):
print('dead')
break
time.sleep(self.sleep)
2016-12-07 00:46:29 +08:00
if __name__ == "__main__":
2021-02-26 21:18:55 +08:00
pyautogui.PAUSE = 0
2018-05-19 02:44:59 +08:00
print("Running in 3 seconds, minimize this windows. To stop the program drag the mouse to the top-left corner of your screen.")
time.sleep(3)
x, y = pyautogui.locateCenterOnScreen('branch.png')
2021-02-26 21:18:55 +08:00
pyautogui.moveTo(x, y)
time.sleep(0.3)
2021-02-26 21:18:55 +08:00
print("Im playing...")
lumberjack = lumberjackBot(x, y)
lumberjack.play()