Automate Chrome Dino Game using Python
For this article, we will be building a Python bot that can play the “Chrome Dino Offline Game“. Chrome dino is a dinosaur game that launches itself on your web browser when there is no internet connection. For the introduction of the game, the dinosaur has to jump to avoid the approaching cactus or duck to avoid the bird.
The first fundamental step would be to install Python IDE on the computer which we can do from the official website:
- pyautogui: This is a GUI automation tool library that will help us to take screenshots of the screen and select the intensity of each pixel in the screen.
- pillow: Pillow is an open-source Python library for image processing. Pillow provides tools for handling and transforming image data, including reading, writing, and manipulating image files.
- keyboard: A library that allows the user to simulate keyboard input and to control and read the keyboard events, This will give us direct access to the keyboard attached to the system and control the dino to jump or duck.
Required Modules
Here are the following modules we need to install before we head onto the coding part:
pip install pyautogui pip install pillow pip install keyboard
Stepwise Implementation to Create a Dino Game in Python
Step 1: Import the Required module.
Python3
import pyautogui as gui import keyboard from PIL import Image, ImageGrab import time import math |
Step 2: A function for returning the pixel of the trigger point
The function loads the pixel data of the image using px = image.load(). The load method returns a matrix-like object that represents the image’s pixel data. Finally, the function returns the pixel value at the specified x and y coordinates using px[x, y].
Python3
def get_pixel(image, x, y): px = image.load() return px[x, y] |
Step 3: Saving the screenshot of the picture to know when there is a change of pixels at the trigger point.
The function starts by setting some variables for the size of the image to be taken and calculating the time for the robot to jump. The function then enters an infinite loop using Python while True. The loop continues until the q key is pressed, which is checked using if keyboard.is_pressed(‘q’). In each iteration of the loop, the function takes a screenshot of the current game window using sct_img = gui.screenshot(region=(x, y, width, height)) and saves it as an image named “dino.jpg” using sct_img.save(“dino.jpg”). The function then gets the background color of the screenshot image using the get_pixel function which was defined previously.
Python3
def start(): # set size of the image to be taken x, y, width, height = 0 , 102 , 1920 , 872 # calculating time jumping_time = 0 last_jumping_time = 0 current_jumping_time = 0 last_interval_time = 0 # interval for bot to find obstacles y_search1, y_search2, x_start, x_end = 557 , 486 , 400 , 415 y_search_for_bird = 460 # allowing 3s to switch the interface to Google Chrome # after the program is executed time.sleep( 3 ) while True : t1 = time.time() # press q to exit the robot if keyboard.is_pressed( 'q' ): break sct_img = gui.screenshot(region = (x, y, width, height)) sct_img.save( "dino.jpg" ) # get the background color of the screenshot image bg_color = get_pixel(sct_img, 100 , 100 ) # To be continued in next step |
Step 4: Here we finally implement the main logic for automating the game
The code has a for loop that iterates through the range of x_start to x_end in reverse order using for i in reversed(range(x_start, x_end)). In each iteration, the code checks if the color of the pixels at two specified coordinates is different from the background color. If this is the case, the code simulates a jump by pressing the “up” key on the keyboard using keyboard.press(‘up’). The time of the jump is recorded and stored in the jumping_time variable.
The code then checks if the color of the pixel at another specified coordinate is different from the background color using if get_pixel. If this is the case, the code simulates a duck by pressing the “down” key on the keyboard using keyboard.press(“down”), wait for 0.4 seconds using time.sleep(0.4), and then release the “down” key using the keyboard.release(“down”).
The code then calculates the time between the current jump and the last jump and stores it in the interval_time variable. If the intervals between the last two jumps are not the same and the time between the last jump and the previous one is not equal to 0, it means that the game is accelerating, and the code increases the value of x_end by 4. Finally, the code updates the values of last_jumping_time and last_interval_time before starting the next iteration.
Python3
for i in reversed ( range (x_start, x_end)): # color of the pixel does not match the # color of the background color if get_pixel(sct_img, i, y_search1) ! = bg_color \ or get_pixel(sct_img, i, y_search2) ! = bg_color: keyboard.press( 'up' ) jumping_time = time.time() current_jumping_time = jumping_time break if get_pixel(sct_img, i, y_search_for_bird) ! = bg_color: keyboard.press( "down" ) time.sleep( 0.4 ) # press keyboard arrow down to duck keyboard.release( "down" ) break # Time between this jump and the last one interval_time = current_jumping_time - last_jumping_time # game is accelerating if the intervals not same if last_interval_time ! = 0 and math.floor (interval_time) ! = math.floor(last_interval_time): x_end + = 4 if x_end > = width: x_end = width # get the last jump last_jumping_time = jumping_time # get the time between the last jump and the previous one last_interval_time = interval_time start() |
Complete Code
Python3
import pyautogui as gui import keyboard from PIL import Image, ImageGrab import time import math def get_pixel(image, x, y): px = image.load() return px[x, y] def start(): # set size of the image to be taken x, y, width, height = 0 , 102 , 1920 , 872 # calculating time jumping_time = 0 last_jumping_time = 0 current_jumping_time = 0 last_interval_time = 0 # interval for bot to find obstacles y_search1, y_search2, x_start, x_end = 557 , 486 , 400 , 415 y_search_for_bird = 460 # allowing 3s to switch the interface to Google Chrome # after the program is executed time.sleep( 3 ) while True : t1 = time.time() # press q to exit the robot if keyboard.is_pressed( 'q' ): break sct_img = gui.screenshot(region = (x, y, width, height)) sct_img.save( "dino.jpg" ) # get the background color of the screenshot image bg_color = get_pixel(sct_img, 100 , 100 ) for i in reversed ( range (x_start, x_end)): # color of the pixel does not match the # color of the background color if get_pixel(sct_img, i, y_search1) ! = bg_color \ or get_pixel(sct_img, i, y_search2) ! = bg_color: keyboard.press( 'up' ) jumping_time = time.time() current_jumping_time = jumping_time break if get_pixel(sct_img, i, y_search_for_bird) ! = bg_color: keyboard.press( "down" ) time.sleep( 0.4 ) # press keyboard arrow down to duck keyboard.release( "down" ) break # Time between this jump and the last one interval_time = current_jumping_time - last_jumping_time # game is accelerating if the intervals not same if last_interval_time ! = 0 and math.floor (interval_time) ! = math.floor(last_interval_time): x_end + = 4 if x_end > = width: x_end = width # get the last jump last_jumping_time = jumping_time # get the time between the last jump and the previous one last_interval_time = interval_time start() |
Output:
Just follow the below steps to run the bot:
- Open on chrome browser: and use this website: chrome://dino/
- Now go back to IDE and run the program.
- Press the space button and the dino will jump automatically.
- And, press Q to quit the game and it will take the last score screenshot in your local directory.
Contact Us