contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Sun Apr 28 02:43:58 2019 +0000
Revision:
31:4d4a9d78cae5
Parent:
30:c5060010a1e6
Child:
32:e5d997d2ed79
fixed instructions menu not showing properly

Who changed what in which revision?

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