QUICK TIP - Create a play bot T-Rex in Chrome – Hardcode version

0 12,431
img_5f12ed4547cb3 | A&H Business Technology
Related posts

As you probably know, Google Chrome browser has a small game built in so you can kill time whenever you have disconnection.

If you visit a website with Chrome and no Internet, you’ll see an image of a pretty cute dinosaur showing up. The way to play the game is very simple: press “Space” / “Up” to jump to avoid obstacles, cactus or flying dinosaurs.

A small Chrome game that helps you kill time when you lose your life (source: https://mathewsachin.github.io)

I will write a series on how to create a bot to play this game. Currently I have solved the problem with 2 methods:

  • Hardcode
  • Using Genetic Algorithm in a neural network to solve the problem

If I have time, I will find ways to use reinforcement learning as the third way to solve this problem. For stability, we will play on another site instead of playing on Chrome which has disconnected.

I choose this page: http://www.trex-game.skipser.com/ In this article, I will talk about how to solve by hardcode method.

Hardcode method

The idea of ​​this method is very simple: we will constantly check the front of the dinosaur, if a strange object appears, the dinosaur will jump. This is basically how we played this game in the beginning.

Initial setup

Firstly, we need to import the necessary packages. We will use it mssto take screenshots, pyautoguioutput it, and some other familiar packages.

We will define some constants to use later:

We create functions to play the game, including: 1 class Cordinatesto store the location of the object, and 2 functions to restart the game as well as press the Up key.

Identify the object

We create a function get_cactus_box_valueto see if there is cactus_boxan obstacle in the area ahead of the dinosaur (which we call here ). We will convert the regional value cactus_boxto gray and save the value, so that it can be compared to the value BLANK_BOXof that area when there are no obstructions.

Please note that the values ​​here will vary depending on the settings of the machine, if you want to run on your computer, you may need to fix a bit.

At this point, we have basically completed the code for 1 game.

However, we should do one more function check_gameover, so that when there is a gameover, we will detect and call the function restart_gameto play again.

After we have prepared all the above components, we will create a function mainto combine all the above components.

System settings

The environment of the system is Ubuntu 16.04, using chrome.

  • Step 1: turn on the browser and access: http://www.trex-game.skipser.com/, start the game.
  • Step 2: Split the screen by typing Ctrl + Super + Left/Right
  • Step 3: run the file chrome_trex_api.py
  • Step 4: record the clip and bring it to show off: v


Okay here we go, and this is our result. Pretty good, right?

Run the file \ m /

In the following articles I will introduce Genetic Algorithms and how to apply it to create bots to learn how to play this game.

TinyURL for this post: https://tinyurl.com/y4xelwja

Sorry, The Comment Form Is Temporarily Closed At This Time
You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More