contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Thu Apr 25 10:44:53 2019 +0000
Revision:
29:e660274d8222
Parent:
28:39607fb67e88
Child:
30:c5060010a1e6
added doxygen comments

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