Project tutorial
The Chrome Dino Game on an LCD Shield

The Chrome Dino Game on an LCD Shield © GPL3+

The Chrome dinosaur game that is now on an Arduino LCD shield!

  • 19 views
  • 0 comments
  • 0 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
Any Arduino that follows the Arduino 1.0 pinout should work
×1
Kuman LCD Shield
If you are using a different shield, do not forget to change the pins used by the lcd and the button definitions!
×1
Knowledge on operating your computer and the Arduino IDE
×1

Necessary tools and machines

Hands
Computer

Apps and online services

Ide web
Arduino IDE
Do I really need to mention this though?

About this project

Somehow, I am very bad at coming up with ideas.

It's just like that.

But anyway, I was browsing the Project Hub and I found this project. It was a great project!

But I still made my own version in Arduino C because 1. I wanted to make something and 2. I don't want to download another thing on my computer.

(I still would highly recommend checking it out! Link is here!)

Here are some pictures:

Features

  • Splash screen that waits until you are ready.
  • When you are holding the SELECT button to jump, score is stopped from incrementing so that you can't just hold down the button the entire time.
  • Custom characters for the dinosaur, cacti, birds, and blocks.
  • Death screen.
  • High score saved in internal EEPROM.
  • Ability to erase high score using "secret" code. (You can read the code and figure out the code!)

How to Play

  • Press SELECT to start.
  • Pres SELECT to jump.
  • When you die, reset using the RESET button.
  • Repeat until you are sick of it.
  • To erase high score, click DOWN when you are at the splash screen. Then increment/decrement the code using the UP and DOWN buttons respectively to change the code. Default code is 123. Press SELECT to enter. Hi score should be erased.

How the Code Works

First, I include all of the necessary libraries and the bitmaps. Then, I define an object called lcd. I define a bunch of variables and #defines. Then I write an array as a buffer for holding what goes on the screen. Then I define more variables.

Then setup starts. I do all of the necessary stuff, like creating the characters and showing the splash screen.

We have loop next. I define some timing variables, like in blinkwithoutdelay. I do the check if the time has passed long enough. Then, I use the random function to decide when and what to put in the buffer. After that, I shift all of the elements in the buffer down one using a for loop. Then it checks whether to add the score or not because as I said earlier, when you are jumping you cannot get any points.

Then I draw the barriers next, as you see the blocks bitmap.

Then I check whether the SELECT (jump) button is pressed. It then modifies the buffer to show whether the dinosaur is jumping or not.

The next code checks whether dinosaur has been airborne long enough so that the dinosaur can come down.

This is where the code checks whether there is an obstacle in the way.

If the dinosaur didn't crash, then the Arduino updates the lcd and prints the score. Else, it shows the death screen.

Then the final code is not required-it blinks the LED. If you are having problems with timing, then you could remove/comment the blink code out.

Last, the Arduino build process includes the Functions tab at the end of the sketch for us. This includes lots of functions that are used throughout the code.

Code

Chrome_Dino_Game_on_LCDC/C++
Paste into editor as main sketch
#include <LiquidCrystal.h>
#include <EEPROM.h>

#include "bitmaps.h"

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int adc_key_in  = 0;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

byte runnerArea[16] {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32};
byte jump = 32;

int score = 0;
bool freeze_score = 0;

byte correct_code = 123;

unsigned long previousMillis = 0;
unsigned long previousMillisLED = 0;
unsigned long jumpTime = 0;
const int jumpLength = 500;

#define checkSafe runnerArea[1] == 32 || runnerArea[1] == 0

const byte chance_of_ob = 15;
int speedOfScroller = 300;

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);

  lcd.createChar(0, dino);
  lcd.createChar(1, cacti);
  lcd.createChar(2, bird);
  lcd.createChar(3, block);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(13, OUTPUT);
  randomSeed(A1);
  lcd.clear();
  showSplashScreen(1000, true);
}

void loop() {
  unsigned long currentMillis = millis();
  unsigned long currentMillisOb = millis();
  if (currentMillisOb - previousMillis >= speedOfScroller) {
    previousMillis = currentMillisOb;
    if (random(chance_of_ob) == 0) {
      runnerArea[15] = 1;
    } else if (random(chance_of_ob) == 1) {
      runnerArea[15] = 2;
    } else {
      runnerArea[15] = 32;
    }
    for (int i = 0; i <= 15; i++) {
      runnerArea[i] = runnerArea[i + 1];
    }
    if (freeze_score == 0) {
      score++;
    }
  }
  drawBarrier();

  if (read_LCD_buttons() == btnSELECT) {
    // runnerArea[1] = 32;
    if (runnerArea[1] != 32 && (runnerArea[1] != 1 || runnerArea[1] != 2)) {
      runnerArea[1] = 32;
    }
    jump = 0;
    freeze_score = 1;
    jumpTime = millis();
  }
  if (millis() - jumpTime >= jumpLength) {
    if (checkSafe) {
      runnerArea[1] = 0;
      jump = 32;
      freeze_score = 0;
    } else {
      showCrashScreen();
    }
  }
  updateLcd();
  printScore();

  if (millis() - previousMillisLED >= 500) {
    previousMillisLED = currentMillis;
    digitalWrite(13, !digitalRead(13));
  }
}
FunctionsC/C++
Upload as a tab named Functions
// Functions for main.cpp

int read_LCD_buttons() {
  adc_key_in = analogRead(0);

  if (adc_key_in > 1000) return btnNONE;

  if (adc_key_in < 10)  return btnRIGHT;
  if (adc_key_in < 110)  return btnUP;
  if (adc_key_in < 300)  return btnDOWN;
  if (adc_key_in < 450)  return btnLEFT;
  if (adc_key_in < 700)  return btnSELECT;

  return btnNONE;
}

void updateLcd() {
  for (int i = 0; i <= 15; i++) {
    lcd.setCursor(i, 1);
    lcd.write(runnerArea[i]);
  }
  lcd.setCursor(1, 0);
  lcd.write(jump);
}

void drawBarrier() {
  runnerArea[0] = 3;
  runnerArea[15] = 3;
}

void printScore() {
  lcd.setCursor(4, 0);
  lcd.print("Score: ");
  lcd.setCursor(11, 0);
  lcd.print(score);
}

void showSplashScreen(int delayTime, boolean wait_for_start) {
  lcd.home();
  lcd.print("Chrome Dino Game");
  drawSplashGraphics();
  delay(delayTime);
  if (wait_for_start) {
    while (read_LCD_buttons() != btnSELECT) {
      checkForEEPROMUserInitErase();
    }
  }
  lcd.clear();
}

boolean checkForEEPROMUserInitErase() {
  if (read_LCD_buttons() == btnDOWN) {
    lcd.clear();
    lcd.home();
    lcd.print("Enter Executive");
    lcd.setCursor(0, 1);
    lcd.print("Code: ");
    if (EnterPassword() == 0) {
      lcd.clear();
      lcd.home();
      lcd.print("Chrome Dino Game");
      drawSplashGraphics();
      return 0;
    }
    lcd.home();
    lcd.print("Clearing EEPROM");
    EEPROMWriteInt(0, 0);
    delay(250);
    lcd.home();
    lcd.print("               ");
    lcd.home();
    lcd.print("Done!");
    digitalWrite(13, !digitalRead(13));
    delay(100);
    digitalWrite(13, !digitalRead(13));
    delay(400);
    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.home();
    lcd.print("Chrome Dino Game");
    drawSplashGraphics();
  }
  return 1;
}

bool leave = 0;
byte code = 0;

boolean EnterPassword() {
  lcd.setCursor(0, 1);
  lcd.print("Code: ");
  while (leave == 0) {
    lcd.setCursor(6, 1);
    lcd.print("    ");
    lcd.setCursor(6, 1);
    lcd.print(code);
    if (read_LCD_buttons() == btnUP) {
      code++;
      delay(50);
    } else if (read_LCD_buttons() == btnDOWN) {
      code--;
      delay(50);
    }
    if (read_LCD_buttons() == btnSELECT) {
      if (code == correct_code) {
        leave = 1;
      } else {
        wrong();
      }
    }
    delay(50);
  }
  delay(250);
  lcd.home();
  lcd.clear();
  lcd.print("Access granted!");
  delay(500);
  lcd.clear();
  return 1;
}

void wrong() {
  lcd.setCursor(0, 1);
  lcd.print("Incorrect!");
  code = 0;
  delay(250);
  lcd.setCursor(0, 1);
  lcd.print("Code: ");
}

void drawRandChar() {
  lcd.setCursor(random(3, 15), 1);
  lcd.write(byte(random(1, 3)));
}

void drawSplashGraphics() {
  lcd.setCursor(0, 1);
  lcd.write(byte(3));
  lcd.setCursor(15, 1);
  lcd.write(byte(3));
  lcd.setCursor(1, 1);
  lcd.write(byte(0));
  drawRandChar();
  drawRandChar();
  drawRandChar();
}

void showCrashScreen() {
  lcd.setCursor(4, 1);
  lcd.print("Game Over!");
  delay(2500);
  lcd.setCursor(4, 1);
  lcd.print("Best: ");
  lcd.setCursor(10, 1);
  lcd.print("      ");
  lcd.setCursor(10, 1);
  if (EEPROMReadInt(0) <= score) {
    EEPROMWriteInt(0, score);
  }
  lcd.print(EEPROMReadInt(0));
  while (true) {
    digitalWrite(13, !digitalRead(13));
    delay(500);
  }
}

void EEPROMWriteInt(int address, int value)
{
  byte two = (value & 0xFF);
  byte one = ((value >> 8) & 0xFF);

  EEPROM.update(address, two);
  EEPROM.update(address + 1, one);
}

int EEPROMReadInt(int address)
{
  long two = EEPROM.read(address);
  long one = EEPROM.read(address + 1);

  return ((two << 0) & 0xFFFFFF) + ((one << 8) & 0xFFFFFFFF);
}
bitmaps.hC Header File
Upload as a seperate tab named bitmaps.h
// Bitmaps-Do not modify

byte dino[8] = {
  0b00000,
  0b00111,
  0b00111,
  0b10110,
  0b11111,
  0b01010,
  0b01010,
  0b00000
};

byte cacti[8] = {
  0b00100,
  0b00101,
  0b10101,
  0b10101,
  0b10111,
  0b11100,
  0b00100,
  0b00000
};

byte bird[8] = {
  0b00000,
  0b00100,
  0b01100,
  0b11110,
  0b00111,
  0b00110,
  0b00100,
  0b00000
};

byte block[8] = {
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};

Schematics

Schematic
It is a bare Arduino. Pretend there is a LCD Shield attached to the Arduino
bare_arduino_zKLz4XfR46.fzz

Comments

Similar projects you might like

Google Chrome Dinosaur Game on 16x2 LCD Shield

Project showcase by brzi

  • 4,034 views
  • 4 comments
  • 28 respects

Arduino Game By LCD

Project tutorial by Mohammed Magdy

  • 36,048 views
  • 34 comments
  • 127 respects

LCD Game

Project showcase by Iron_SalsaStudio

  • 7,331 views
  • 8 comments
  • 14 respects

Arduino LCD Video Game

Project tutorial by xXarduino_11Xx

  • 1,838 views
  • 0 comments
  • 9 respects

Ninja Dollar - LCD game

Project tutorial by anime

  • 2,606 views
  • 3 comments
  • 6 respects

OSEPP LCD and keypad shield

Project tutorial by Joe

  • 39,649 views
  • 27 comments
  • 50 respects
Add projectSign up / Login