contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Mon Apr 29 17:34:37 2019 +0000
Revision:
32:e5d997d2ed79
Parent:
31:4d4a9d78cae5
Child:
33:24ef796ff2c8
Fixed accelerometer sensitivity

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