Skip to content

Commit 8428ace

Browse files
authored
Merge pull request #724 from oshi36/add-chromedino-game
chrome dino game automation in python
2 parents 7bbe9f7 + 4711823 commit 8428ace

File tree

7 files changed

+438
-138
lines changed

7 files changed

+438
-138
lines changed

Python/README.md

+318-138
Large diffs are not rendered by default.

Python/chromedino_script/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Chrome Dino Game Automation Using Python
2+
3+
## Description
4+
This python scripts enables chrome dinosaur to play chrome dino game automatically.
5+
6+
7+
## Execution
8+
* Run `pip install -r requirements.txt` .
9+
* Run `python coordinates.py` for knowing the cooridnates of your screen.
10+
* Go to the `chrome://dino` in your Google Chrome Browser.
11+
* Run `python enemy_location_checker.py` to know the location of cactus and bird (obstacles for dino).
12+
* Now , Run `python main.py`
13+
* Then go to the `chrome://dino` in your Google Chrome Browser and press any key from keyboard to start the game.
14+
* See how it plays and press a key to interrupt/stop the game.
15+
16+
17+
## Features of files
18+
### Coordinates.py:
19+
To know the coordinates of screen first we need to find the X and Y locations of screen .
20+
21+
### enemy_location_checker.py:
22+
To make dino identify the cactus and bird , captured the image of two make a frame of it and trained dino to escape it.
23+
As , there are only three colors present in this game : black(for night) , white(for day) and gray(for obstacles and dino)
24+
So , To match the gray pixels of obstacle to make identify dino for obstacle and if obstacle is close to dino then make a jump.
25+
For this take a image with obstacles coming in between using **ImageGrab from pillow library** and then find out the pixels in which the obstacles are coming.
26+
27+
### main.py
28+
As game continues, search width has to be increased for bot.For this **time module from python** helps to simulate the dino acceleration.
29+
Also reverse the loop for dino to get searched from back of screenshot to identify obstacle and it will speed up the program.
30+
31+
32+
33+
#### All the requirements for this script is mentioned in **requirements.txt** file.
34+
35+
36+
37+
38+
39+
40+
![Demo of the Game](demo_dinogame.gif)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pyautogui, sys
2+
3+
# To know the location of X and Y coordinates.
4+
print('Press Ctrl-C to quit.')
5+
try:
6+
while True:
7+
x, y = pyautogui.position()
8+
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
9+
print(positionStr, end='')
10+
print('\b' * len(positionStr), end='', flush=True)
11+
except KeyboardInterrupt:
12+
print('\n')
589 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pyautogui
2+
from PIL import Image, ImageGrab
3+
import time
4+
5+
# To check the obstacles
6+
while True:
7+
image = ImageGrab.grab().convert('L') # translating to greyscale (faster)
8+
data = image.load()
9+
10+
# cactus
11+
for i in range (275, 325):
12+
for j in range(563, 650):
13+
data[i, j] = 0
14+
15+
# bird
16+
for i in range (250, 300):
17+
for j in range(410, 563):
18+
data[i, j] = 200
19+
20+
image.show()
21+
break

Python/chromedino_script/main.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pyautogui as gui
2+
import keyboard
3+
import time
4+
import math
5+
6+
# Screen Dimensions for screen of size 1920X1080
7+
top, left, width, height = 293, 0, 700, 465
8+
9+
# helper variables to calculate time
10+
last = 0
11+
total_time = 0
12+
13+
# the intervals where the bot will search for obstacles
14+
y_search_cactus, x_start, x_end = 350, 350, 380
15+
y_search_bird = 275 # for the birds
16+
17+
18+
time.sleep(4)
19+
while True:
20+
t1 = time.time()
21+
if keyboard.is_pressed('q'): # Emergency Button
22+
break
23+
24+
# increase the search width every second to simulate the dino acceleration
25+
if math.floor(total_time) != last:
26+
x_end += 4
27+
if x_end >= width:
28+
x_end = width
29+
last = math.floor(total_time)
30+
31+
# Get a screen shot
32+
screenshot = gui.screenshot(region=(left,top, width, height))
33+
pixels = screenshot.load()
34+
35+
background_color = pixels[440, 30]
36+
37+
for i in reversed(range(x_start, x_end)):
38+
if pixels[i, y_search_cactus] != background_color\
39+
or pixels[i, y_search_bird] != background_color:
40+
keyboard.press(' ') # jump
41+
break
42+
43+
t2 = time.time()-t1
44+
total_time += t2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PyAutoGUI==0.9.52
2+
Pillow==8.1.0
3+
keyboard==0.13.5

0 commit comments

Comments
 (0)