In this project, we’ll see how to make the famous Running T-Rex Dinosaur Game that we all enjoy on Google Chrome when there is no Internet Connection available. Here, we’ll learn how to build T-rex Dinosaur Game With Arduino on Tinkercad Simulation Software.
We’ve seen before how powerful Tinkercad Simulation can be and used it to make many projects.
Supplies
Hardware Components:
Arduino UNO – Official Arduino UNO Board from Amazon or any compatible Arduino Board
16×2 LCD
1K ohm Resistor
2 Pushbuttons
Some Wires
Circuit Connection
We connect Arduino UNO to the LCD using 4 Pins of Data and the control Pins R/W, E and RS.
We connect LCD power pins VCC and GND to Arduino 5V and GND.
The contrast V0 pin connected to GND.
The Backlight LED +ve connected to 5v and the -ve connected to GND through 1K ohm Resistor.
We use pushbuttons to control the game.
Arduino Code
Here we start by including the LCD library and the LCD definition and the bitmap array for the dinosaur character.
Then we define the character for the tree.
The definition for buttons Enter and Select.
Constants
String for alphabet to write your name.
Variables
Start by initializing the LCD. Then create special characters the dinosaur character and the tree character.
The main loop function, start by clearing the LCD.
The handle menu function
reads from the two push buttons. The pin mode as internal pull up using the internal pull up resistor.
Menus and Functions
The menu Start and Score.
Then we print the menu and we read the buttons using digital read function.
The print score function prints the score on the LCD.
The start game function
Now, we need to program Arduino UNO board inside Tinkercad environment with this code to start executing it.
On the upper right Code tab, click the dropdown menu and select the text only. When you’re prompted to the caution that you are going to deleted all the Blocks , you click Continue.
Then you paste the above code in the software sheet.
Now, you’re ready to start the circuit execution.
// AeroArduino.com
/*
This is a working Trex Dinosaur with buttons Select and Enter on pins 8 and 9
It has a score registers to save
Video on YouTube:
https://youtu.be/8tQP66L5U7Y
*/
#include <LiquidCrystal.h>
// Defines the pins that will be used for the display
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//bitmap array for the dino character
byte dino [8]
{ B00000,
B00111,
B00101,
B10111,
B11100,
B11111,
B01101,
B01100,
};
//character for the tree
byte tree [8]
{
B00011,
B11011,
B11011,
B11011,
B11011,
B11111,
B01110,
B01110
};
const int BUTTON_ENTER = 8;
const int BUTTON_SELECT = 9;
const int MENU_SIZE = 2;
const int LCD_COLUMN = 16;
const int TREE_CHAR = 6;
const int DINO_CHAR = 7;
const String ALPHABET[26] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
boolean isPlaying = false;
boolean isShowScore = false;
boolean isDinoOnGround = true;
int currentIndexMenu = 0;
int score = 0;
int scoreListSize = 0;
String scoreList[20];
void setup() {
lcd.begin(16, 2);
lcd.createChar(DINO_CHAR, dino);
lcd.createChar(TREE_CHAR, tree);
Serial.begin(9600);
pinMode(BUTTON_ENTER, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
}
void loop() {
lcd.clear();
handleMenu();
delay(300);
}
void handleMenu() {
String menu[MENU_SIZE] = { "START", "SCORE" };
for (int i = 0; i < MENU_SIZE; i++) {
if (i == currentIndexMenu) {
lcd.setCursor(0, i);
lcd.print("-> ");
}
lcd.setCursor(3, i);
lcd.print(menu[i]);
}
if (digitalRead(BUTTON_SELECT) == LOW) {
currentIndexMenu = currentIndexMenu == 0 ? 1 : 0;
}
if (digitalRead(BUTTON_ENTER) == LOW) {
currentIndexMenu == 0 ? startGame() : showScore();
}
}
void showScore () {
isShowScore = true;
delay(200);
int currentIndex = 0;
const int lastIndex = scoreListSize - 1;
printScore(currentIndex, lastIndex);
while (isShowScore) {
if (digitalRead(BUTTON_SELECT) == LOW) {
currentIndex = currentIndex < lastIndex ? currentIndex + 1 : 0;
printScore(currentIndex, lastIndex);
}
if (digitalRead(BUTTON_ENTER) == LOW) {
isShowScore = false;
}
delay(200);
}
}
void printScore(int index, int lastIndex) {
lcd.clear();
if (lastIndex == -1) {
lcd.print("NO SCORE");
}
else {
lcd.print(scoreList[index]);
if (index < lastIndex) {
lcd.setCursor(0, 1);
lcd.print(scoreList[index + 1]);
}
}
}
void startGame () {
isPlaying = true;
while (isPlaying) {
handleGame();
}
}
void handleGame() {
lcd.clear();
int buttonPressedTimes = 0;
// Generate two random distances for the space between the trees
int secondPosition = random(4, 9);
int thirdPosition = random(4, 9);
int firstTreePosition = LCD_COLUMN;
const int columnValueToStopMoveTrees = -(secondPosition + thirdPosition);
// this loop is to make the trees move, this loop waiting until
// all the trees moved
for (; firstTreePosition >= columnValueToStopMoveTrees; firstTreePosition--) {
lcd.setCursor(13, 0);
lcd.print(score);
defineDinoPosition();
int secondTreePosition = firstTreePosition + secondPosition;
int thirdTreePosition = secondTreePosition + thirdPosition;
showTree(firstTreePosition);
showTree(secondTreePosition);
showTree(thirdTreePosition);
if (isDinoOnGround) {
if (firstTreePosition == 1 || secondTreePosition == 1 || thirdTreePosition == 1) {
handleGameOver();
delay(5000);
break;
}
buttonPressedTimes = 0;
} else {
if (buttonPressedTimes > 3) {
score -= 3;
}
buttonPressedTimes++;
}
score++;
delay(500);
}
}
void handleGameOver () {
lcd.clear();
lcd.print("GAME OVER");
lcd.setCursor(0, 1);
lcd.print("SCORE: ");
lcd.print(score);
delay(2000);
saveScore();
}
void saveScore () {
lcd.clear();
String nick = "";
int nameSize = 0;
int alphabetCurrentIndex = 0;
lcd.print("TYPE YOUR NAME");
while (nameSize != 3) {
lcd.setCursor(nameSize, 1);
lcd.print(ALPHABET[alphabetCurrentIndex]);
if (digitalRead(BUTTON_SELECT) == LOW) {
alphabetCurrentIndex = alphabetCurrentIndex != 25 ? alphabetCurrentIndex + 1 : 0;
}
if (digitalRead(BUTTON_ENTER) == LOW) {
nick += ALPHABET[alphabetCurrentIndex];
nameSize++;
alphabetCurrentIndex = 0;
}
delay(300);
}
scoreList[scoreListSize] = nick + " " + score;
scoreListSize++;
isPlaying = false;
score = 0;
}
void showTree (int position) {
lcd.setCursor(position, 1);
lcd.write(TREE_CHAR);
// clean the previous position
lcd.setCursor(position + 1, 1);
lcd.print(" ");
}
void defineDinoPosition () {
int buttonState = digitalRead(BUTTON_ENTER);
buttonState == HIGH ? putDinoOnGround() : putDinoOnAir();
}
void putDinoOnGround () {
lcd.setCursor(1, 1);
lcd.write(DINO_CHAR);
lcd.setCursor(1, 0);
lcd.print(" ");
isDinoOnGround = true;
}
void putDinoOnAir () {
lcd.setCursor(1, 0);
lcd.write(DINO_CHAR);
lcd.setCursor(1, 1);
lcd.print(" ");
isDinoOnGround = false;
}
How to Make #Arduino T-Rex Dinosaur #game With #Tinkercad Arduino #Simulation https://t.co/3xcUMgOKu6 via @YouTube
— Ahmed Ebeed – AeroArduino (@ahmedebeed555) May 24, 2023
Tinkercad Simulation
Now, click the Start Simulation button to start the simulation.
You see that Arduino is connected to the USB port on the screen and it indicates that it’s powered and started software execution.
If all the steps are correctly followed, the LCD is illuminated and the game is started.
Congratulations, you’ve just built Arduino circuit that plays tones of your favorite Animation on Tinkercad.
Then you can start the game simulation in action and you can play that game as in the real circuit.
Now you can build the circuit using real components.
Here you can find the Tinkercad Simulation where you can see the circuit connection and code. You can also edit all the connection and the code.
Project on Tinkercad
https://www.tinkercad.com/things/5PvUeEYt8Oe
Videos
Here are videos of the project on my YouTube Channel AeroArduino. Don’t forget to Subscribe and Hit that Notification Button to get new videos.