updated recognition

This commit is contained in:
Enrique Moran 2018-05-18 20:44:59 +02:00 committed by GitHub
parent 0f6ccb0125
commit 6aa919c24b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 34 deletions

View File

@ -14,19 +14,18 @@ class lumberjackBot():
self.treeX = treeX self.treeX = treeX
self.treeY = treeY self.treeY = treeY
# Those attributes has been placed here in order to save calcs: # Those attributes has been placed here in order to save calcs:
self.pixelL = (0,0,0) # Left side branch color self.x = treeX - 22 # Left side branch X location
self.pixelR = (0,0,0) # Right side branch color self.y = treeY - 244 # Left side branch Y location
self.lX = treeX - 30 # Left side branch X location self.movement_buffer = ["right"] # First movement
self.rX = treeX + 30 # Right side branch X location
self.y = treeY - 132 # Both side branch Y location
def move(self, direction): def move(self):
speed = 0.2 speed = 0.17
if direction == "left": if self.movement_buffer[0] == "left" and len(self.movement_buffer) == 2:
pyautogui.typewrite(['left', 'left'], speed) pyautogui.typewrite(['left', 'left'], speed)
elif direction == "right": elif self.movement_buffer[0] == "right" and len(self.movement_buffer) == 2:
pyautogui.typewrite(['right', 'right'], speed) pyautogui.typewrite(['right', 'right'], speed)
self.movement_buffer = self.movement_buffer[1:]
def get_color(self, rgb): def get_color(self, rgb):
r = rgb & 0xff r = rgb & 0xff
@ -34,35 +33,26 @@ class lumberjackBot():
b = (rgb >> 16) & 0xff b = (rgb >> 16) & 0xff
return r,g,b return r,g,b
def get_pixel(self, x, y, side): # Modify class atribute def get_pixel(self, x, y): # Modify class atribute
screen = windll.user32.GetDC(0) screen = windll.user32.GetDC(0)
rgb = windll.gdi32.GetPixel(screen, x, y) rgb = windll.gdi32.GetPixel(screen, x, y)
if side == 'L': return self.get_color(rgb)
self.pixelL = self.get_color(rgb)
elif side == 'R':
self.pixelR = self.get_color(rgb)
def play(self): def play(self):
self.move("right") pyautogui.moveTo(self.x, self.y)
while True: while True:
if self.pixelL == (161, 116, 56): pixel_color = self.get_pixel(self.x, self.y)
self.move("right") if pixel_color == (161, 116, 56):
elif self.pixelR == (161, 116, 56): self.movement_buffer.append("right")
self.move("left") else:
self.movement_buffer.append("left")
#print(pixel_color, self.x, self.y, self.movement_buffer)
def pixelThreadL(self): self.move()
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__": if __name__ == "__main__":
print("Running in 5 seconds, minimize this windows.") 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(5) time.sleep(3)
playX, playY = pyautogui.locateCenterOnScreen('playButton.png') playX, playY = pyautogui.locateCenterOnScreen('playButton.png')
pyautogui.moveTo(playX, playY) pyautogui.moveTo(playX, playY)
pyautogui.click() # Start the game by pressing play button pyautogui.click() # Start the game by pressing play button
@ -71,8 +61,4 @@ if __name__ == "__main__":
time.sleep(0.3) time.sleep(0.3)
print("Im playing... To stop me click on IDLE and press CTRL+F6.") print("Im playing... To stop me click on IDLE and press CTRL+F6.")
lumberjack = lumberjackBot(playX, playY, treeX, treeY) 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 lumberjack.play() # Game start