steve 9 January 2017 wordpress, business, economy, design

Learn how to AutoMate Chrome Dino

Blog image

To play this game you have to search chrome://dino to play this game offline and online.

So to automate dino you need to install python3 in your system and then you need to install some package:

  • pyautogui
  • PIL

So firest we need to install pyautogui and PIL into our system by using give command

pip install pyautogui
pip install pillow

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

  1. First of all, we need to capture the screenshot of the chrome dino game screen on greyscale.(Black and White, because image processing will be good on greyscale).
  2. The we just want to add some kind of rectange on this image for cactus so that when we get those pixels of the rectangle near to dino the accordingly I can write the logic to jump dino
  3. The same goes for birds, will make a rectangle but little lighter than a cactus rectangle. (to differentiate both rectangles) and when we found this rectangle is near to dino then we 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 start coding and implement our algorithm and see how it works.

First, we need to create a file lets say dino_1.py in my case. in this file first you need to import two package 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 clik(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')
                    

Now we nned 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 you desktop or laptop. this coordinate is working for my pc screed size maybe it will differ for you. so your rectangle should look like this.

dino

Now, you need to make the rectangle for birds

# Draw the rectangle for birds
        for i ine 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.

dino_2

So now once you are done with the position of the rectangle 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 iscolision fuction inside that we will check the collision for both rectangle and Dino.

def isColision(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, you file will look like this


import pyautogui # pip install pyautogui
from PIL import Image, ImageGrab # pip install pillow
# from numpy import asarray
import time

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

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

if __name__ == "__main__":
    time.sleep(5)
    click('up') 
    
    while True:
        image = ImageGrab.grab().convert('L')  
        data = image.load()
        isCollision(data)
        
        # # Draw the rectangle for cactus
        # for i in range(530, 610):
        #     for j in range(130, 160):
        #          data[i, j] = 0
        
        # # # Draw the rectangle for birds
        # for i in range(530, 560):
        #     for j in range(100, 125):
        #         data[i, j] = 171

        # image.show()
        # break