contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

GameEngine/RocketRacer.cpp

Committer:
OmarAlebiary
Date:
2019-03-30
Revision:
12:1d3b0218d8d0
Parent:
11:d4aaa959bb20
Child:
13:cec06eb1d7b0

File content as of revision 12:1d3b0218d8d0:

#include "RocketRacer.h"



const int enemy[11][9] =   {
    { 0,0,0,0,1,0,0,0,0 },
    { 0,0,0,1,1,1,0,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
};

const int rocket[11][9] =   {
    { 0,0,0,0,1,0,0,0,0 },
    { 0,0,0,1,1,1,0,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,0,0,1,1,1,0,0,0 },
    { 0,0,0,0,1,0,0,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
};

int first_enemy_position, second_enemy_position, enemy_phase;
int game_speed = 0;
int score = 0;
char POS=2;
bool enemy_dead = true;
bool control = true;

 

void RocketRacer::Main_Game_Display(N5110 &lcd){
    
    lcd.clear();
    lcd.drawRect(0,0,50,47,FILL_TRANSPARENT);
    char score_buffer[14];
    char score_buffer1[14];
    char level_buffer[14];
    char level_buffer1[14];

    //dispaly score
    sprintf(score_buffer,"score");
    lcd.printString(score_buffer,55,0);
    sprintf(score_buffer1,"%d",score);
    lcd.printString(score_buffer1,58,1);
    //dispaly level
    sprintf(level_buffer,"Level");
    lcd.printString(level_buffer,55,3);
    sprintf(level_buffer1,"%d",game_speed);
    lcd.printString(level_buffer1,58,4);
    lcd.refresh();
    
    }


void RocketRacer::Joystick_position(Gamepad &pad){
    Direction d=pad.get_direction();

    if(d==E && POS!=3 && control==true){
        POS++; 
        control = false;
        printf("its Right\n");
    }
    else if(d==W && POS!=1 && control==true){
        POS--;
        control = false;  
        printf("its left\n");
    }
    else if(d==CENTRE){
        control = true;
        printf("its center\n");
        }
}


void RocketRacer::Generate_New_Enemy(){
  
  srand(time(NULL));
  
  if (enemy_dead){ 
    first_enemy_position = POS; 
    second_enemy_position = (rand() % 3)+1; 
    enemy_phase = 0; 
    enemy_dead = false;
    }
}
    
void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){
             
     if (enemy_phase>22 && ((first_enemy_position== POS) || (second_enemy_position == POS)) ){
         End_Game(pad,lcd);
     }
     if (enemy_phase>40){
         enemy_dead = true;
         score++;
         } 
    }
    
void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){
    
        lcd.clear(); 
    
        Joystick_position(pad);
        player_position(lcd,POS);
            
        Generate_New_Enemy();
        
        
         enemy_position(lcd,first_enemy_position, enemy_phase);
         enemy_phase++;
         enemy_position(lcd,second_enemy_position, enemy_phase);
         enemy_phase++;
         
         Check_Enemy_Dead(lcd,pad);
         
         Game_difficulty(pad);
         
         lcd.refresh();
    }


//adds difficulty to the game after proceeding with each level
void RocketRacer::Game_difficulty(Gamepad &pad){
  
     if (score>=0 && score<=5){
        pad.led(1,1.0);
         game_speed = 1; 
         wait(0.09);
         }  
    if (score>5 && score<=10){
      pad.led(2,1.0);
        game_speed = 2;
         wait(0.07);
        }      
    if (score>10 && score<=20){
        pad.led(3,1.0);
        game_speed = 3; 
        wait(0.06); 
        }
    if (score>20 && score<=25){
        pad.led(4,1.0);
        game_speed = 4; 
        wait(0.05); 
        }
    if (score>25 && score<=30){
        pad.led(5,1.0);
        game_speed = 4; 
        wait(0.05); 
        }    
   if (score>30){
       pad.led(6,1.0);
       game_speed = 5;
        wait(0.03);
        }
    }


void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
  if (place==1){
      lcd.drawSprite(2,phase,11,9,(int *)enemy);
      }
  
  if (place==2){
      lcd.drawSprite(18,phase,11,9,(int *)enemy);
      }
  
  if (place==3){
      lcd.drawSprite(34,phase,11,9,(int *)enemy);
      }
      lcd.refresh();
  
}

void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
  
  Main_Game_Display(lcd);
  
  
  if (RocketPosition==1){   
      lcd.drawSprite(2,34,11,9,(int *)rocket);
      }
  if (RocketPosition==2){
      lcd.drawSprite(18,34,11,9,(int *)rocket);
      }
  if (RocketPosition==3){
      lcd.drawSprite(34,34,11,9,(int *)rocket);
      }
      lcd.refresh();
}


void RocketRacer::End_Game(Gamepad &pad,N5110 &lcd){
    lcd.clear();
    char buffer1[14];
    
    lcd.printString("Game over!!!",5,0);
    lcd.printString("Better Luck ",2,1);
    lcd.printString("next time",2,2);
    lcd.printString("High score:",2,3);
    sprintf(buffer1,"%d",score);
    lcd.printString(buffer1,20,4); 
    pad.leds_on();
    pad.tone(5000,2);
       
    lcd.refresh();
    wait(500);
    
    }