Python PyAutoGUI Image Processing Automation
Chrome Dino Bot

Project Overview

An automated bot that plays the Chrome dinosaur game by detecting obstacles and jumping over them using image processing and screen analysis.

Completed: 2022
Status: Completed
Project: Personal

Project Description

The Chrome Dino Bot is an automation script that plays the popular Chrome dinosaur game that appears when you have no internet connection. The bot uses image processing techniques to detect obstacles in the game and automatically jumps over them by pressing the space key.

As the game progresses and the speed increases, the bot dynamically adjusts its detection area to look further ahead, ensuring it can react to obstacles in time.

Technical Implementation

The bot is implemented in Python and uses several libraries for screen capture and control:

dino_bot.py
Copied to clipboard!
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

import time

import pyautogui

from PIL import ImageGrab

# Constants

START_COORDS = (260, 445)

END_COORDS = (280, 500)

TARGET_COLOR = (172, 172, 172)

JUMP_DELAY = 0.0

def is_target_color_present():

# Capture screen within the specified coordinates range

screenshot = ImageGrab.grab(bbox=(START_COORDS[0], START_COORDS[1], END_COORDS[0], END_COORDS[1]))

pixels = screenshot.load()

# Check if any pixel matches the target color

for x in range(screenshot.width):

for y in range(screenshot.height):

if pixels[x, y] == TARGET_COLOR:

return True

return False

def jump():

for _ in range(3):

pyautogui.press('space')

time.sleep(0.01)

# Delay before starting the bot (gives time to focus on the game)

time.sleep(4)

start_time = time.time()

while True:

if is_target_color_present():

jump()

# Check if 8 seconds have passed - adjust detection area

if time.time() - start_time >= 8:

if START_COORDS[0] < 300:

# Increment the x-coordinate of START_COORDS and END_COORDS by 8

START_COORDS = (START_COORDS[0] + 8, START_COORDS[1])

END_COORDS = (END_COORDS[0] + 8, END_COORDS[1])

elif 300 < START_COORDS[0] < 400:

# Increment the x-coordinate by 25 (look further ahead)

START_COORDS = (START_COORDS[0] + 25, START_COORDS[1])

END_COORDS = (END_COORDS[0] + 25, END_COORDS[1])

else:

START_COORDS = (START_COORDS[0] + 15, START_COORDS[1])

END_COORDS = (END_COORDS[0] + 15, END_COORDS[1])

start_time = time.time()

print("Updated")

print(START_COORDS)

print(END_COORDS)

# Adjust the delay between each iteration if needed

time.sleep(JUMP_DELAY)

Key Features

  • Real-time screen capture and analysis
  • Pixel-level obstacle detection
  • Adaptive detection area that moves further ahead as game speed increases
  • Multiple space press for reliable jumping
  • Game performance monitoring through console outputs

Development Process

The development of this bot involved several key steps:

  1. Identifying the screen coordinates where obstacles appear
  2. Determining the target color to detect (cactus and bird obstacles)
  3. Implementing the screen capture and pixel analysis logic
  4. Setting up the jumping mechanism
  5. Adding dynamic adjustment of the detection area to handle increasing game speed
  6. Testing and optimizing the bot's performance

Challenges & Solutions

  • Challenge: Increasing game speed making obstacle detection difficult
    Solution: Implemented dynamic adjustment of detection area based on time passed
  • Challenge: Reliably detecting obstacles of different shapes
    Solution: Used color-based detection instead of shape recognition

Results & Impact

The Chrome Dino Bot successfully automates gameplay and can achieve high scores beyond what most human players can manage. The bot demonstrates how simple image processing techniques can be used to create effective game automation systems. It serves as an educational tool to understand basic principles of computer vision and automation.

Go Back