contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Fri May 03 00:27:52 2019 +0000
Revision:
33:24ef796ff2c8
Parent:
32:e5d997d2ed79
Child:
40:13b8467526d0
added in-line comments to all classes of the project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OmarAlebiary 6:958376d55d70 1 #include "RocketRacer.h"
OmarAlebiary 6:958376d55d70 2
OmarAlebiary 32:e5d997d2ed79 3 // create object and specifiy pins
OmarAlebiary 30:c5060010a1e6 4 FXOS8700CQ device(I2C_SDA,I2C_SCL);
OmarAlebiary 25:7e3b6df93dd5 5
OmarAlebiary 30:c5060010a1e6 6 //default constructor of the class and initializing the private variables
OmarAlebiary 15:8a768106c297 7 RocketRacer::RocketRacer()
OmarAlebiary 15:8a768106c297 8 :first_enemy_position(0),second_enemy_position(0),enemy_phase(0),
OmarAlebiary 15:8a768106c297 9 game_speed (0),score(0),Init_position(2),enemy_dead(true),control(true)
OmarAlebiary 15:8a768106c297 10 {}
OmarAlebiary 6:958376d55d70 11
OmarAlebiary 33:24ef796ff2c8 12 //destructor of the class
OmarAlebiary 29:e660274d8222 13 RocketRacer::~RocketRacer(){
OmarAlebiary 29:e660274d8222 14 }
OmarAlebiary 33:24ef796ff2c8 15
OmarAlebiary 33:24ef796ff2c8 16 /////////setters/////////////////
OmarAlebiary 33:24ef796ff2c8 17 //sets the enemy position
OmarAlebiary 29:e660274d8222 18 void RocketRacer::set_first_position(int first_enemy_position) {
OmarAlebiary 29:e660274d8222 19 first_enemy_position = first_enemy_position;
OmarAlebiary 29:e660274d8222 20 }
OmarAlebiary 21:5c98996d1487 21
OmarAlebiary 33:24ef796ff2c8 22 //sets the second enemy position
OmarAlebiary 29:e660274d8222 23 void RocketRacer::set_second_position(int second_enemy_position) {
OmarAlebiary 29:e660274d8222 24 second_enemy_position = second_enemy_position;
OmarAlebiary 29:e660274d8222 25 }
OmarAlebiary 29:e660274d8222 26
OmarAlebiary 33:24ef796ff2c8 27
OmarAlebiary 29:e660274d8222 28 void RocketRacer::set_enemy_phase(int enemy_phase) {
OmarAlebiary 29:e660274d8222 29 enemy_phase = enemy_phase;
OmarAlebiary 29:e660274d8222 30 }
OmarAlebiary 29:e660274d8222 31
OmarAlebiary 33:24ef796ff2c8 32 //sets the game speed
OmarAlebiary 29:e660274d8222 33 void RocketRacer::set_game_speed(int game_speed) {
OmarAlebiary 29:e660274d8222 34 game_speed = game_speed;
OmarAlebiary 29:e660274d8222 35 }
OmarAlebiary 29:e660274d8222 36
OmarAlebiary 33:24ef796ff2c8 37 //sets the game score
OmarAlebiary 29:e660274d8222 38 void RocketRacer::set_game_score(int score) {
OmarAlebiary 29:e660274d8222 39 score = score;
OmarAlebiary 29:e660274d8222 40 }
OmarAlebiary 32:e5d997d2ed79 41
OmarAlebiary 33:24ef796ff2c8 42 //sets the flag enemy_dead
OmarAlebiary 29:e660274d8222 43 void RocketRacer::set_enemy_dead(bool enemy_dead) {
OmarAlebiary 29:e660274d8222 44 enemy_dead = enemy_dead;
OmarAlebiary 29:e660274d8222 45 }
OmarAlebiary 32:e5d997d2ed79 46
OmarAlebiary 33:24ef796ff2c8 47 //sets the flag control
OmarAlebiary 29:e660274d8222 48 void RocketRacer::set_control(bool control) {
OmarAlebiary 29:e660274d8222 49 control = control;
OmarAlebiary 29:e660274d8222 50 }
OmarAlebiary 32:e5d997d2ed79 51
OmarAlebiary 33:24ef796ff2c8 52 //sets the initial position
OmarAlebiary 29:e660274d8222 53 void RocketRacer::set_init_position(int Init_position) {
OmarAlebiary 29:e660274d8222 54 Init_position = Init_position;
OmarAlebiary 29:e660274d8222 55 }
OmarAlebiary 32:e5d997d2ed79 56
OmarAlebiary 32:e5d997d2ed79 57
OmarAlebiary 12:1d3b0218d8d0 58 void RocketRacer::Main_Game_Display(N5110 &lcd){
OmarAlebiary 21:5c98996d1487 59
OmarAlebiary 21:5c98996d1487 60 lcd.clear(); //clears the lcd
OmarAlebiary 24:a049cef2cc2e 61 lcd.drawRect(0,0,46,47,FILL_BLACK);//draws a transparent rectangle on the lcd
OmarAlebiary 21:5c98996d1487 62 char score_buffer[14];//buffer to store the score
OmarAlebiary 12:1d3b0218d8d0 63 char score_buffer1[14];
OmarAlebiary 21:5c98996d1487 64 char level_buffer[14];//buffer to store the current level
OmarAlebiary 24:a049cef2cc2e 65 char level_buffer1[14];
OmarAlebiary 21:5c98996d1487 66 //display score
OmarAlebiary 12:1d3b0218d8d0 67 sprintf(score_buffer,"score");
OmarAlebiary 12:1d3b0218d8d0 68 lcd.printString(score_buffer,55,0);
OmarAlebiary 12:1d3b0218d8d0 69 sprintf(score_buffer1,"%d",score);
OmarAlebiary 12:1d3b0218d8d0 70 lcd.printString(score_buffer1,58,1);
OmarAlebiary 24:a049cef2cc2e 71 lcd.drawLine(15, 2, 15,48,0);
OmarAlebiary 24:a049cef2cc2e 72 lcd.drawLine(30, 2, 30,48,0);
OmarAlebiary 21:5c98996d1487 73 //display level
OmarAlebiary 12:1d3b0218d8d0 74 sprintf(level_buffer,"Level");
OmarAlebiary 12:1d3b0218d8d0 75 lcd.printString(level_buffer,55,3);
OmarAlebiary 12:1d3b0218d8d0 76 sprintf(level_buffer1,"%d",game_speed);
OmarAlebiary 12:1d3b0218d8d0 77 lcd.printString(level_buffer1,58,4);
OmarAlebiary 24:a049cef2cc2e 78 lcd.refresh();//refreshes the lcd to render
OmarAlebiary 31:4d4a9d78cae5 79 }
OmarAlebiary 7:7e50cac5e0f4 80
OmarAlebiary 11:d4aaa959bb20 81
OmarAlebiary 30:c5060010a1e6 82 void RocketRacer::accelerometer_position(Gamepad &pad){
OmarAlebiary 30:c5060010a1e6 83
OmarAlebiary 30:c5060010a1e6 84 float pitchAngle = device.get_pitch_angle();
OmarAlebiary 33:24ef796ff2c8 85 //statement to check if the gamepad rolled to the right
OmarAlebiary 33:24ef796ff2c8 86 if( (pitchAngle > 100)&& Init_position!=3 && control==true){
OmarAlebiary 30:c5060010a1e6 87 Init_position++;//increments the position of the player sprite
OmarAlebiary 30:c5060010a1e6 88 control = false; //sets the flag to false
OmarAlebiary 32:e5d997d2ed79 89 wait(0.1);// small delay to prevent previous press being detected again
OmarAlebiary 30:c5060010a1e6 90 // printf("its Right\n");
OmarAlebiary 30:c5060010a1e6 91 }
OmarAlebiary 33:24ef796ff2c8 92 //statement to check if the gamepad rolled to the left
OmarAlebiary 33:24ef796ff2c8 93 else if( (pitchAngle < 90 )&& Init_position!=1 && control==true){
OmarAlebiary 30:c5060010a1e6 94 Init_position--;//decrements the position of the player sprite
OmarAlebiary 30:c5060010a1e6 95 control = false;//sets the flag to false
OmarAlebiary 32:e5d997d2ed79 96 wait(0.01);// small delay to prevent previous press being detected again
OmarAlebiary 30:c5060010a1e6 97 // printf("its left\n");
OmarAlebiary 30:c5060010a1e6 98 }
OmarAlebiary 30:c5060010a1e6 99 else {//statement to check if the joystick position is center
OmarAlebiary 30:c5060010a1e6 100 control = true;//sets the flag to true
OmarAlebiary 32:e5d997d2ed79 101 wait(0.01);
OmarAlebiary 30:c5060010a1e6 102 // printf("its center\n");
OmarAlebiary 30:c5060010a1e6 103 }
OmarAlebiary 30:c5060010a1e6 104 }
OmarAlebiary 30:c5060010a1e6 105
OmarAlebiary 30:c5060010a1e6 106
OmarAlebiary 11:d4aaa959bb20 107 void RocketRacer::Joystick_position(Gamepad &pad){
OmarAlebiary 33:24ef796ff2c8 108 //assigning d to the method to get the joystick direction
OmarAlebiary 33:24ef796ff2c8 109 Direction d=pad.get_direction();
OmarAlebiary 11:d4aaa959bb20 110
OmarAlebiary 19:ad9dc8e418c9 111 if( (d==E||pad.check_event(Gamepad::R_PRESSED) == true )
OmarAlebiary 21:5c98996d1487 112 && Init_position!=3 && control==true){//statement to check if the joystick moved right
OmarAlebiary 21:5c98996d1487 113 Init_position++;//increments the position of the player sprite
OmarAlebiary 21:5c98996d1487 114 control = false; //sets the flag to false
OmarAlebiary 28:39607fb67e88 115 wait(0.09);// small delay to prevent previous press being detected again
OmarAlebiary 15:8a768106c297 116 // printf("its Right\n");
OmarAlebiary 8:b547037f42be 117 }
OmarAlebiary 19:ad9dc8e418c9 118 else if( (d==W ||pad.check_event(Gamepad::L_PRESSED) == true )
OmarAlebiary 21:5c98996d1487 119 && Init_position!=1 && control==true){//statement to check if the joystick moved left
OmarAlebiary 21:5c98996d1487 120 Init_position--;//decrements the position of the player sprite
OmarAlebiary 21:5c98996d1487 121 control = false;//sets the flag to false
OmarAlebiary 23:2ca9735b16ef 122 wait(0.01);// small delay to prevent previous press being detected again
OmarAlebiary 15:8a768106c297 123 // printf("its left\n");
OmarAlebiary 8:b547037f42be 124 }
OmarAlebiary 21:5c98996d1487 125 else if(d==CENTRE){//statement to check if the joystick position is center
OmarAlebiary 21:5c98996d1487 126 control = true;//sets the flag to true
OmarAlebiary 23:2ca9735b16ef 127 wait(0.01);
OmarAlebiary 15:8a768106c297 128 // printf("its center\n");
OmarAlebiary 9:edb39a8334ee 129 }
OmarAlebiary 11:d4aaa959bb20 130 }
OmarAlebiary 12:1d3b0218d8d0 131
OmarAlebiary 12:1d3b0218d8d0 132
OmarAlebiary 11:d4aaa959bb20 133 void RocketRacer::Generate_New_Enemy(){
OmarAlebiary 12:1d3b0218d8d0 134
OmarAlebiary 21:5c98996d1487 135 srand(time(NULL));//seeding the random function
OmarAlebiary 9:edb39a8334ee 136
OmarAlebiary 21:5c98996d1487 137 if (enemy_dead){//statement to check if the enemy crossed the player sprite
OmarAlebiary 21:5c98996d1487 138 first_enemy_position = Init_position;//places the first enemy above the player's sprite
OmarAlebiary 21:5c98996d1487 139 second_enemy_position = (rand() % 3)+1;//generates a random number from 1 to 3
OmarAlebiary 28:39607fb67e88 140 //printf("%d",second_enemy_position);
OmarAlebiary 12:1d3b0218d8d0 141 enemy_phase = 0;
OmarAlebiary 12:1d3b0218d8d0 142 enemy_dead = false;
OmarAlebiary 11:d4aaa959bb20 143 }
OmarAlebiary 12:1d3b0218d8d0 144 }
OmarAlebiary 28:39607fb67e88 145
OmarAlebiary 28:39607fb67e88 146
OmarAlebiary 11:d4aaa959bb20 147 void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){
OmarAlebiary 33:24ef796ff2c8 148 //statement to check if the enemies collided with the rocket
OmarAlebiary 19:ad9dc8e418c9 149 if (enemy_phase>23 && ((first_enemy_position== Init_position)
OmarAlebiary 33:24ef796ff2c8 150 || (second_enemy_position == Init_position)) ){
OmarAlebiary 28:39607fb67e88 151 End_Game(pad,lcd);//calls the end game method that displays game over screen
OmarAlebiary 33:24ef796ff2c8 152
OmarAlebiary 12:1d3b0218d8d0 153 }
OmarAlebiary 33:24ef796ff2c8 154 if (enemy_phase>39){//statement to check if the enemies crossed without colliding with the rocket
OmarAlebiary 12:1d3b0218d8d0 155 enemy_dead = true;
OmarAlebiary 21:5c98996d1487 156 score++;//increments the score
OmarAlebiary 33:24ef796ff2c8 157 }
OmarAlebiary 33:24ef796ff2c8 158 }
OmarAlebiary 33:24ef796ff2c8 159
OmarAlebiary 11:d4aaa959bb20 160
OmarAlebiary 30:c5060010a1e6 161 void RocketRacer::Game_Loop_accelerometer(N5110 &lcd,Gamepad &pad){
OmarAlebiary 30:c5060010a1e6 162
OmarAlebiary 30:c5060010a1e6 163 lcd.clear(); //clears the lcd
OmarAlebiary 33:24ef796ff2c8 164 //calls the method to get the joystick direction according to the user
OmarAlebiary 33:24ef796ff2c8 165 accelerometer_position(pad);
OmarAlebiary 33:24ef796ff2c8 166 //calls the method to draw the sprites according to joystick postion
OmarAlebiary 33:24ef796ff2c8 167 player_position(lcd,Init_position);
OmarAlebiary 30:c5060010a1e6 168
OmarAlebiary 33:24ef796ff2c8 169 Generate_New_Enemy();//generate 2 new enemies
OmarAlebiary 33:24ef796ff2c8 170 //places the first enemy according to the position
OmarAlebiary 33:24ef796ff2c8 171 enemy_position(lcd,first_enemy_position, enemy_phase);
OmarAlebiary 33:24ef796ff2c8 172 enemy_phase++;//increments the current phase of the enemy
OmarAlebiary 33:24ef796ff2c8 173 enemy_position(lcd,second_enemy_position, enemy_phase);//places the second enemy according to the position
OmarAlebiary 33:24ef796ff2c8 174 enemy_phase++;//increments the current phase of the enemy
OmarAlebiary 33:24ef796ff2c8 175
OmarAlebiary 33:24ef796ff2c8 176 Check_Enemy_Dead(lcd,pad);//checks if enemies crossed the rocket
OmarAlebiary 33:24ef796ff2c8 177
OmarAlebiary 33:24ef796ff2c8 178 Game_difficulty(pad); //adds difficulty to the game
OmarAlebiary 33:24ef796ff2c8 179
OmarAlebiary 33:24ef796ff2c8 180 lcd.refresh();//refreshes the screen
OmarAlebiary 33:24ef796ff2c8 181 }
OmarAlebiary 28:39607fb67e88 182
OmarAlebiary 12:1d3b0218d8d0 183 void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){
OmarAlebiary 33:24ef796ff2c8 184 lcd.clear(); //clears the lcd
OmarAlebiary 11:d4aaa959bb20 185
OmarAlebiary 33:24ef796ff2c8 186 Joystick_position(pad);//calls the method to get the joystick direction according to the user
OmarAlebiary 33:24ef796ff2c8 187 player_position(lcd,Init_position);//calls the method to draw the sprites according to joystick postion
OmarAlebiary 33:24ef796ff2c8 188
OmarAlebiary 33:24ef796ff2c8 189 Generate_New_Enemy();//generate 2 new enemies
OmarAlebiary 11:d4aaa959bb20 190
OmarAlebiary 33:24ef796ff2c8 191 enemy_position(lcd,first_enemy_position, enemy_phase);//places the first enemy according to the position
OmarAlebiary 33:24ef796ff2c8 192 enemy_phase++;//increments the current phase of the enemy
OmarAlebiary 33:24ef796ff2c8 193 enemy_position(lcd,second_enemy_position, enemy_phase);//places the second enemy according to the position
OmarAlebiary 33:24ef796ff2c8 194 enemy_phase++;//increments the current phase of the enemy
OmarAlebiary 33:24ef796ff2c8 195
OmarAlebiary 33:24ef796ff2c8 196 Check_Enemy_Dead(lcd,pad);//checks if enemies crossed the rocket
OmarAlebiary 33:24ef796ff2c8 197
OmarAlebiary 33:24ef796ff2c8 198 Game_difficulty(pad); //adds difficulty to the game
OmarAlebiary 33:24ef796ff2c8 199
OmarAlebiary 33:24ef796ff2c8 200 lcd.refresh();//refreshes the screen
OmarAlebiary 33:24ef796ff2c8 201 }
OmarAlebiary 8:b547037f42be 202
OmarAlebiary 9:edb39a8334ee 203
OmarAlebiary 12:1d3b0218d8d0 204 //adds difficulty to the game after proceeding with each level
OmarAlebiary 12:1d3b0218d8d0 205 void RocketRacer::Game_difficulty(Gamepad &pad){
OmarAlebiary 10:7323785c071c 206 if (score>=0 && score<=5){
OmarAlebiary 21:5c98996d1487 207 pad.led(1,1.0); game_speed = 1;//first led on to indicate level 1
OmarAlebiary 21:5c98996d1487 208 wait(0.09);//reduces the wait on each level
OmarAlebiary 20:1907ef5d29bb 209 }if (score>5 && score<=10){
OmarAlebiary 21:5c98996d1487 210 pad.led(2,1.0); game_speed = 2;//first&second leds on to indicate level 2
OmarAlebiary 10:7323785c071c 211 wait(0.07);
OmarAlebiary 20:1907ef5d29bb 212 }if (score>10 && score<=20){
OmarAlebiary 21:5c98996d1487 213 pad.led(3,1.0); game_speed = 3;//1,2,3 leds on to indicate level 3
OmarAlebiary 10:7323785c071c 214 wait(0.06);
OmarAlebiary 21:5c98996d1487 215 }if (score>20 && score<=25){//1,2,3,4 leds on to indicate level 4
OmarAlebiary 20:1907ef5d29bb 216 pad.led(4,1.0); game_speed = 4;
OmarAlebiary 10:7323785c071c 217 wait(0.05);
OmarAlebiary 21:5c98996d1487 218 }if (score>25 && score<=30){//1,2,3,4,5 leds on to indicate level 5
OmarAlebiary 20:1907ef5d29bb 219 pad.led(5,1.0); game_speed = 5;
OmarAlebiary 15:8a768106c297 220 wait(0.04);
OmarAlebiary 21:5c98996d1487 221 }if (score>30){//all 6 leds on to indicate level 6
OmarAlebiary 20:1907ef5d29bb 222 pad.led(6,1.0); game_speed = 6;
OmarAlebiary 10:7323785c071c 223 wait(0.03);
OmarAlebiary 20:1907ef5d29bb 224 }
OmarAlebiary 21:5c98996d1487 225 }
OmarAlebiary 9:edb39a8334ee 226
OmarAlebiary 9:edb39a8334ee 227
OmarAlebiary 9:edb39a8334ee 228 void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
OmarAlebiary 14:8df7e6fced07 229
OmarAlebiary 21:5c98996d1487 230 if (place==1){//draws the enemy sprite at position 1
OmarAlebiary 28:39607fb67e88 231 lcd.drawSprite(3,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 232 }
OmarAlebiary 9:edb39a8334ee 233
OmarAlebiary 21:5c98996d1487 234 if (place==2){//draws the enemy sprite at position 2
OmarAlebiary 28:39607fb67e88 235 lcd.drawSprite(19,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 236 }
OmarAlebiary 9:edb39a8334ee 237
OmarAlebiary 21:5c98996d1487 238 if (place==3){//draws the enemy sprite at position 3
OmarAlebiary 28:39607fb67e88 239 lcd.drawSprite(35,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 240 }
OmarAlebiary 9:edb39a8334ee 241 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 242
OmarAlebiary 9:edb39a8334ee 243 }
OmarAlebiary 9:edb39a8334ee 244
OmarAlebiary 33:24ef796ff2c8 245
OmarAlebiary 9:edb39a8334ee 246 void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
OmarAlebiary 9:edb39a8334ee 247
OmarAlebiary 21:5c98996d1487 248 Main_Game_Display(lcd);//displays the game screen
OmarAlebiary 9:edb39a8334ee 249
OmarAlebiary 21:5c98996d1487 250 if (RocketPosition==1){//places the rocket at positon 1
OmarAlebiary 28:39607fb67e88 251 lcd.drawSprite(3,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 252 }
OmarAlebiary 21:5c98996d1487 253 if (RocketPosition==2){//places the rocket at positon 2
OmarAlebiary 28:39607fb67e88 254 lcd.drawSprite(19,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 255 }
OmarAlebiary 21:5c98996d1487 256 if (RocketPosition==3){//places the rocket at positon 3
OmarAlebiary 28:39607fb67e88 257 lcd.drawSprite(35,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 258 }
OmarAlebiary 9:edb39a8334ee 259 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 260 }
OmarAlebiary 9:edb39a8334ee 261
OmarAlebiary 9:edb39a8334ee 262
OmarAlebiary 12:1d3b0218d8d0 263 void RocketRacer::End_Game(Gamepad &pad,N5110 &lcd){
OmarAlebiary 14:8df7e6fced07 264
OmarAlebiary 10:7323785c071c 265 lcd.clear();
OmarAlebiary 10:7323785c071c 266 char buffer1[14];
OmarAlebiary 21:5c98996d1487 267 //prints the gameover to the lcd with the score achieved
OmarAlebiary 8:b547037f42be 268 lcd.printString("Game over!!!",5,0);
OmarAlebiary 10:7323785c071c 269 lcd.printString("Better Luck ",2,1);
OmarAlebiary 10:7323785c071c 270 lcd.printString("next time",2,2);
OmarAlebiary 10:7323785c071c 271 lcd.printString("High score:",2,3);
OmarAlebiary 10:7323785c071c 272 sprintf(buffer1,"%d",score);
OmarAlebiary 10:7323785c071c 273 lcd.printString(buffer1,20,4);
OmarAlebiary 14:8df7e6fced07 274
OmarAlebiary 25:7e3b6df93dd5 275 pad.leds_on();//turns all leds on
OmarAlebiary 10:7323785c071c 276 lcd.refresh();
OmarAlebiary 28:39607fb67e88 277 tones.End_Game_Melody(pad); //calling tones object to play melody
OmarAlebiary 7:7e50cac5e0f4 278
OmarAlebiary 25:7e3b6df93dd5 279
OmarAlebiary 28:39607fb67e88 280 wait(50);
OmarAlebiary 25:7e3b6df93dd5 281
OmarAlebiary 33:24ef796ff2c8 282 }
OmarAlebiary 25:7e3b6df93dd5 283