Skip to content

Commit

Permalink
feat: auto-play game support
Browse files Browse the repository at this point in the history
  • Loading branch information
icenfame committed Mar 12, 2022
1 parent 8fcb005 commit 2d2ed5b
Showing 1 changed file with 57 additions and 23 deletions.
80 changes: 57 additions & 23 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Automation module"""
"""Bot for Telegram game LumberJack"""
import sys
from time import sleep
import pyautogui
import numpy as np
import keyboard
Expand All @@ -14,6 +16,10 @@ def detect_game():
pyautogui.locateOnScreen('regions/buttons/left.png')
)

if not button:
sys.exit(
"[ERROR]: Cannot find game. Make sure you have opened game on your screen")

canvas_width = 600
white = (255, 255, 255)
background = (51, 51, 51)
Expand Down Expand Up @@ -46,10 +52,9 @@ def detect_game():


def screenshot(region):
"""Make screenshot of specific region"""
"""Take a screenshot of a specific region"""

img = pyautogui.screenshot(region=region)
return img
return pyautogui.screenshot(region=region)


def check_player(region, img):
Expand All @@ -61,58 +66,87 @@ def check_player(region, img):

if img.getpixel((head_x, region[3] - head_y)) == head_color:
return "left"
elif img.getpixel((region[2] - head_x, region[3] - head_y)) == head_color:

if img.getpixel((region[2] - head_x, region[3] - head_y)) == head_color:
return "right"
else:
exit("Player not found")

return None


def check_wood(region, img, player):
"""Check wood over the player head"""
def check_branch(region, img, player):
"""Check branch over the player head"""

wood_y = region[3] - 498
wood_w = 32
wood_h = 100
branch_y = region[3] - 498
branch_w = 32
branch_h = 100
branch_color = (126, 173, 79)

if player == "left":
wood_x = 230
branch_x = 230
elif player == "right":
wood_x = region[2] - 230 - wood_w
branch_x = region[2] - 230 - branch_w

# TODO find all woods by pixel

img = img.crop((wood_x, wood_y, wood_x + wood_w, wood_y + wood_h))
img = img.crop((branch_x, branch_y, branch_x +
branch_w, branch_y + branch_h))
arr = np.array(img)
arr = arr.reshape((-1, 3))

if [126, 173, 79] in arr:
if branch_color in arr:
return player

return None


def play(region):
"""Auto-play game"""

play_button = (
pyautogui.locateOnScreen('regions/buttons/play.png') or
pyautogui.locateOnScreen('regions/buttons/replay.png')
)

if play_button:
pyautogui.click(play_button)

pyautogui.click(region)


def main():
"""Main function"""

region = detect_game()
score = 0

# TODO auto start game & end if lost
play(region)
sleep(1)

while True:
if keyboard.is_pressed('esc'):
break
sys.exit("[INFO]: User ESC exit")

img = screenshot(region)
player = check_player(region, img)
wood = check_wood(region, img, player)

if player == wood == "left":
if not player:
if score > 0:
print("[INFO]: Score", score - 2)

break

branch = check_branch(region, img, player)

if player == branch == "left":
pyautogui.press("right")
pyautogui.press("right")
elif player == wood == "right":
elif player == branch == "right":
pyautogui.press("left")
pyautogui.press("left")
else:
pyautogui.press(player)
pyautogui.press(player)

score += 2


if __name__ == "__main__":
main()

0 comments on commit 2d2ed5b

Please sign in to comment.