Chrome Dino Game Automation in Python



# Not For Copy paste coders



Code Link

So, just if the case you are not aware of the chrome Dino game, let me tell you it is a chrome game. You must have seen this game once in your lifetime. If not, then may I ask you a question? Answer in the comments if you want, else ignore. I would like to ask "Are you from Mars?" No, it's not a silly question. It's very important. I want to know the answer. I am really serious. The Dinosaur Game is a built-in browser game in the Google Chrome web browser. The player guides a pixelated Tyrannosaurus rex across a side-scrolling landscape, avoiding obstacles to achieve a higher score. Search chrome://dino on Chrome browser and you will see the game.

Well, I think I am talking too much let's get straight to the topic...

Alert: Don't panic while running the code, just go to the python shell or the terminal (cmd/bash or anything on which the code is currently running) and press Ctrl+C to stop the running code.

So, for the project, the python modules/libraries I used in this project are pillow and pyautogui. Well, I think that pyautogui is one among few important libraries for automation of anything in python. Pyautogui allows you to automate the keys of your keyboard and even mouse. Just for an example, if you are going to subscribe this blog, then you can use Pyautogui to take your mouse and click on it. Isn't it awesome that this library makes that so easy. Also the other library I used is for image processing. Pillow is a image processing library.

Well, I also used time module to give a variation in hte project.

Let's understand the logic of the code to know the use of Pillow. The logic of the code is that the system takes few screenshots and then, just for easy processing, with the help of Pillow module, we convert the screenshots to grayscale image and then process for a particular color pixel in a space.

Basically, I have made a rectangular region with the coordinates of the screen. Now lwt's suppose, a dark black pixel comes in that rectangular region. The code will detect that and suddenly, with the help of pyautogui, we press the "up" arrow key. That's it. Now the point where the code becomes complicated is that, it depends on your systems processing power on how much time it would take after getting the input, processing it and return the output (hit a key). So, may be on your system, this code will not work properly.

The other case is, due to some variations in device resolution and pixel sizes of different devices, the data for the coordinates of square region may vary on different devices. So, there are a lot of chances that *this code is not for Copy paste coders*

Well, I think I am talking too complicated. This blog is feeling like a professional coder. Isn't it? So, that's all for this project.

If you want more like this, do let me know in the comments below... And Subscribe to this blog because I would be sharing more such projects of mine in future on this blog. Also, if you didn't see, check out my last project of a Music Player web app made in JavaScript. Click here

Then Bye Bye!
Thanks for reading... :)
See you! (If you subscribe)

2 Comments

  1. The code could have been made more short by using loop instead of directly printing 1... 2... 3... Btw it's alright!

    ReplyDelete
  2. You can simply type Runner.instance_.gameOver = () => {} in the console and now relax. Your dino will never die

    ReplyDelete