![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
contains my game for the embedded systems project 2645
Dependencies: mbed FXOS8700CQQQ
Diff: GameEngine/RocketRacer.cpp
- Revision:
- 21:5c98996d1487
- Parent:
- 20:1907ef5d29bb
- Child:
- 23:2ca9735b16ef
--- a/GameEngine/RocketRacer.cpp Sun Apr 14 10:56:24 2019 +0000 +++ b/GameEngine/RocketRacer.cpp Mon Apr 15 13:37:03 2019 +0000 @@ -1,6 +1,6 @@ #include "RocketRacer.h" - +//enemy sprite const int enemy[11][9] = { { 0,0,0,0,1,0,0,0,0 }, { 0,0,0,1,1,1,0,0,0 }, @@ -15,6 +15,7 @@ { 1,1,1,1,1,1,1,1,1 }, }; +//player sprite const int rocket[11][9] = { { 0,0,0,0,1,0,0,0,0 }, { 0,0,0,1,1,1,0,0,0 }, @@ -29,55 +30,58 @@ { 0,0,1,1,1,1,1,0,0 }, }; - +/* +default constructor of the class and initializing th eprivate variables +*/ RocketRacer::RocketRacer() :first_enemy_position(0),second_enemy_position(0),enemy_phase(0), game_speed (0),score(0),Init_position(2),enemy_dead(true),control(true) {} + + void RocketRacer::Main_Game_Display(N5110 &lcd){ - - lcd.clear(); - lcd.drawRect(0,0,50,47,FILL_TRANSPARENT); - char score_buffer[14]; + + lcd.clear(); //clears the lcd + lcd.drawRect(0,0,50,47,FILL_TRANSPARENT);//draws a transparent rectangle on the lcd + char score_buffer[14];//buffer to store the score char score_buffer1[14]; - char level_buffer[14]; + char level_buffer[14];//buffer to store the current level char level_buffer1[14]; - - - //dispaly score + + //display score sprintf(score_buffer,"score"); lcd.printString(score_buffer,55,0); sprintf(score_buffer1,"%d",score); lcd.printString(score_buffer1,58,1); - //dispaly level + //display 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(); + lcd.refresh();//refreshes the lcd to render } void RocketRacer::Joystick_position(Gamepad &pad){ - Direction d=pad.get_direction(); + Direction d=pad.get_direction();//assigning the object d to the method to get the joystick direction if( (d==E||pad.check_event(Gamepad::R_PRESSED) == true ) - && Init_position!=3 && control==true){ - Init_position++; - control = false; + && Init_position!=3 && control==true){//statement to check if the joystick moved right + Init_position++;//increments the position of the player sprite + control = false; //sets the flag to false // printf("its Right\n"); } else if( (d==W ||pad.check_event(Gamepad::L_PRESSED) == true ) - && Init_position!=1 && control==true){ - Init_position--; - control = false; + && Init_position!=1 && control==true){//statement to check if the joystick moved left + Init_position--;//decrements the position of the player sprite + control = false;//sets the flag to false // printf("its left\n"); } - else if(d==CENTRE){ - control = true; + else if(d==CENTRE){//statement to check if the joystick position is center + control = true;//sets the flag to true // printf("its center\n"); } } @@ -85,11 +89,11 @@ void RocketRacer::Generate_New_Enemy(){ - srand(time(NULL)); + srand(time(NULL));//seeding the random function - if (enemy_dead){ - first_enemy_position = Init_position; - second_enemy_position = (rand() % 3)+1; + if (enemy_dead){//statement to check if the enemy crossed the player sprite + first_enemy_position = Init_position;//places the first enemy above the player's sprite + second_enemy_position = (rand() % 3)+1;//generates a random number from 1 to 3 enemy_phase = 0; enemy_dead = false; } @@ -98,73 +102,73 @@ void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){ if (enemy_phase>23 && ((first_enemy_position== Init_position) - || (second_enemy_position == Init_position)) ){ - End_Game(pad,lcd); + || (second_enemy_position == Init_position)) ){//statement to check if the enemies collided with the rocket + End_Game(pad,lcd);//calls the end game method that displays game over screen } - if (enemy_phase>39){ + if (enemy_phase>39){//statement to check if the enemies crossed without colliding with the rocket enemy_dead = true; - score++; + score++;//increments the score } } void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){ - lcd.clear(); + lcd.clear(); //clears the lcd - Joystick_position(pad); - player_position(lcd,Init_position); + Joystick_position(pad);//calls the method to get the joystick direction according to the user + player_position(lcd,Init_position);//calls the method to draw the sprites according to joystick postion - Generate_New_Enemy(); + Generate_New_Enemy();//generate 2 new enemies - enemy_position(lcd,first_enemy_position, enemy_phase); + enemy_position(lcd,first_enemy_position, enemy_phase);//places the first enemy according to the position enemy_phase++; - enemy_position(lcd,second_enemy_position, enemy_phase); + enemy_position(lcd,second_enemy_position, enemy_phase);//places the second enemy according to the position enemy_phase++; - Check_Enemy_Dead(lcd,pad); + Check_Enemy_Dead(lcd,pad);//checks if enemies crossed the rocket - Game_difficulty(pad); + Game_difficulty(pad); //adds difficulty to the game - lcd.refresh(); + lcd.refresh();//refreshes the screen } //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); + pad.led(1,1.0); game_speed = 1;//first led on to indicate level 1 + wait(0.09);//reduces the wait on each level }if (score>5 && score<=10){ - pad.led(2,1.0); game_speed = 2; + pad.led(2,1.0); game_speed = 2;//first&second leds on to indicate level 2 wait(0.07); }if (score>10 && score<=20){ - pad.led(3,1.0); game_speed = 3; + pad.led(3,1.0); game_speed = 3;//1,2,3 leds on to indicate level 3 wait(0.06); - }if (score>20 && score<=25){ + }if (score>20 && score<=25){//1,2,3,4 leds on to indicate level 4 pad.led(4,1.0); game_speed = 4; wait(0.05); - }if (score>25 && score<=30){ + }if (score>25 && score<=30){//1,2,3,4,5 leds on to indicate level 5 pad.led(5,1.0); game_speed = 5; wait(0.04); - }if (score>30){ + }if (score>30){//all 6 leds on to indicate level 6 pad.led(6,1.0); game_speed = 6; wait(0.03); } - } +} void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){ - if (place==1){ + if (place==1){//draws the enemy sprite at position 1 lcd.drawSprite(2,phase,11,9,(int *)enemy); } - if (place==2){ + if (place==2){//draws the enemy sprite at position 2 lcd.drawSprite(18,phase,11,9,(int *)enemy); } - if (place==3){ + if (place==3){//draws the enemy sprite at position 3 lcd.drawSprite(34,phase,11,9,(int *)enemy); } lcd.refresh(); @@ -173,15 +177,15 @@ void RocketRacer::player_position(N5110 &lcd,char RocketPosition){ - Main_Game_Display(lcd); + Main_Game_Display(lcd);//displays the game screen - if (RocketPosition==1){ + if (RocketPosition==1){//places the rocket at positon 1 lcd.drawSprite(2,34,11,9,(int *)rocket); } - if (RocketPosition==2){ + if (RocketPosition==2){//places the rocket at positon 2 lcd.drawSprite(18,34,11,9,(int *)rocket); } - if (RocketPosition==3){ + if (RocketPosition==3){//places the rocket at positon 3 lcd.drawSprite(34,34,11,9,(int *)rocket); } lcd.refresh(); @@ -192,7 +196,7 @@ lcd.clear(); char buffer1[14]; - + //prints the gameover to the lcd with the score achieved lcd.printString("Game over!!!",5,0); lcd.printString("Better Luck ",2,1); lcd.printString("next time",2,2); @@ -200,8 +204,8 @@ sprintf(buffer1,"%d",score); lcd.printString(buffer1,20,4); - pad.leds_on(); - pad.tone(4500,1); + pad.leds_on();//turns all leds on + pad.tone(4500,1);//makes a tone to indicate gmeover lcd.refresh(); wait(500);