Open In App

Automate Chrome Dino Game using Python

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Chrome Dino game appears when your internet connection is down in Chrome. In this article, we’ll build a Python bot that can play the game by automatically making the dinosaur jump to avoid cacti and duck to avoid birds. Before we start coding, we need to install a few Python libraries:

  • pyautogui: Automates the GUI by taking screenshots and interacting with the screen.
  • pillow: Handles image processing and pixel manipulation.
  • keyboard: Allows the bot to simulate key presses like jumping or ducking.

Install the required libraries by running:

pip install pyautogui pillow keyboard

Stepwise Implementation to Create a Dino Game

Step 1: Import the Required module.

Python
import pyautogui as gui
import keyboard
from PIL import Image, ImageGrab
import time
import math

Step 2: Get pixel color at specific coordinates

We’ll need to check the color of certain pixels to detect obstacles in the game. Here’s a function that retrieves the color of a pixel at a given position.

Python
def get_pixel(image, x, y):
    px = image.load()
    return px[x, y]

Step 3: Take Screenshots to Monitor the Game

We’ll take a screenshot of the game area to check for obstacles. The screenshot will be saved and analyzed to detect any changes in the game that require the bot to act.

Python
def start():
    # Screenshot area
    x, y, w, h = 0, 102, 1920, 872

    # Time variables
    jump_time, last_jump, curr_jump, last_int = 0, 0, 0, 0

    # Search parameters
    y1, y2, x_s, x_e = 557, 486, 400, 415
    y_bird = 460

    # Allow 3s for interface switch
    time.sleep(3)
    while True:
        t1 = time.time()

        # Exit on 'q'
        if keyboard.is_pressed('q'):
            break

        img = gui.screenshot(region=(x, y, w, h))
        img.save("dino.jpg")

        # Get background color
        bg_color = get_px(img, 100, 100)

        # Check for obstacles
        for i in reversed(range(x_s, x_e)):
            if get_px(img, i, y1) != bg_color or get_px(img, i, y2) != bg_color:
                keyboard.press('up')
                jump_time = time.time()
                curr_jump = jump_time
                break
            if get_px(img, i, y_bird) != bg_color:
                keyboard.press("down")
                time.sleep(0.4)
                keyboard.release("down")
                break

        # Adjust for speed
        int_time = curr_jump - last_jump
        if last_int != 0 and math.floor(int_time) != math.floor(last_int):
            x_e += 4
            if x_e >= w:
                x_e = w

        # Update times
        last_jump = jump_time
        last_int = int_time

Step 4: Run the bot

To start the bot, open Chrome and navigate to the Dino game by typing chrome://dino/ in the address bar.

  1. Run the Python script.
  2. Press the spacebar to make the bot start playing.
  3. Press Q to stop the bot, and it will save the last score as a screenshot.

Complete code

Python
import pyautogui as gui
import keyboard
from PIL import Image, ImageGrab
import time
import math

def get_px(img, x, y):
    px = img.load()
    return px[x, y]

def start():
    # Screenshot area
    x, y, w, h = 0, 102, 1920, 872

    # Time variables
    jump_time, last_jump, curr_jump, last_int = 0, 0, 0, 0

    # Search parameters
    y1, y2, x_s, x_e = 557, 486, 400, 415
    y_bird = 460

    # Allow 3s for interface switch
    time.sleep(3)
    while True:
        t1 = time.time()

        # Exit on 'q'
        if keyboard.is_pressed('q'):
            break

        img = gui.screenshot(region=(x, y, w, h))
        img.save("dino.jpg")

        # Get background color
        bg_color = get_px(img, 100, 100)

        # Check for obstacles
        for i in reversed(range(x_s, x_e)):
            if get_px(img, i, y1) != bg_color or get_px(img, i, y2) != bg_color:
                keyboard.press('up')
                jump_time = time.time()
                curr_jump = jump_time
                break
            if get_px(img, i, y_bird) != bg_color:
                keyboard.press("down")
                time.sleep(0.4)
                keyboard.release("down")
                break

        # Adjust for speed
        int_time = curr_jump - last_jump
        if last_int != 0 and math.floor(int_time) != math.floor(last_int):
            x_e += 4
            if x_e >= w:
                x_e = w

        # Update times
        last_jump = jump_time
        last_int = int_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.
Automate Chrome Dino Game using Python

Chrome dino game



Next Article
Article Tags :
Practice Tags :

Similar Reads