import pyautogui import time from PIL import ImageGrab class lumberjackBot(): __author__ = "EnriqueMoran" def __init__(self, x, y): self.branch_width = 60 self.vertical_branch_distance = 70 self.to_match = (169, 113, 65) self.speed = 0.08 self.sleep = 0.083 self.sleep_min = 0.078 self.sleep_max = 0.090 self.xl = x - self.branch_width self.xr = x + self.branch_width self.y = y 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) def play(self): last = [True]*5 pyautogui.typewrite(['right', 'right'], self.speed) while True: match_l, match_r = self.get_pixel() if match_l: print('left match') pyautogui.typewrite(['right', 'right'], self.speed) elif match_r: print('right match') pyautogui.typewrite(['left', 'left'], self.speed) else: if not last[-1]: pyautogui.typewrite(['right', 'right'], self.speed) 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) if __name__ == "__main__": pyautogui.PAUSE = 0 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') pyautogui.moveTo(x, y) time.sleep(0.3) print("Im playing...") lumberjack = lumberjackBot(x, y) lumberjack.play()