Building bot for playing google chrome dinosaur game in Python
As you might already know, Google Chrome has a dinosaur game when you are offline. In this article, we are going to look at building a bot to play this dinosaur game. We are going to implement it in Python programming language and also use the concepts of image processing.
In case you don’t want to go offline for the game, use this website instead: http://www.trex-game.skipser.com/
Bot to play the google chrome dinosaur game
Let’s import the required libraries, but make sure you have them installed on your system.
from PIL import ImageOps import pyscreenshot as ImageGrab import pyautogui import time import numpy as np
We set up a class to define the coordinates from your screen of the restart button and the top right corner of the dinosaur’s position. To get these positions, adjust your browser in half of the screen.
Take a snapshot of the screen in the above-stated configuration. Use paint or any other related software to get the coordinates of both the objects mentioned above. In our case, these coordinates are replay = (530, 516) and dino = (195, 522).
class Cordinates(): # coordinates of replay button replay = (530, 516) # coordinates of top-right corner of dinosaur dino = (195, 522)
We define function restartGame() to click the restart button using its coordinates.
def restartGame(): #automating the replay button pyautogui.click(Cordinates.replay)
We define function pressSpace() to make the dinosaur jump using the space key.
def pressSpace(): pyautogui.keyDown("space") # a small time sleep for space # to be easily recognized by the game time.sleep(0.05) print("Jump") pyautogui.keyUp("space")
Let’s check if both the functions work properly:
restartGame() time.sleep(1) pressSpace()
Once you see if it works appropriately, comment out the last three lines. Furthermore, we have to guess about the area where we want our dino to jump if there is any tree. For that, note the pixel values of the top left and bottom right corner of the box we want. So, the box is where the dino is going to jump if any tree appears.
def imgBox(): box = (Cordinates.dino+60, Cordinates.dino, Cordinates.dino+160, Cordinates.dino+20) image = ImageGrab.grab(box) grayImage = ImageOps.grayscale(image) a = np.array(grayImage.getcolors()) print(a.sum()) return(a.sum())
Analyzing grey-scale images is better than colored images, so we convert our grabbed image to a grey-scale using ImageOps. Now, we convert the image to an array and sum it up using the NumPy library. After that, we print out the sum of the array elements.
while True: imgBox()
Running the program now would get us the sum at every iteration. After that, note down this value when there are no obstacles in the picture and comment out the above code snippet. Here, the value is 4247. We define the main function to run the game.
def main(): restartGame() while True: if(imgBox()!= 2247): pressSpace() time.sleep(0.1) main()
After this, you should be able to automate the game. Following are the constraints where the code won’t work:
- When it gets night in the offline mode.
- When the dino has to duck from the bird.
But it’s pretty much pleasing to see the dino run on its own. Here’s our version of the bot playing the dinosaur game