From 6da76dc3445e129b06ff25b4e235f8172c846df1 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 26 Feb 2021 21:18:55 +0800 Subject: [PATCH] fix shit code (rewrite) --- branch.png | Bin 114 -> 684 bytes lumberjackBot.py | 99 ++++++++++++++++++++++++----------------------- playButton.png | Bin 123 -> 0 bytes 3 files changed, 50 insertions(+), 49 deletions(-) delete mode 100644 playButton.png diff --git a/branch.png b/branch.png index e0c78c996f4b9e8ee4006699fc23cd3dabf8472c..9d2d6d7444854cdec0dca88822ac60dae3832ca7 100644 GIT binary patch literal 684 zcmeAS@N?(olHy`uVBq!ia0vp^N zr;B4q#hkadZTl}9NU(o6u3x!xhrYTWtD@m1BLf42kCr!nxYU@siK$JS>=$x%YuP20 zzOxrrEm`j2`MGFS-~W2=Usvp}mQ6lUdh>h!{FgS9Hk+UBxb>;(t5?Cjs4|c1yBZmp z*?1%j5)L%L87t?{YRJnl>U{FUMH1>X*?qc3{)%yw5>?~~W*;Tq)nSlUGe$Vggr z&A;-auoRm1onpuAxgXDY9r1GS`|~2HH#c6|$>=ldopo4L_}BED(5ICbmtJ~Na>3_z z*5lgKJ{Q!^b{7dG=We}cGi_;Z&DHAtm)&Of@M#A)2Td#6TRG?aBn=+R=W`7G?^(W| zb31dg`0-4mLrx36*X=g$S=zJl*3QlEWODa@%sl*j=f7@VkVj%zDT_*5+g)3E zr^7IG`O7==dN^mUS@Ehp*wBC5+C6JHdC}tqsQ=8CiTv^QLKkbZ7pgzAt(^bD=tp4N z_uZ#wtZVJDNp?A-qx|cW-Jiwzb!!*jmoA$7&Ha1S&eV`KX-BinyqlgpXD+?bX`7Mr jrG5La%e(ca)-&pE+IGco#;vEogTe~DWM4fC#5$! literal 114 zcmeAS@N?(olHy`uVBq!ia0vp^tUxTm!3HFa)Xf(FDb50q$YKTtzQZ8Qcszea3Q$nR z)5S4FBRF|RrP^D2t7V-DcccFQe^HUat*5i`M}J>?+k&I@4Br-NoT$}my989v;OXk; Jvd$@?2>|(HB8&h4 diff --git a/lumberjackBot.py b/lumberjackBot.py index ffa1cd8..52d76d4 100644 --- a/lumberjackBot.py +++ b/lumberjackBot.py @@ -1,67 +1,68 @@ import pyautogui import time -from ctypes import windll -import threading - +from PIL import ImageGrab class lumberjackBot(): __author__ = "EnriqueMoran" - def __init__(self, playX, playY, treeX, treeY, x, y): - self.playX = playX - self.playY = playY - self.treeX = treeX - self.treeY = treeY - # Those attributes has been placed here in order to save calcs: - self.x = x # Left side branch X location - self.y = y + 5 # Left side branch Y location - self.movement_buffer = ["right"] # First movement + 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 move(self): - speed = 0.14 - if self.movement_buffer[0] == "left" and len(self.movement_buffer) == 2: - pyautogui.typewrite(['left', 'left'], speed) - elif self.movement_buffer[0] == "right" and len(self.movement_buffer) == 2: - pyautogui.typewrite(['right', 'right'], speed) - 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 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: - pixel_color = self.get_pixel(self.x, self.y) - if pixel_color == (161, 116, 56): - self.movement_buffer.append("right") - self.move() - elif pixel_color == (211, 247, 255) or pixel_color == (241, 252, 255): - self.movement_buffer.append("left") - self.move() - #print(pixel_color, self.x, self.y, self.movement_buffer) - - + 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) - 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') - pyautogui.moveTo(x, y + 5) - treeX, treeY = playX - 6, playY - 177 # Tree position + pyautogui.moveTo(x, y) time.sleep(0.3) - print("Im playing... To stop me click on IDLE and press CTRL+F6.") - lumberjack = lumberjackBot(playX, playY, treeX, treeY, x, y) - lumberjack.play() # Game start + print("Im playing...") + lumberjack = lumberjackBot(x, y) + lumberjack.play() diff --git a/playButton.png b/playButton.png deleted file mode 100644 index 77eccca9c98fa5a58aca4ad084628e057c042922..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 123 zcmeAS@N?(olHy`uVBq!ia0vp^l0eMC!3HFGl0#yF6lZ})WHAE+-(e7DJf6QI1t_TN z>Eak-;h+4YWXE&+wI5fgsyqzz_Bh0&zb*7{{qEf#|F{2@b!g1vVV&~$-G7D;_c|}# TSP`KJ)X3oJ>gTe~DWM4fnPe!r