Omar Alebiary / Mbed 2 deprecated el17oa

Dependencies:   mbed FXOS8700CQQQ

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RocketRacer.cpp Source File

RocketRacer.cpp

00001 #include "RocketRacer.h"
00002 
00003 // create object and specifiy pins
00004 FXOS8700CQ device(I2C_SDA,I2C_SCL);
00005 
00006 //default constructor of the class and initializing the private variables
00007 RocketRacer::RocketRacer()
00008 :first_enemy_position(0),second_enemy_position(0),enemy_phase(0),
00009 game_speed (0),score(0),pitchAngle(0.0),Init_position(2),enemy_dead(true),control(true)
00010 {}
00011 
00012 //destructor of the class
00013 RocketRacer::~RocketRacer(){
00014 }
00015 
00016 /////////setters/////////////////
00017 
00018 //sets the enemy position
00019 void RocketRacer::set_first_position(int first_enemy_position) {
00020  first_enemy_position = first_enemy_position;
00021 }
00022 
00023 //sets the second enemy position
00024 void RocketRacer::set_second_position(int second_enemy_position) {
00025  second_enemy_position = second_enemy_position;
00026 }
00027 
00028 
00029 void RocketRacer::set_enemy_phase(int enemy_phase) {
00030  enemy_phase = enemy_phase;
00031 }
00032 
00033 //sets the game speed
00034 void RocketRacer::set_game_speed(int game_speed) {
00035  game_speed = game_speed;
00036 }
00037 
00038 //sets the game score
00039 void RocketRacer::set_game_score(int score) {
00040  score = score;
00041 }
00042 
00043 //sets the flag enemy_dead
00044 void RocketRacer::set_enemy_dead(bool enemy_dead) {
00045  enemy_dead = enemy_dead;
00046 }
00047 
00048 //sets the flag control
00049 void RocketRacer::set_control(bool control) {
00050  control = control;
00051 }
00052 
00053 //sets the initial position
00054 void RocketRacer::set_init_position(int Init_position) {
00055  Init_position = Init_position;
00056 }
00057 
00058 /////////Game Methods/////////////////
00059 
00060 void RocketRacer::Main_Game_Display(N5110 &lcd){
00061     
00062     lcd.clear(); //clears the lcd 
00063     lcd.drawRect(0,0,46,47,FILL_BLACK);//draws a transparent rectangle on the lcd
00064     char score_buffer[14];//buffer to store the score 
00065     char score_buffer1[14];
00066     char level_buffer[14];//buffer to store the current level 
00067     char level_buffer1[14];  
00068     //display score
00069     sprintf(score_buffer,"score");
00070     lcd.printString(score_buffer,55,0);
00071     sprintf(score_buffer1,"%d",score);
00072     lcd.printString(score_buffer1,58,1);
00073     lcd.drawLine(15, 2, 15,48,0);
00074     lcd.drawLine(30, 2, 30,48,0);
00075     //display level
00076     sprintf(level_buffer,"Level");
00077     lcd.printString(level_buffer,55,3);
00078     sprintf(level_buffer1,"%d",game_speed);
00079     lcd.printString(level_buffer1,58,4);
00080     lcd.refresh();//refreshes the lcd to render   
00081 }
00082 
00083 
00084 void RocketRacer::accelerometer_position(Gamepad &pad){
00085     
00086     pitchAngle = device.get_pitch_angle();//gets the pitch angle
00087     //statement to check if the gamepad rolled to the right
00088     if( (pitchAngle > 100)&& Init_position!=3 && control==true){
00089         Init_position++;//increments the position of the player sprite 
00090         control = false; //sets the flag to false
00091         wait(0.1);// small delay to prevent previous press being detected again
00092 //        printf("its Right\n");
00093     }
00094     //statement to check if the gamepad rolled to the left
00095     else if( (pitchAngle < 90 )&& Init_position!=1 && control==true){
00096         Init_position--;//decrements the position of the player sprite
00097         control = false;//sets the flag to false
00098         wait(0.01);// small delay to prevent previous press being detected again
00099 //        printf("its left\n");
00100     }
00101     else {//statement to check if the joystick position is center
00102         control = true;//sets the flag to true
00103         wait(0.01);
00104 //        printf("its center\n");
00105         }    
00106 }
00107 
00108 
00109 void RocketRacer::Joystick_position(Gamepad &pad){
00110     //assigning d to the method to get the joystick direction  
00111     Direction d=pad.get_direction();
00112 
00113     if( (d==E||pad.check_event(Gamepad::R_PRESSED) == true )
00114      && Init_position!=3 && control==true){//statement to check if the joystick moved right
00115         Init_position++;//increments the position of the player sprite 
00116         control = false; //sets the flag to false
00117         wait(0.09);// small delay to prevent previous press being detected again
00118 //        printf("its Right\n");
00119     }
00120     else if( (d==W ||pad.check_event(Gamepad::L_PRESSED) == true )
00121     && Init_position!=1 && control==true){//statement to check if the joystick moved left
00122         Init_position--;//decrements the position of the player sprite
00123         control = false;//sets the flag to false
00124         wait(0.01);// small delay to prevent previous press being detected again
00125 //        printf("its left\n");
00126     }
00127     else if(d==CENTRE){//statement to check if the joystick position is center
00128         control = true;//sets the flag to true
00129         wait(0.01);
00130 //        printf("its center\n");
00131         }
00132 }
00133 
00134 
00135 
00136 
00137 
00138 void RocketRacer::Generate_New_Enemy(){
00139   
00140   srand(time(NULL));//seeding the random function
00141   
00142   if (enemy_dead){//statement to check if the enemy crossed the player sprite 
00143     first_enemy_position = Init_position;//places the first enemy above the player's sprite  
00144     second_enemy_position = (rand() % 3)+1;//generates a random number from 1 to 3 
00145     //printf("%d",second_enemy_position);
00146     enemy_phase = 0; 
00147     enemy_dead = false;
00148     }
00149 }
00150 
00151    
00152 void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){
00153      //statement to check if the enemies collided with the rocket         
00154      if (enemy_phase>23 && ((first_enemy_position== Init_position)
00155       || (second_enemy_position == Init_position)) ){ 
00156          End_Game(pad,lcd);//calls the end game method that displays game over screen
00157            
00158      }
00159      if (enemy_phase>39){//statement to check if the enemies crossed without colliding with the rocket 
00160          enemy_dead = true;
00161          score++;//increments the score
00162      }
00163 }
00164  
00165     
00166 void RocketRacer::Game_Loop_accelerometer(N5110 &lcd,Gamepad &pad){
00167     
00168     lcd.clear(); //clears the lcd
00169     //calls the method to get the joystick direction according to the user
00170     accelerometer_position(pad);
00171     //calls the method to draw the sprites according to accelerometer postion
00172     player_position(lcd,Init_position);  
00173     Generate_One_Enemy();//generate 1 new enemy
00174     //places the first enemy according to the position
00175     enemy_position(lcd,first_enemy_position, enemy_phase);
00176     enemy_phase++;//increments the current phase of the enemy
00177     //places the second enemy according to the position
00178     enemy_position(lcd,second_enemy_position, enemy_phase);
00179     enemy_phase++;//increments the current phase of the enemy
00180     
00181     Check_Enemy_Dead(lcd,pad);//checks if enemies crossed the rocket
00182     
00183     Game_difficulty(pad); //adds difficulty to the game 
00184     
00185     lcd.refresh();//refreshes the screen
00186 }
00187 
00188 
00189 void RocketRacer::Generate_One_Enemy(){
00190   
00191   srand(time(NULL));//seeding the random function
00192   
00193   if (enemy_dead){//statement to check if the enemy crossed the player sprite  
00194     second_enemy_position = (rand() % 3)+1;//generates a random number from 1 to 3 
00195     //printf("%d",second_enemy_position);
00196     enemy_phase = 0; 
00197     enemy_dead = false;
00198     }
00199 }
00200 
00201     
00202 void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){
00203     lcd.clear(); //clears the lcd
00204     
00205     Joystick_position(pad);//calls the method to get the joystick direction according to the user
00206     player_position(lcd,Init_position);//calls the method to draw the sprites according to joystick postion
00207     
00208     Generate_New_Enemy();//generate 2 new enemies
00209     
00210     enemy_position(lcd,first_enemy_position, enemy_phase);//places the first enemy according to the position
00211     enemy_phase++;//increments the current phase of the enemy
00212     enemy_position(lcd,second_enemy_position, enemy_phase);//places the second enemy according to the position
00213     enemy_phase++;//increments the current phase of the enemy
00214     
00215     Check_Enemy_Dead(lcd,pad);//checks if enemies crossed the rocket
00216     
00217     Game_difficulty(pad); //adds difficulty to the game 
00218     
00219     lcd.refresh();//refreshes the screen
00220 }
00221 
00222 
00223 //adds difficulty to the game after proceeding with each level
00224 void RocketRacer::Game_difficulty(Gamepad &pad){
00225      if (score>=0 && score<=5){
00226         pad.led(1,1.0); game_speed = 1;//first led on to indicate level 1 
00227          wait(0.09);//reduces the wait on each level
00228     }if (score>5 && score<=10){
00229       pad.led(2,1.0); game_speed = 2;//first&second leds on to indicate level 2
00230          wait(0.07);
00231     }if (score>10 && score<=20){
00232         pad.led(3,1.0); game_speed = 3;//1,2,3 leds on to indicate level 3 
00233         wait(0.06); 
00234     }if (score>20 && score<=25){//1,2,3,4 leds on to indicate level 4 
00235         pad.led(4,1.0); game_speed = 4; 
00236         wait(0.05); 
00237     }if (score>25 && score<=30){//1,2,3,4,5 leds on to indicate level 5 
00238         pad.led(5,1.0); game_speed = 5; 
00239         wait(0.04); 
00240     }if (score>30){//all 6 leds on to indicate level 6 
00241        pad.led(6,1.0); game_speed = 6;
00242         wait(0.03);
00243     }
00244 }
00245 
00246 
00247 void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
00248   
00249   if (place==1){//draws the enemy sprite at position 1
00250       lcd.drawSprite(3,phase,11,9,(int *)enemy);
00251       }
00252   
00253   if (place==2){//draws the enemy sprite at position 2
00254       lcd.drawSprite(19,phase,11,9,(int *)enemy);
00255       }
00256   
00257   if (place==3){//draws the enemy sprite at position 3
00258       lcd.drawSprite(35,phase,11,9,(int *)enemy);
00259       }
00260       lcd.refresh();
00261   
00262 }
00263 
00264 
00265 void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
00266   
00267   Main_Game_Display(lcd);//displays the game screen
00268   
00269   if (RocketPosition==1){//places the rocket at positon 1   
00270       lcd.drawSprite(3,34,11,9,(int *)rocket);
00271       }
00272   if (RocketPosition==2){//places the rocket at positon 2
00273       lcd.drawSprite(19,34,11,9,(int *)rocket);
00274       }
00275   if (RocketPosition==3){//places the rocket at positon 3
00276       lcd.drawSprite(35,34,11,9,(int *)rocket);
00277       }
00278       lcd.refresh();
00279 }
00280 
00281 
00282 void RocketRacer::End_Game(Gamepad &pad,N5110 &lcd){
00283     
00284     lcd.clear();
00285     char buffer1[14];
00286     //prints the gameover to the lcd with the score achieved
00287     lcd.printString("Game over!!!",5,0);
00288     lcd.printString("Better Luck ",2,1);
00289     lcd.printString("next time",2,2);
00290     lcd.printString("High score:",2,3);
00291     sprintf(buffer1,"%d",score);
00292     lcd.printString(buffer1,20,4); 
00293     
00294     pad.leds_on();//turns all leds on    
00295     lcd.refresh();
00296     tones.End_Game_Melody(pad); //calling tones object to play melody
00297     
00298     
00299     wait(50);
00300     
00301 }
00302