-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathdino.py
73 lines (65 loc) · 1.73 KB
/
dino.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
from PIL import ImageGrab, ImageOps
import pyautogui
import time
import numpy as np
class Bot:
"""Bot for playing Chrome dino run game"""
def __init__(self):
self.restart_coords = (480, 503)
self.dino_coords = (207, 534)
self.area = (self.dino_coords[0] + 90, self.dino_coords[1],
self.dino_coords[0] + 150, self.dino_coords[1] + 5)
def set_dino_coords(self, x, y):
"""
Change default dino coordinates
:param x: top right x coordinate (int)
:param y: top right y coordinate (int)
:return: None
"""
self.dino_coords = (x, y)
def set_restart_coords(self, x, y):
"""
Change default restart button coordinates
:param x: center x coordinate (int)
:param y: center y coordinate (int)
:return: None
"""
self.restart_coords = (x, y)
def restart(self):
"""
Restart the game and set default crawl run
:return: None
"""
pyautogui.click(self.restart_coords)
pyautogui.keyDown('down')
def jump(self):
"""
Jump over the obstacle
:return: None
"""
pyautogui.keyUp('down')
pyautogui.keyDown('space')
time.sleep(0.095)
pyautogui.keyUp('space')
pyautogui.keyDown('down')
def detection_area(self):
"""
Checks the area to have obstacles
:return: float
"""
image = ImageGrab.grab(self.area)
gray_img = ImageOps.grayscale(image)
arr = np.array(gray_img.getcolors())
# print(arr.mean())
return arr.mean()
def main(self):
"""
Main loop of the playing
:return: None
"""
self.restart()
while True:
if self.detection_area() < 273:
self.jump()
bot = Bot()
bot.main()