Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
94 lines (62 loc) · 1.79 KB
/
Copy pathcode.py
File metadata and controls
94 lines (62 loc) · 1.79 KB
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
import pyautogui
import time
from PIL import ImageGrab
# DETECTION AREA
# print("Move your mouse to the point right in front of the dino (where obstables appear).")
# print("You have 5 seconds...")
# time.sleep(5)
# x, y = pyautogui.position()
# print(f"Coordinates: x={x}, y={y}")
# Area in front of the Dino
CHECK_LEFT = 170
CHECK_TOP = 520
CHECK_RIGHT = 350
CHECK_BOTTOM = 625
# Dark gray color of the cactus
OBSTACLE_COLOR = (83, 83, 83)
# Allows for small color differences
COLOR_TOLERANCE = 35
# Prevent repeated jumps for the same obstacle
JUMP_COOLDOWN = 0.55
# DETECT CACTUS
def obstacle_detected():
screenshot = ImageGrab.grab(
bbox=(
CHECK_LEFT,
535,
CHECK_RIGHT,
615
)
)
width, height = screenshot.size
for x in range(width):
dark_pixels = 0
for y in range(height):
r, g, b = screenshot.getpixel((x, y))
if (
abs(r - OBSTACLE_COLOR[0]) <= COLOR_TOLERANCE
and abs(g - OBSTACLE_COLOR[1]) <= COLOR_TOLERANCE
and abs(b - OBSTACLE_COLOR[2]) <= COLOR_TOLERANCE
):
dark_pixels += 1
else:
dark_pixels = 0
# A cactus has significant vertical height.
# Small ground bumps will not reach this count.
if dark_pixels >= 10:
return True
return False
# START
print("Starting in 5 seconds...")
print("Switch to the Dino game.")
time.sleep(5)
print("Bot started!")
last_jump_time = 0
# GAME LOOP
while True:
current_time = time.time()
if obstacle_detected():
if current_time - last_jump_time >= JUMP_COOLDOWN:
pyautogui.press("space")
last_jump_time = current_time
time.sleep(0.01)