forked from Raamyy/Chrome-Dino-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
66 lines (51 loc) · 1.62 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
import pyautogui as gui
import keyboard
import time
import math
# Helper function to get value of pixel in image
def getPixel(Image,x, y):
px = Image.load()
return px[x, y]
# Screen Dimensions
top, left, width, height = 293, 0, 1920, 465
screenDimensions = {
"top": top,
"left": left,
"width": width,
"height": height
}
# helper variables to calculate time
last = 0
total_time = 0
# the intervals where the bot will search for obstacles
y_search, x_start, x_end = 350, 435, 450
y_search2 = 275 # for the birds
time.sleep(1)
while True:
t1 = time.time()
if keyboard.is_pressed('e'): # Emergency Button
break
# increase the search width every second to simulate the dino acceleration
if math.floor(total_time) != last:
x_end += 4
if x_end >= width:
x_end = width
last = math.floor(total_time)
# a way to get a screen shot but it was too slow
# sct_img = sct.grab(screenDimensions)
# mss.tools.to_png(sct_img.rgb, sct_img.size, output="test.png")
# Get a screen shot
sct_img = gui.screenshot(region=(left,top, width, height))
# Get the color of the world background
bgColor = getPixel(sct_img, 440, 30)
for i in reversed(range(x_start, x_end)):
# if i found a pixel in the search interval with a colour other than the bg colour, then it is an obstacle
if getPixel(sct_img,i,y_search) != bgColor\
or getPixel(sct_img,i,y_search2) != bgColor:
keyboard.press(' ') # jump
break
t2 = time.time()-t1
total_time += t2
# DEBUG
print(x_end)
print(total_time,t2)