Automate Chrome Dino Game using Python | pyautogui and PIL

Ayush Gemini
Analytics Vidhya
Published in
5 min readMay 13, 2020

Hey folks, today we are going to do a very interesting thing. we are going to automate the Google Chrome Dino game using Python.

Chrome-dino-game-automate-python

What is Google Chrome Dino Game?

Google Chrome Dino Game is the free offline/online game in which you can see in your chrome browser when there is no internet connection available.

How many characters in this game?

There are mostly 3 characters here.

  1. Dino
  2. Cactus
  3. Birds

How to Play the Game?

There is two way to play this game.

  1. Offline
  2. Online

Offline:

when you don’t have internet connectivity in your laptop you will redirect to this game and by click on the enter button game will start. In this game on a random period cactus and birds comes near to dino and dino need to save itself from this cactus and birds collision by using jump or duck. And as per your performance, you will get your score.

Online:

If you want to play it online then you can visit https://chromedino.com/ and start the game the same as you do in Offline.

How to automate the chrome dino game using python

I am assuming you have already installed Python3 in your system. If not kindly download it from here. After that for automation the process we will need two python package.

  1. pyautogui
  2. PIL

So first we need to install pyautogui and PIL into our system by using given command.

pip3 install pyautogui
pip3 install pillow

Okay, After doing this we will be having pyautogui and pillow. pyautogui will help us to automatically press the keys of a laptop.

what steps I am thinking to do this?

So what steps we are going to take to implement this is here.

  1. first of all, I need to capture the screenshot of the chrome dino game screen on greyscale.(Black & white, because image processing will be good on greyscale).
  2. Then I just want to add some kind of rectangle on this image for cactus so that when I get those pixels of the rectangle near to dino the accordingly I can write the logic to jump dino(by pressing up key).
  3. The same goes for birds, will make a rectangle but little lighter than a cactus rectangle. (To differentiate both rectangles) and when I found this rectangle is near to dino then I will press down key to duck the dino.
  4. That’s it, after this, you will see your chrome dino game is automated using python.

Let’s get started with code now to implement our algorithm and see how it works.

First, we need to create a file lets say game.py in my case. In this file first you need to import two packages and time.

import pyautogui 
from PIL import Image, ImageGrab
import time

After that, we should make a function that can press a key for you.

def click(key):
pyautogui.keyDown(key)
return

Now in main, you need to start the game by pressing up key

if __name__ == "__main__":
time.sleep(5)
click('up')

as you can see we are taking a 5-sec delay so that after running the program we can get some time to go on the chrome browser page and after that, we just calling the click function to press the up key, and then our game will start.

Now we need to take the screenshot constantly on greyscale. for that, we need to use

while True:  # for infinity loop
# capture image in black & white format
image = ImageGrab.grab().convert('L')
data = image.load()

After that, we need to draw a rectangle for cactus.

# Draw the rectangle for cactus
for i in range(530, 610):
for j in range(130, 160):
data[i, j] = 0
image.show()
break

This code will draw a rectangle on the captured image and returned it to you. But you need to play around with x,y coordinates in for loop as per your desktop or laptop. This coordinate is working for my laptop screen size maybe it will differ for you. so your rectangle should look like this.

Black Rectangle in image for cactus

Now, You need to make the rectangle for birds.

# Draw the rectangle for birds
for i in range(530, 560):
for j in range(100, 125):
data[i, j] = 171

Your screen should look like this as you can see a grey rectangle is for the bird.

Grey Rectangle for Bird

So now once you are done with the position of the rectangles in the image, the next task is to check the collision with the dino. if you observer carefully then in this game dino is not moving ahead while cactus and birds are coming near to dino. So let’s see how we will do this. we will make the iscollision function inside that we will check the collision for both rectangle and dino.

def isCollision(data):
# Check colison for birds
for i in range(530,560):
for j in range(80, 127):
if data[i, j] < 171:
click("down")
return
# Check colison for cactus
for i in range(530, 620):
for j in range(130, 160):
if data[i, j] < 100:
click("up")
return
return

Now on the main function, inside while loop we will call this function and comment the code written for rectangle and comment the break as well. because now we don’t need them.

After doing all of the above steps, your file will look like this

we are ready now to run this program by using given command on terminal.

python3 game.py

then move to the chrome browser game you will see your dino is running automatically.

You can download the source code from Github.

Happy coding.

See you in the next story. Stay tuned!

You can connect with me on LinkedIn & Github.

--

--

Ayush Gemini
Analytics Vidhya

I’m a core Android developer with complimenting skills as a web developer from India.