Skip to content

Commit

Permalink
Refactor of code and updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedeparg committed Jul 17, 2022
1 parent c2a951f commit 5948371
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LumberJack Telegram Bot 🪓
# LumberJack Telegram Bot 🪵🪓

Bot made for LumberJack telegram game. It recognises a couple of key pixels on your screen and makes lumberjack dodge tree's branches.

Expand All @@ -8,8 +8,15 @@ Bot made for LumberJack telegram game. It recognises a couple of key pixels on y

The script searches for the play button anywhere on the screen. Once it is pressed, it tries to locate the most lower branch and take it into account to play the game.

After that, it uses PyAutoGUI to simulate user key inputs and play the game
After that, it uses PyAutoGUI to simulate user inputs and play the game.

## Usage example 👀

Just run the python script after the installation of the requirements through `pip install -r requirements.txt` and place the browser window with the game in focus. After that, just relax and enjoy beating your friends!
Here you can check for a video showcase of the bot playing the game (image clickable):
[![Showcase video](https://img.youtube.com/vi/09UVK9AhwZQ/0.jpg)](https://www.youtube.com/watch?v=09UVK9AhwZQ )

First install the requierements through `pip install -r requirements.txt`.

**Optional:** I recommend creating a [virtual enviornment](https://docs.python.org/3/tutorial/venv.html) for testing and development.

After that, just run `python lumberjacBot.py` and place the browser window with the game in focus. Then relax and enjoy beating your friends! 😄
74 changes: 38 additions & 36 deletions lumberjackBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def capture(self, region=None):

if region is None:
region = CG.CGRectInfinite
else:
# else:
# TODO: Odd widths cause the image to warp. This is likely
# caused by offset calculation in ScreenPixel.pixel, and
# could could modified to allow odd-widths
if region.size.width % 2 > 0:
emsg = "Capture region width should be even (was %s)" % (
region.size.width)
raise ValueError(emsg)
# if region.size.width % 2 > 0:
# emsg = "Capture region width should be even (was %s)" % (
# region.size.width)
# raise ValueError(emsg)

# Create screenshot as CGImage
image = CG.CGWindowListCreateImage(
Expand All @@ -56,7 +56,7 @@ def pixel(self, x, y):

# Pixel data is unsigned char (8bit unsigned integer),
# and there are for (blue,green,red,alpha)
data_format = "BBBB"
data_format = 'BBBB'

# Calculate offset, based on
# http://www.markj.net/iphone-uiimage-pixel-color/
Expand All @@ -71,54 +71,56 @@ def pixel(self, x, y):

class lumberjackBot():

def __init__(self, playX, playY, treeX, treeY, x, y):
def __init__(self, playX, playY, treeX, treeY, branchX, branchY):
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 # Left side branch Y location
if self.x/2 > self.treeX/2:
self.branchX = branchX
self.branchY = branchY
# Know if branch is right or left of the tree
if self.branchX/2 > self.treeX/2:
self.right = True
else:
self.right = False
self.movement_buffer = ['right']

self.pixel = ScreenPixel()
self.region = CG.CGRectMake(x/2, y/2, 2, 2)
# We just need to check for 1 pixel where the branch is
self.region = CG.CGRectMake(branchX/2, branchY/2, 1, 1)

def move(self, direction):
self.movement_buffer.append(direction)
speed = 0.032 # 2 frames
if self.movement_buffer[0] == "left":
speed = 0.033 # 2 frames aprox.

# Always double movement because there is a gap between branches
if self.movement_buffer[0] == 'left':
print('left')
pyautogui.typewrite(['left', 'left'], speed)
elif self.movement_buffer[0] == "right":
elif self.movement_buffer[0] == 'right':
print('right')
pyautogui.typewrite(['right', 'right'], speed)
self.movement_buffer = self.movement_buffer[1:]
#time.sleep(0.06)

def play(self):
while True:
self.pixel.capture(region=self.region)
pixel_color = self.pixel.pixel(0, 0)
if self.right:
if pixel_color[2] < 200:
self.move("left")
self.move('left')
else:
self.move("right")
self.move('right')
else:
if pixel_color[2] < 200:
self.move("right")
self.move('right')
else:
self.move("left")
self.move('left')


def bottom_most_branch(branches):
def lowest_branch(branches):
bottom_most = None
# Search for the branch most lower
# Search for the lowest branch
for branch in branches:
if bottom_most is None:
bottom_most = branch
Expand All @@ -127,23 +129,23 @@ def bottom_most_branch(branches):
return bottom_most[:2]


if __name__ == "__main__":
print("Running in 3 seconds, minimize this windows. To stop the program drag the mouse to the top-left corner of your screen.")
if __name__ == '__main__':
print(f'Running in 3 seconds. To stop the program drag the mouse to the top-left corner of your screen.')
time.sleep(3)
playX, playY = pyautogui.locateCenterOnScreen('imgs/play.png', confidence=0.9)
playX, playY = pyautogui.locateCenterOnScreen(
'imgs/play.png', confidence=0.9)
# Any resolution divided by 2 is to take into account retina display and defualt
# screen scale that Apple uses
playX, playY = round(playX/2), round(playY/2)
pyautogui.moveTo(playX, playY)
pyautogui.click() # Start the game by pressing play button
# Start the game by pressing play button. Does not work with replay.
pyautogui.click()
time.sleep(0.5) # Wait for screen refresh
branches = pyautogui.locateAllOnScreen('imgs/branch.png', confidence=0.9)
x, y = bottom_most_branch(branches)
#x, y = x/2, y/2
pyautogui.moveTo(x/2, y/2 + 5)
treeX, treeY = playX - 6, playY - 177 # Tree position
#treeX, treeY = treeX/2, treeY/2
# time.sleep(0.3)
treeX, treeY = pyautogui.locateCenterOnScreen('imgs/tree.png', confidence=0.9)
print(f"Arbol: {treeX/2}, {treeY/2}")
print("Im playing...")
lumberjack = lumberjackBot(playX, playY, treeX, treeY, x, y)
branchX, branchY = lowest_branch(branches)
pyautogui.moveTo(branchX/2, branchY/2 + 5)
treeX, treeY = pyautogui.locateCenterOnScreen(
'imgs/tree.png', confidence=0.9)
print(f'Im playing...')
lumberjack = lumberjackBot(playX, playY, treeX, treeY, branchX, branchY)
lumberjack.play() # Game start

0 comments on commit 5948371

Please sign in to comment.