-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
145 lines (102 loc) · 3.4 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Bot for Telegram game LumberJack"""
import sys
from time import sleep
import pyautogui
import keyboard
def detect_game():
"""Detect game on a screen"""
screen = pyautogui.screenshot()
button = (
pyautogui.locateOnScreen('regions/buttons/play.png') or
pyautogui.locateOnScreen('regions/buttons/replay.png') or
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)
blue = (211, 247, 255)
# Left
for left in range(button.left.item(), 0, -1):
if screen.getpixel((left - 1, button.top.item())) != white:
game_x = left
break
# Right
if screen.getpixel((game_x + canvas_width - 1, button.top.item())) == white:
game_w = canvas_width
# Top
for top in range(button.top.item(), 0, -1):
if (screen.getpixel((game_x - 1, top - 1)) != background and
screen.getpixel((game_x, top + 1)) == blue):
game_y = top
break
# Bottom
for bottom in range(button.top.item(), screen.height, 1):
if screen.getpixel((game_x, bottom)) != white:
game_h = bottom - game_y
break
return (game_x, game_y, game_w, game_h)
def screenshot(region):
"""Take a screenshot of a specific region"""
return pyautogui.screenshot(region=region)
def check_player(region, img):
"""Check player position by the head"""
head_x = 240
head_y = 388
head_color = (51, 93, 101)
if img.getpixel((head_x, region[3] - head_y)) == head_color:
return "left"
if img.getpixel((region[2] - head_x, region[3] - head_y)) == head_color:
return "right"
return None
def check_branch(region, img, player, offset_y):
"""Check a branch on a screen"""
branch_y = region[3] - 399
branch_color = (136, 99, 50)
if player == "left":
branch_x = 240
elif player == "right":
branch_x = region[2] - 240
return img.getpixel((branch_x, branch_y - (100 * offset_y))) == branch_color
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)
sleep(1)
def main():
"""Main function"""
pyautogui.PAUSE = 1 / 60
region = detect_game()
score = 0
play(region)
while True:
sleep(0.2)
if keyboard.is_pressed('esc'):
sys.exit("[INFO]: User ESC exit")
img = screenshot(region)
player = check_player(region, img)
if not player:
if score > 0:
print(f"[INFO]: Score {score}")
break
for offset_y in range(6):
if check_branch(region, img, player, offset_y):
if player == "left":
pyautogui.press("right")
pyautogui.press("right")
elif player == "right":
pyautogui.press("left")
pyautogui.press("left")
else:
pyautogui.press(player)
pyautogui.press(player)
score += 12
if __name__ == "__main__":
main()