fix shit code (rewrite)

This commit is contained in:
JerryXiao 2021-02-26 21:18:55 +08:00
parent 244978be12
commit 6da76dc344
Signed by: Jerry
GPG key ID: 9D9CE43650FF2BAA
3 changed files with 50 additions and 49 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 B

After

Width:  |  Height:  |  Size: 684 B

View file

@ -1,67 +1,68 @@
import pyautogui import pyautogui
import time import time
from ctypes import windll from PIL import ImageGrab
import threading
class lumberjackBot(): class lumberjackBot():
__author__ = "EnriqueMoran" __author__ = "EnriqueMoran"
def __init__(self, playX, playY, treeX, treeY, x, y): def __init__(self, x, y):
self.playX = playX self.branch_width = 60
self.playY = playY self.vertical_branch_distance = 70
self.treeX = treeX self.to_match = (169, 113, 65)
self.treeY = treeY self.speed = 0.08
# Those attributes has been placed here in order to save calcs: self.sleep = 0.083
self.x = x # Left side branch X location self.sleep_min = 0.078
self.y = y + 5 # Left side branch Y location self.sleep_max = 0.090
self.movement_buffer = ["right"] # First movement
self.xl = x - self.branch_width
self.xr = x + self.branch_width
self.y = y
def move(self): def get_pixel(self):
speed = 0.14 scrshot = ImageGrab.grab()
if self.movement_buffer[0] == "left" and len(self.movement_buffer) == 2: m = lambda x: scrshot.getpixel(x) == self.to_match
pyautogui.typewrite(['left', 'left'], speed) match_l = any(filter(m, [(self.xl, self.y-offset) for offset in range(self.vertical_branch_distance)]))
elif self.movement_buffer[0] == "right" and len(self.movement_buffer) == 2: match_r = any(filter(m, [(self.xr, self.y-offset) for offset in range(self.vertical_branch_distance)]))
pyautogui.typewrite(['right', 'right'], speed) return (match_l, match_r)
self.movement_buffer = self.movement_buffer[1:]
def get_color(self, rgb):
r = rgb & 0xff
g = (rgb >> 8) & 0xff
b = (rgb >> 16) & 0xff
return r,g,b
def get_pixel(self, x, y): # Modify class atribute
screen = windll.user32.GetDC(0)
rgb = windll.gdi32.GetPixel(screen, x, y)
return self.get_color(rgb)
def play(self): def play(self):
last = [True]*5
pyautogui.typewrite(['right', 'right'], self.speed)
while True: while True:
pixel_color = self.get_pixel(self.x, self.y) match_l, match_r = self.get_pixel()
if pixel_color == (161, 116, 56): if match_l:
self.movement_buffer.append("right") print('left match')
self.move() pyautogui.typewrite(['right', 'right'], self.speed)
elif pixel_color == (211, 247, 255) or pixel_color == (241, 252, 255): elif match_r:
self.movement_buffer.append("left") print('right match')
self.move() pyautogui.typewrite(['left', 'left'], self.speed)
#print(pixel_color, self.x, self.y, self.movement_buffer) 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__": 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.") 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) time.sleep(3)
playX, playY = pyautogui.locateCenterOnScreen('playButton.png')
pyautogui.moveTo(playX, playY)
pyautogui.click() # Start the game by pressing play button
time.sleep(0.5) # Wait for screen refresh
x, y = pyautogui.locateCenterOnScreen('branch.png') x, y = pyautogui.locateCenterOnScreen('branch.png')
pyautogui.moveTo(x, y + 5) pyautogui.moveTo(x, y)
treeX, treeY = playX - 6, playY - 177 # Tree position
time.sleep(0.3) time.sleep(0.3)
print("Im playing... To stop me click on IDLE and press CTRL+F6.") print("Im playing...")
lumberjack = lumberjackBot(playX, playY, treeX, treeY, x, y) lumberjack = lumberjackBot(x, y)
lumberjack.play() # Game start lumberjack.play()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 123 B