Home ARDUINO How to Build Race Game with Arduino and Nokia LCD module

How to Build Race Game with Arduino and Nokia LCD module

Programming has always been fun, and it uses a development platform like Arduino for a lot of benefits. Almost all programmers here will try to develop a game using the language they learn/practice. This helps them adjust their programming skills in an interesting and productive way. Since I introduced it, I have been a devoted advocate of Arduino and have always wanted to try something cool when I found out how cool the idea of using a graphic LCD monitor like the Nokia 5110 and Arduino to develop a game could get kicked Now. This is an interesting way to adjust some programming skills while having fun, so although you may also be interested in developing your game. Therefore, in this tutorial we will learn how to create decent games using Arduino and graphic LCD.

Competition plan:

Before we begin, it is very important to plan how your game actually works. I chose the Nokia 5110 graphics LCD monitor and joystick for hardware selection. I assume in this tutorial that you have chosen the same. Since the Nokia 5110 does not have a lot of space, we have planned the entire game within the 84 x 48 pixel resolution of our monitor. We have already produced a tutorial on how to connect the Nokia 5110 LCD to the Arduino.

Collecting hardware

  • Nokia LCD Module (Buy Now)
  • Arduino UNO (Buy Now)
  • Joystick Module (Buy Now)

Carry out the wiring

How to Build Race Game with Arduino and Nokia LCD module

The circuit of this Arduino game is very simple, we only need to connect the Nokia 5110 liquid crystal display module and joystick with the Arduino. The complete circuit diagram is as follows

The Nokia 5110 LCD can work at 3.3V and the joystick module can work at 5V, so make sure to connect only the 3.3V LCD because 5V will be permanently damaged. The LCD communicates with the Arduino via the SPI protocol and the joystick only reads the ADC read voltage changes. The connection settings will be as follows

How to Build Race Game with Arduino and Nokia LCD module

Upload source code

#include <SPI.h> //SPI librarey for Communication

#include <Adafruit_GFX.h> //Graphics lib for LCD

#include <Adafruit_PCD8544.h> //Nokia 5110 LCD librarey

//More info on how to interface Nokia 5110 with LCD is given here: https://circuitdigest.com/microcontroller-projects/nokia5110-graphical-l…

//Bitmap Data for SpaceShip

static const unsigned char PROGMEM ship[] =

{

B00000000,B00000000,

B00000001,B00000000,

B00000011,B10000000,

B00000010,B10000000,

B00000010,B11000000,

B00000111,B11000000,

B00001101,B11100000,

B00011111,B11110000,

B00111111,B11111000,

B01111111,B11111100,

B01111111,B11111100,

B01111111,B11111100,

B00011111,B11110000,

B00000111,B11100000,

B00000000,B00000000,

};

//Bitmap Data for enemyship

static const unsigned char PROGMEM enemy[] =

{

B00000101,B11000000,

B00001011,B11100000,

B00000011,B11100000,

B00110011,B11111000,

B01111111,B11111100,

B10111111,B11111010,

B01110111,B11011100,

B01111110,B11111100,

B00111111,B11111100,

B11101111,B11101110,

B11000001,B00000110,

B10000001,B00000010,

B10000000,B00000010,

B00000000,B00000000,

};

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); //Specifiy the pins to which the LCD is connected

int enemy_0_pos, enemy_1_pos, enemy_phase;

int Joy_X;

int game_speed = 0;

int score = 0;

char POS=2;

boolean enemy_dead = true;

boolean control = true;

void setup()   {

Serial.begin(9600); //Serial Monitor for Debugging

display.begin(); //Begin the LCD communication

display.setContrast(30); //Set the contrast of the display

display.clearDisplay();   // clears the screen and start new

}

void loop() {

display.clearDisplay();   // clears the screen and start new

gamescreen(); //Displays the box, score and speed values

//Get input from user

Joy_X = analogRead(A1); //Read the X vaue from Joystick

if (Joy_X < 312 && POS!=1 && control==true) //If joy stick moves right

{ POS–; control = false;} //Decrement position of spaceship

else if (Joy_X > 712 && POS!=3 && control==true) //If joy stick moves right

{ POS++; control = false;} //Increment position of spaceship

else if (Joy_X >502 && Joy_X<522) //If joystick back to initial position

control = true; //Preare it for next move

//Input from user received

 

player_car(POS); //Place the Space ship based on the input from user

if (enemy_dead) //Check of enemy ships are dead

{ //If they are dead

enemy_0_pos = POS; //create first enemy above the space ship

enemy_1_pos = random(0,4); //create secound enemy at some other random place

enemy_phase = 0; //Bring the enemy form the top

enemy_dead = false; //Enemy is created so they are not dead anymore

}

enemy_ship (enemy_0_pos,enemy_phase); enemy_phase++; //Place the first enemy on screen and drive him down

enemy_ship (enemy_1_pos,enemy_phase); enemy_phase++; //Place the secound enemy on screen and drive him down

if (enemy_phase>22 && ((enemy_0_pos == POS) || (enemy_1_pos == POS)) ) //If the Spaceship touches any one of the enemy

game_over(); //Display game over

 

if (enemy_phase>40) //If thespace ship escapes the enemys

{enemy_dead = true; score++;} //Increase the score and kill the enemys

Level_Controller(); //BAsed on score increase the speed of game

 

display.display();  //Update the display with all the changes made so far

}

void   Level_Controller() //Increase the speed of game based on the score.

{

if (score>=0 && score<=10) //If score 0-10

{

game_speed = 0; delay(80); //slow the game by 80ms

}

if (score>10 && score<=20) //If score 10-40

{

game_speed = 1; delay(70); //slow the game by 70ms

}

if (score>20 && score<=30) //If score 20-40

{

game_speed = 2; delay(60); //slow the game by 60ms

}

if (score>30 && score<=40) //If score 30-40

{

game_speed = 3; delay(50); //slow the game by 50ms

}

}

void enemy_ship(int place, int phase) //Place the enemy_ship in the new place and phase

{

if (place==1)

display.drawBitmap(2, phase, enemy, 15, 15, BLACK);

if (place==2)

display.drawBitmap(18, phase, enemy, 15, 15, BLACK);

if (place==3)

display.drawBitmap(34, phase, enemy, 15, 15, BLACK);

}

void game_over() //Display game over screen

{

while(1) //The program will be stuck here for ever

{

delay(100);

display.clearDisplay();

display.setCursor(20,2);

display.println(“GAME OVER”);

 

display.display();

}

}

void gamescreen()

{

//Draw the Border for Screen

display.drawLine(0, 0, 0, 47, BLACK);

display.drawLine(50, 0, 50, 47, BLACK);

display.drawLine(0, 47, 50, 47, BLACK);

//Enter Default Texts

display.setTextSize(1);

display.setTextColor(BLACK);

display.setCursor(52,2);

display.println(“Speed”);

display.setCursor(54,12);

display.println(game_speed);

display.setCursor(52,25);

display.println(“Score”);

display.setCursor(54,35);

display.println(score);

}

void player_car(char pos) //Place the spaceship based on the user selected position

{

if (pos==1)

display.drawBitmap(2, 32, ship, 15, 15, BLACK);

if (pos==2)

display.drawBitmap(18, 32, ship, 15, 15, BLACK);

if (pos==3)

display.drawBitmap(34, 32, ship, 15, 15, BLACK);

}

You may also like to read these awesome project articles

Getting Start with Joystick Shield Arduino and Nokia 51110 and build a game

How to Drive and Build Project with Nokia 5110 LCD using Arduino