From a815dc4b16e90c557d545124d2d027a5f0feec1a Mon Sep 17 00:00:00 2001 From: Enrique Moran Date: Tue, 13 Dec 2016 21:02:33 +0100 Subject: [PATCH] Version 1.3 Multithreading added. Sleep on playing has been removed. Efficiency highly increased. Lumberjack no longer gets hit. --- lumberjackBot.py | 81 ++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/lumberjackBot.py b/lumberjackBot.py index f911f00..3bda360 100644 --- a/lumberjackBot.py +++ b/lumberjackBot.py @@ -1,6 +1,7 @@ import pyautogui import time from ctypes import windll +import threading class lumberjackBot(): @@ -11,7 +12,14 @@ class lumberjackBot(): self.playX = playX self.playY = playY self.treeX = treeX - self.treeY = treeY + self.treeY = treeY + # Those attributes has been placed here in order to save calcs: + self.pixelL = (0,0,0) # Left side branch color + self.pixelR = (0,0,0) # Right side branch color + self.lX = treeX - 30 # Left side branch X location + self.rX = treeX + 30 # Right side branch X location + self.y = treeY - 130 # Both side branch Y location + def move(self, direction): if direction == "left": @@ -27,49 +35,42 @@ class lumberjackBot(): b = (rgb >> 16) & 0xff return r,g,b + def get_pixel(self, x, y, side): # Modify class atribute + screen = windll.user32.GetDC(0) + rgb = windll.gdi32.GetPixel(screen, x, y) + if side == 'L': + self.pixelL = self.get_color(rgb) + elif side == 'R': + self.pixelR = self.get_color(rgb) def play(self): - cont = 0 - while(1): - end = windll.user32.GetDC(0) - rgb = windll.gdi32.GetPixel(end, treeX - 30, treeY - 130) - stop = windll.gdi32.GetPixel(end, treeX - 30, treeY - 100) - print(str(self.get_color(rgb))) - if self.get_color(rgb) == (161, 116, 56) or self.get_color(rgb) == (153, 110, 54): - pyautogui.typewrite(['right']) - pyautogui.typewrite(['right']) - elif self.get_color(rgb) != (161, 116, 56) and self.get_color(rgb) != (153, 110, 54): - pyautogui.typewrite(['left']) - pyautogui.typewrite(['left']) - cont += 2 - if self.get_color(stop) == (255,255,255) or cont >= 420: - return 0 - time.sleep(0.075) # Speed of lumberjack + self.move("right") + while True: + if self.pixelL == (161, 116, 56): # or self.pixelL == (153, 110, 54): + self.move("right") + elif self.pixelR == (161, 116, 56): # or self.pixelR == (153, 110, 54): + self.move("left") + + def pixelThreadL(self): + while True: + self.get_pixel(self.lX, self.y, 'L') + + def pixelThreadR(self): + while True: + self.get_pixel(self.rX, self.y, 'R') if __name__ == "__main__": - # LUMBERJACK WINDOWS MUST BE CLEAR playX, playY = pyautogui.locateCenterOnScreen('playButton.png') - pyautogui.moveTo(playX, playY) # Start the game by pressing play button - pyautogui.click() - time.sleep(1) + pyautogui.moveTo(playX, playY) + pyautogui.click() # Start the game by pressing play button + time.sleep(0.5) # Wait for screen refresh treeX, treeY = pyautogui.locateCenterOnScreen('tree.png') # Recognize tree position - time.sleep(1) - print("Im playing...") - lumberjackBot(playX, playY, treeX, treeY).play() - - - - -''' SCORE: - -0.15 -> 254, 254, 256 -0.12 -> 278X, 288, 42, 314, 314 -0.11 -> 314, 314, 314 -0.1 -> 334, 334, 144X, 334, 334 -0.099 -> 334, 334, 334 -0.08 -> 354, 354, 352 -0.075 -> 116X, 28X, 84X, 122X -0.07 -> 70X, 250X, 104X, 8X, 34X - -''' \ No newline at end of file + time.sleep(0.3) + print("Im playing... To stop me click on IDLE and press CTRL+F6.") + lumberjack = lumberjackBot(playX, playY, treeX, treeY) + t1 = threading.Thread(target = lumberjack.pixelThreadL, args = ()) + t2 = threading.Thread(target = lumberjack.pixelThreadR, args = ()) + t1.start() + t2.start() + lumberjack.play() # Game start