Play Google chrome dino game with python and pyautogui!

9 Jun 2018views: 1823

blog cover image

This is a great example of how we can easily use python pyautogui module to do some cool stuffs. Google chrome dino game is really popular and became a trademark of you are offline.

Here we will use python and pyautogui module to play the google chrome dino game. The process used here can also be modified to play other similar games.


  • Python3
  • pyautogui module
  • pillow module

Getting ready:

  • If you don't have python3 installed follow steps here.
  • Install pyautogui using pip install pyautogui in a command prompt
  • Install pillow using pip install pillow in a command prompt.

How it works ?

  1. Takes screenshot of specified area.
  2. Checks for particular pixel colour of obstacles.
  3. If obstacles appears use pyautogui to virtually press space key.
  4. Repeat this again and again.

Step 1:

Go offline and open chrome. Grab it and stick it on one side of the window either left or right. So it won't move and changes its coordinates on the screen.

step-1 of play chrome dino game using pyautogui

Step 2:

Take a screenshot when the plant is just a few centimetres away from the dino use PrtSc key. Then open or any other image editing application and paste it Ctrl+V.

step-2 of play chrome dino game using pyautogui

Step 3:

We need to mark a box where we want to detect the obstacles. Use the editor scale tool to mark the area. Then take a note of the two top-left and bottom-right corner points of the box area as indicated on the image below. These two coordinates will be used to make the box area in which we will detect obstacles. Then crop it.

step-3 of play chrome dino game using pyautogui

Step 4:

Zoom in as much as you can and take a note of two or three pixels coordinates on the plant as shown with Red dots on the image below.

step-4 of play chrome dino game using pyautogui

Stem 5:

Write the python code and save it as

import pyautogui
import time
import PIL.ImageGrab as ImageGrab
pyautogui.FAILSAFE = True

box_area1 = (919,229, 954, 270) #box coordinates(x1,y1, x2,y2) to detect obstacles
pixel_plant1 = (21, 33) #pixel coordinates(x1,y1) of big plants
pixel_plant2 = (19, 29) #pixel coordinates(x1,y1) of small plants
pixel_plant3 = (17, 27) #pixel coordinates(x1,y1) of 3 in row plants

def jump():
play = True
while play:
        box = ImageGrab.grab(box_area1)
        p1 = box.getpixel(pixel_plant1)
        p2 = box.getpixel(pixel_plant2)
        p3 = box.getpixel(pixel_plant3)
        if p1 == (83, 83, 83) or p2 == (83, 83, 83) or p3 == (83, 83, 83):
    except Exception as e:
        play = False

Step 6:

Box coordinates goes to the box_area1 variable. and plant coordinates goes to the pixel_plant variables.
jump function uses pyautogui.keyDown('space') to virtually press down the Space key. pyautogui.keyUp('space') to relese the Space key. And finally print Jump so we know its our code who made the dino jump.

Step 7:

We make our main while loop and put all our game code inside it.
box = ImageGrab.grab(box_area1) takes a screenshot of the box we defined. p1 = box.getpixel(pixel_plant1) gives the value of the pixel same goes for p2 and p3.
With our if statement we check for the pixel value (83, 83, 83) because this is the value of the plant pixel colour.
Whenever any of the three p1,p2 and p3 has the value (83, 83, 83) we call our jump() function.

Step 78

Run our code by browsing to the folder which contains our code file and open a commant prompt there windows user can use the shortcut Shift + Left Click and select open a command prompt here. Then use python to run it.

Result: small sample

result of play chrome dino game using pyautogui

One test resulted a score over 600 using the same code.

Hope you guys! found it interesting and do share the post also subscribe to Tech Conductor on one click below or throw a thumb on the floating bell icon. This will help you get all the blog post updates via web push notifications.

Thank you for Subscribing! We will make sure you never miss a thing.


Om Prakash Mahato

Full stack developer, author, founder of TechConductor.


  • commenter image
    Please Like and share, if found helpful :-)
    • commenter image
      pyautogui is awesome!

Leave a comment:

Name is ok.
Comment is ok.
Email is ok.
Website is ok.