Google Dinosour Game Automation
For day 94 of this course, the goal was to automate the Google Dinosaur game. Previous projects in this course used Selenium for web automation, however, to complete this project, PyAutoGui and Pillow were required to complete it.
Project Goals & Steps to Complete
The overall goal for this project was to automate the Google Dinosaur game. This project was smaller than others I have completed for the portfolio section, but it still required some tinkering to get a desirable result.
The general steps for the project as as follows:
- Open Chrome and navigate to the game using PyAutoGui.
- Start the game using PyAutoGui.
- Automate the game using PyAutoGui and Pillow.
Additionally, I coded the loop to end once the “Game Over” prompt is displayed. Alternatively, the game could be replayed if a jump command is issued when the game ends. However, I chose not to go that route to avoid an infinite loop.
Project Challenges
Originally, I thought this project could easily be completed using only PyAutoGui since I noticed that it had the capability to detect a pixel’s RGB colors. Using PyAutoGui, I used the pixel() method to get the color of the pixel at a given location. My initial thought was that when a cactus object crosses the given point, the detected color change would trigger the jump function.
Unfortunately, only using pixel() did not work successfully. The dinosaur game speeds by too quickly for the program to pick up an object touching the given point.
To overcome this problem, I ended up using Pillow to draw out a box and detect when an object crosses the defined rectangle. If the color change is caught in the rectangle, the jump function was triggered using PyAutoGui’s press() method. Press() was also used to detect birds and make the dinosaur duck when they entered the defined space.
A rough drawing of where those rectangles were placed to detect objects is depicted on the screenshot under the project’s overview section.
Project Takeaways
I thought this project would be easy to complete using only PyAutoGui, however, after a bit of playing around, I found out that I would have to incorporate the use of the Pillow library.
Ultimately, I was able to successfully get the project up and running, however, it is not perfect. I have not found the ideal spot to place the object boxes, so when the game speeds up, sometimes the dinosaur does not jump in time to avoid a cactus. Additionally, there are some instances where the jump function isn’t triggered regardless of speed.
Overall, the program performs the functions that are required to automate the Google Dinosaur game. With a bit more tweaking, I am sure the timing for the jumps could be improved!