Projects

Tricking T-Rex

Arduino-Based Chrome Dino Game

DIYODE Magazine

Issue 33, April 2020

This article includes additional downloadable resources.
Please log in to access.

Log in

Here’s a great way to hack the Chrome Dino game when your Internet goes down (or when you’re self-isolating).

If you use Chrome as your favorite web browser, you have no doubt come across the Dino (T-Rex) game to keep you entertained when there is no internet.

With most of the world in self-isolation at the moment (shakes fist at COVID-19), and makers looking for things to keep them occupied, we thought this project would be fun.

All you need is an Arduino Uno, a small servo, Light Dependent Resistor (LDR), and a 10k resistor to get started.

THE BROAD OVERVIEW

The Chrome Dino game appears when your Intenet connection goes down (You can also access it by entering chrome://dino into your Chrome browser).

The game requires you to press the space bar on your keyboard for the dinosaur to jump over the moving cactus and other objects. The more objects you jump over, the higher your score.

This project fully automates the process by detecting the onscreen obstacle using an LDR, and then triggering the keyboard’s space bar using a servo. Genius!

HOW IT WORKS

The circuit is quite straightforward. We detect the black moving obstacle on the computer’s screen by attaching an LDR using sticky tape. The LDR and a 10k resistor form a voltage divider, which sends the signal to the Analog pin 0 of the Arduino Uno board.

To activate the keyboard’s space bar, we attach a standard SG90 servo using Blue-Tac® or similar. The servo is powered from the Uno board, and is activated from the signal coming from the Uno board’s Digital Pin 2.

THE BUILD:

Fritzing diagram
We have provided a Fritzing diagram for you to follow to make all of the connections. The LDR connects to Analog pin 0 and the servo’s signal wire to Digital Pin 2.

With your wiring complete, you can connect the LDR and servo to your computer’s screen and keyboard.

MOUNTING THE LDR

You will need strong enough tape to hold the LDR to your screen, but not too strong that it ruins your screen. To find the position on the screen, you will need to start the game. Open your Chrome browser and type chrome://dino into the browser. Press the space key on your keyboard to start the game. The screen will go full screen. Tape the LDR onto the area just in front of the T-Rex so it can detect the moving objects.

MOUNTING THE SERVO

We used Blu-Tac® to hold our servo in place but you could also use adhesive tape. Keep in mind that the servo might lift away from the keyboard if you don’t hold it down well enough. It may be trial and error to get it right.

Parts Required:Jaycar
1 x Arduino Uno or Compatible BoardXC4410
1 x Small ServoYM2758
1 x Light Dependent Resistor (LDR)RD3480
1 x 10K Resistor*RR0596
*Quantity required, may only be sold in packs. Breadboard and prototyping hardware also required.

THE CODE

The code shown here is available for download, once you have logged into our website. This also saves you from typing it out.

You need to upload this code to your Arduino Uno.

Note: If this is your first time using the Arduino IDE, you can visit our Setting Up The Arduino IDE article from Issue 17.

#include <Servo.h>
Servo myservo;
int sensorPin = A0;
int sensorValue = 0;
int val;
void setup() {
  // put your setup code here, to run once:
  myservo.attach(2);
  Serial.begin(9600);
  myservo.write(80);
}
void loop() {
  // put your main code here, to run repeatedly:
  sensorValue = (analogRead(sensorPin));
  if (sensorValue < 650) {
    // cactus 
    val = 100;
  }
  else {
    //no cactus
    val =  80;
    myservo.write(val);
    delay(50);
  }
  Serial.println(sensorValue); //debug
  myservo.write(val);
  //delay(15);
}

TESTING

With the LDR and servo mounted, and the code uploaded to your Uno, it’s time to play.

Start the Dino game running by pressing the space key. Your servo should trigger the keyboard’s space key when the object is detected. If you find that the dinosaur doesn’t jump, then you may want to position the LDR so that it is better aligned with the cactus.

If it still isn’t jumping, you will need to check the connections to the Arduino board.

We wrote the code based on our testing environment. i.e. our laptop screen’s brightness, the resolution of the display, and even the display’s refresh rate. Screen tearing could potentially cause differences in the operation of the code. We would also not recommend dark mode for this, as the contrast ratio is dramatically reduced.

If everything is connected up correctly and the servo still isn’t triggering, you need to check that the threshold values set in the code are in the same range as your circumstances. To do this, run the game until the game ends and the T-rex is stuck in a cactus.

With the program running on the Arduino, open your Arduino IDE’s Serial Monitor (Control + Shift + M on a PC). This will display the sensor value from the LDR.

Mounting the LDR
Move the LDR over the cactus and take note of the sensor’s value. This value represents the new trigger value.

Likewise, move the sensor over the white area of the screen and take note of the value shown on the serial monitor.

For our prototype, the white empty space had a value of between 690 and 750, while the cactus returned a value between 600 and 640. Thus, we decided the best trigger value for our setup was something less than 650 (gives a little room for an outlier). You can choose and add your value by changing the 650 value in this line to whatever your trigger should be.

 if (sensorValue < 650) {

You can now disable the serial print function, which will speed up the program by commenting out the print line using // changing the line from:

Serial.println(sensorValue);

To:

//Serial.println(sensorValue);

Another potential issue you may encounter is the servo not moving enough or moving too much for your situation. To rectify this, you need to adjust the val value for each possible situation. i.e. cactus and no cactus. For the cactus situation, you need the servo to travel to a position where the space bar is actuated. The value you enter is in degrees. For our prototype, the cactus position was 100° and our no cactus was 80°.

You need these values to be as close as possible, so you don’t waste time with the servo moving too far. In both instances, you need to modify the line to match your specific situation:

val =  80;

The final issue you may face relates to timing. In some situations, the servo will trigger too early or too late. The easiest way to rectify this is to adjust the position of the LDR. If the servo is reacting to slowly, move the LDR closer toward the T-rex. If it’s jumping too soon, move the LDR further from the Dino.

Happy gaming!

WHERE TO FROM HERE?

You'll soon find that this Dino game has a lot more to it than pressing the space bar to jump over cactus. To dodge the flying dinosaurs (Pteranodon?) you'll need a servo to press the Down key on your keyboard. This is beyond the scope of this project, but it's a good challenge for you to take this fun project to the next level. (Tag us on social media if you build it @diyodemag)

Editors Note:

We tried to find the original source for this project idea but found it very difficult with the numerous reposts online without crediting the source. If you are the true brains behind this clever Arduino-based game, then please get in touch with us.

An Internet search finds various designs, including one from Rafi Rasheed on the Arduino Project Hub (rafitc), however, this is more recent than the ones we’ve seen doing the rounds in social media.

Some recent ones we've seen on Instagram include:

@rohan__barnwal

@elektrobilgim

@abdullahbirol

@desi_guys__

@elektrobilgim