Arduino Space Race Game Version _1

About: I am an Electric Electronic Engineer student . I want to share some projects with you

Hey friends. Today ı am ganna show you how can you make a Space Race Game by LCD5110 screen and joystic.Let's look steps...

Step 1: NEEDS

Here is a list of parts that i used to make this project:

  • Arduino UNO
  • Joystick Module
  • Jumper Wires (male to male and male to female)
  • Breadboard

Step 2: SCHEMA

VCC -> Arduino 5V

BL -> Arduino pin 3

GND -> Arduino GND

CLK (SCLK) -> Arduino pin 8

DIN (MOSI) -> Arduino pin 9

DC -> Arduino pin 10

CE or CS -> Arduino pin 12

RST (RESET) -> Arduino pin 11

Step 3: CODE

The code has explanations of code functions. You will easly understand it. If you have a problem , you can contact me.

<p>#include <lcd5110_graph.h><br></lcd5110_graph.h></p><p>LCD5110 lcd(8,9,10,11,12);      //(clk,din,dc,rst,ce)</p><p>int BL=3;
extern uint8_t SmallFont[];     //describe the fonts
extern uint8_t MediumNumbers[]; 
extern uint8_t arac[];          //describe our shape
extern uint8_t dusman_araci[];  //desribe enemy shape
const int xpin=A0;              //joystick x pin 
char pozisyon=2;                //describe the location, our shape start location 2
boolean kontrol = true;               
boolean dusmanOldu = true;
int skor=0;
int hiz=0;
int dusmanKonumu1;              //describe enemy1 x location  
int dusmanKonumu2;              //describe enemy2 x location
int dusmaninYdeKonumu;          //describe enemies y location</p><p> void dusmanAraci(int yer, int asama ){   //set locations for enemy shapes 
  if (yer==1){                                      //if location=1;
  lcd.drawBitmap(2 , asama, dusman_araci, 12, 8);   //draw enemy shape
  }
  if (yer==2){
  lcd.drawBitmap(18, asama, dusman_araci, 12, 8);
  }
  if (yer==3){
  lcd.drawBitmap(34, asama, dusman_araci, 12, 8);
  }}</p><p>  void oyunBitti() {                    //if we failure, the game over
  while(1) {                           
     delay(100);                        //wait 1 milisecond
     lcd.clrScr();                      //clear the screen
     lcd.setFont(SmallFont);            //set font
     lcd.print("GAME OVER",CENTER,20);  //print
     lcd.update();                      //update the screen
 }}</p><p> 
void setup() {
  pinMode(BL,OUTPUT);     //set back light Output 
  lcd.InitLCD();          //initilaze the lcd screen
  lcd.setContrast(55);    //set contrast(amound 0 to 127)
  Serial.begin(9600);     //start serial comunation 
}</p><p>void loop() {
  analogWrite(BL,200);</p><p> 
  
  //set game screen 
  lcd.drawRect(0,0,47,47);    //we draw rectangle 
  lcd.setFont(SmallFont);     //set font
  lcd.print("skor",53,0);     //printf score
  lcd.setFont(MediumNumbers); //set font
  lcd.printNumI(skor,59,7);   //get score 
  lcd.setFont(SmallFont);     
  lcd.print("hiz",56,25);     //print speed
  lcd.setFont(MediumNumbers); 
  lcd.printNumI(hiz,59,32);   //get speed
  lcd.update();               //update</p><p>  
  
  //set the joyistick location 
  int x=analogRead(xpin);
  if(x<300 && pozisyon!=1 && kontrol==true ){   //if location!=1,x state<300 and control is true
    pozisyon--;                                 //decrease the location it means move shape to the left
    kontrol=false;                              //control false for another movement
  }
  else if(x>600 && pozisyon!=3 && kontrol==true){ //if location!=3,x state>600 and control is true
    pozisyon++;                                   //increase the location it means move shape to the right
    kontrol=false;
  }
  else if(x>300 && x<600 ){                       
    kontrol=true;
  }
   Serial.println(x);                             //for learning x state
 
  //set the our shape location
  lcd.clrScr();                                   //clear the screen
  if (pozisyon==1){                               //if location=1;
  lcd.drawBitmap(2, 37, arac, 12, 8);}            //draw our ship
  
  if (pozisyon==2){
  lcd.drawBitmap(18, 37, arac, 12, 8);}</p><p>  if (pozisyon==3){
  lcd.drawBitmap(34, 37, arac, 12, 8);}</p><p>  if (dusmanOldu){             //if enemy shape is dead,check are they dead
  dusmanKonumu1 = pozisyon;    //draw first enemy shape
  dusmanKonumu2 = random(0,4); //draw another enemy shape somewhere
  dusmaninYdeKonumu = 0;       //bring enemy from the top
  dusmanOldu = false;}         //enemy is recreated so they are not dead
  
 
  dusmanAraci (dusmanKonumu1,dusmaninYdeKonumu); dusmaninYdeKonumu++; //draw first enemy shape and get it from top to bottom
  dusmanAraci (dusmanKonumu2,dusmaninYdeKonumu); dusmaninYdeKonumu++; //draw second enemy shape and get it from top to bottom
   
   
   if (dusmaninYdeKonumu>22 && ((dusmanKonumu1 == pozisyon) || (dusmanKonumu2 == pozisyon)) ){ //if our shape touch enemy shapes
       oyunBitti();}                                                                           //the game is over
  
   if (dusmaninYdeKonumu>40){   //if our shape escape from enemies
       dusmanOldu = true;       //kill enemy shapes
       skor++;}                 //increase one by one the score
 
    //increase speed according to score
    if (skor>=0 && skor<=10){           //if score is amoung 0 to 10
        hiz = 0; delay(80);            //the game speed is slow 80ms
  }
    if (skor>10 && skor<=20){          //if score is amoung 10 to 20
        hiz = 1; delay(65);            //increase speed to 65ms and increase speed to 1
  }
    if (skor>20 && skor<=30){         
        hiz = 2; delay(50);            
  }
    if (skor>30 && skor<=40){         
        hiz = 3; delay(40);            
  }
  
}</p>

Step 4: CODE BITMAP

Furthermore, you have to include bitmap graphic for shapes.It must be a .c file and the same name with code name.Also,it has to be in the same file.

<p>//------------------------------------------------------------------------------<br>// File generated by LCD Assistant
// http://en.radzio.dxp.pl/bitmap_converter/
//------------------------------------------------------------------------------
#include <avr pgmspace.h="">           //include for progmem function</avr></p><p>const unsigned char arac []PROGMEM = {  //our shape bitmap
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
};</p><p>const unsigned char dusman_araci []PROGMEM = {  //enemy ship bitmap
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
};</p>

Step 5: THANKS FOR VIEWING

Before ı started to share my projects, ı coded in Turkish words so i couldn't chance every words to english.Here the words translation which ı use this project,

Oyun = Game

Arac = Shape

Dusman = Enemy

kontrol = control

Konum,Yer = Location

Oyun Bitti = Game Over

Skor = Score

Hiz = Speed

Share

    Recommendations

    • Tape Contest

      Tape Contest
    • Trash to Treasure

      Trash to Treasure
    • Arduino Contest 2019

      Arduino Contest 2019

    Discussions