![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
contains my game for the embedded systems project 2645
Dependencies: mbed FXOS8700CQQQ
GameEngine/RocketRacer.cpp@21:5c98996d1487, 2019-04-15 (annotated)
- Committer:
- OmarAlebiary
- Date:
- Mon Apr 15 13:37:03 2019 +0000
- Revision:
- 21:5c98996d1487
- Parent:
- 20:1907ef5d29bb
- Child:
- 23:2ca9735b16ef
added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OmarAlebiary | 6:958376d55d70 | 1 | #include "RocketRacer.h" |
OmarAlebiary | 6:958376d55d70 | 2 | |
OmarAlebiary | 21:5c98996d1487 | 3 | //enemy sprite |
OmarAlebiary | 9:edb39a8334ee | 4 | const int enemy[11][9] = { |
OmarAlebiary | 9:edb39a8334ee | 5 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 6 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 7 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 8 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 9 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 10 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 11 | { 1,1,1,1,1,1,1,1,1 }, |
OmarAlebiary | 9:edb39a8334ee | 12 | { 1,1,1,1,1,1,1,1,1 }, |
OmarAlebiary | 9:edb39a8334ee | 13 | { 1,1,1,1,1,1,1,1,1 }, |
OmarAlebiary | 9:edb39a8334ee | 14 | { 1,1,1,1,1,1,1,1,1 }, |
OmarAlebiary | 9:edb39a8334ee | 15 | { 1,1,1,1,1,1,1,1,1 }, |
OmarAlebiary | 9:edb39a8334ee | 16 | }; |
OmarAlebiary | 9:edb39a8334ee | 17 | |
OmarAlebiary | 21:5c98996d1487 | 18 | //player sprite |
OmarAlebiary | 9:edb39a8334ee | 19 | const int rocket[11][9] = { |
OmarAlebiary | 9:edb39a8334ee | 20 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 21 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 22 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 23 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 24 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 25 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 26 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 27 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 28 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 29 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 30 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 31 | }; |
OmarAlebiary | 9:edb39a8334ee | 32 | |
OmarAlebiary | 21:5c98996d1487 | 33 | /* |
OmarAlebiary | 21:5c98996d1487 | 34 | default constructor of the class and initializing th eprivate variables |
OmarAlebiary | 21:5c98996d1487 | 35 | */ |
OmarAlebiary | 15:8a768106c297 | 36 | RocketRacer::RocketRacer() |
OmarAlebiary | 15:8a768106c297 | 37 | :first_enemy_position(0),second_enemy_position(0),enemy_phase(0), |
OmarAlebiary | 15:8a768106c297 | 38 | game_speed (0),score(0),Init_position(2),enemy_dead(true),control(true) |
OmarAlebiary | 15:8a768106c297 | 39 | {} |
OmarAlebiary | 6:958376d55d70 | 40 | |
OmarAlebiary | 21:5c98996d1487 | 41 | |
OmarAlebiary | 21:5c98996d1487 | 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 | 21:5c98996d1487 | 46 | lcd.drawRect(0,0,50,47,FILL_TRANSPARENT);//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 | 12:1d3b0218d8d0 | 50 | char level_buffer1[14]; |
OmarAlebiary | 21:5c98996d1487 | 51 | |
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 | 21:5c98996d1487 | 57 | //display level |
OmarAlebiary | 12:1d3b0218d8d0 | 58 | sprintf(level_buffer,"Level"); |
OmarAlebiary | 12:1d3b0218d8d0 | 59 | lcd.printString(level_buffer,55,3); |
OmarAlebiary | 12:1d3b0218d8d0 | 60 | sprintf(level_buffer1,"%d",game_speed); |
OmarAlebiary | 12:1d3b0218d8d0 | 61 | lcd.printString(level_buffer1,58,4); |
OmarAlebiary | 21:5c98996d1487 | 62 | lcd.refresh();//refreshes the lcd to render |
OmarAlebiary | 7:7e50cac5e0f4 | 63 | |
OmarAlebiary | 7:7e50cac5e0f4 | 64 | } |
OmarAlebiary | 7:7e50cac5e0f4 | 65 | |
OmarAlebiary | 11:d4aaa959bb20 | 66 | |
OmarAlebiary | 11:d4aaa959bb20 | 67 | void RocketRacer::Joystick_position(Gamepad &pad){ |
OmarAlebiary | 14:8df7e6fced07 | 68 | |
OmarAlebiary | 21:5c98996d1487 | 69 | Direction d=pad.get_direction();//assigning the object d to the method to get the joystick direction |
OmarAlebiary | 11:d4aaa959bb20 | 70 | |
OmarAlebiary | 19:ad9dc8e418c9 | 71 | if( (d==E||pad.check_event(Gamepad::R_PRESSED) == true ) |
OmarAlebiary | 21:5c98996d1487 | 72 | && Init_position!=3 && control==true){//statement to check if the joystick moved right |
OmarAlebiary | 21:5c98996d1487 | 73 | Init_position++;//increments the position of the player sprite |
OmarAlebiary | 21:5c98996d1487 | 74 | control = false; //sets the flag to false |
OmarAlebiary | 15:8a768106c297 | 75 | // printf("its Right\n"); |
OmarAlebiary | 8:b547037f42be | 76 | } |
OmarAlebiary | 19:ad9dc8e418c9 | 77 | else if( (d==W ||pad.check_event(Gamepad::L_PRESSED) == true ) |
OmarAlebiary | 21:5c98996d1487 | 78 | && Init_position!=1 && control==true){//statement to check if the joystick moved left |
OmarAlebiary | 21:5c98996d1487 | 79 | Init_position--;//decrements the position of the player sprite |
OmarAlebiary | 21:5c98996d1487 | 80 | control = false;//sets the flag to false |
OmarAlebiary | 15:8a768106c297 | 81 | // printf("its left\n"); |
OmarAlebiary | 8:b547037f42be | 82 | } |
OmarAlebiary | 21:5c98996d1487 | 83 | else if(d==CENTRE){//statement to check if the joystick position is center |
OmarAlebiary | 21:5c98996d1487 | 84 | control = true;//sets the flag to true |
OmarAlebiary | 15:8a768106c297 | 85 | // printf("its center\n"); |
OmarAlebiary | 9:edb39a8334ee | 86 | } |
OmarAlebiary | 11:d4aaa959bb20 | 87 | } |
OmarAlebiary | 12:1d3b0218d8d0 | 88 | |
OmarAlebiary | 12:1d3b0218d8d0 | 89 | |
OmarAlebiary | 11:d4aaa959bb20 | 90 | void RocketRacer::Generate_New_Enemy(){ |
OmarAlebiary | 12:1d3b0218d8d0 | 91 | |
OmarAlebiary | 21:5c98996d1487 | 92 | srand(time(NULL));//seeding the random function |
OmarAlebiary | 9:edb39a8334ee | 93 | |
OmarAlebiary | 21:5c98996d1487 | 94 | if (enemy_dead){//statement to check if the enemy crossed the player sprite |
OmarAlebiary | 21:5c98996d1487 | 95 | first_enemy_position = Init_position;//places the first enemy above the player's sprite |
OmarAlebiary | 21:5c98996d1487 | 96 | second_enemy_position = (rand() % 3)+1;//generates a random number from 1 to 3 |
OmarAlebiary | 12:1d3b0218d8d0 | 97 | enemy_phase = 0; |
OmarAlebiary | 12:1d3b0218d8d0 | 98 | enemy_dead = false; |
OmarAlebiary | 11:d4aaa959bb20 | 99 | } |
OmarAlebiary | 12:1d3b0218d8d0 | 100 | } |
OmarAlebiary | 11:d4aaa959bb20 | 101 | |
OmarAlebiary | 11:d4aaa959bb20 | 102 | void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){ |
OmarAlebiary | 11:d4aaa959bb20 | 103 | |
OmarAlebiary | 19:ad9dc8e418c9 | 104 | if (enemy_phase>23 && ((first_enemy_position== Init_position) |
OmarAlebiary | 21:5c98996d1487 | 105 | || (second_enemy_position == Init_position)) ){//statement to check if the enemies collided with the rocket |
OmarAlebiary | 21:5c98996d1487 | 106 | End_Game(pad,lcd);//calls the end game method that displays game over screen |
OmarAlebiary | 12:1d3b0218d8d0 | 107 | } |
OmarAlebiary | 21:5c98996d1487 | 108 | if (enemy_phase>39){//statement to check if the enemies crossed without colliding with the rocket |
OmarAlebiary | 12:1d3b0218d8d0 | 109 | enemy_dead = true; |
OmarAlebiary | 21:5c98996d1487 | 110 | score++;//increments the score |
OmarAlebiary | 12:1d3b0218d8d0 | 111 | } |
OmarAlebiary | 11:d4aaa959bb20 | 112 | } |
OmarAlebiary | 11:d4aaa959bb20 | 113 | |
OmarAlebiary | 12:1d3b0218d8d0 | 114 | void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){ |
OmarAlebiary | 11:d4aaa959bb20 | 115 | |
OmarAlebiary | 21:5c98996d1487 | 116 | lcd.clear(); //clears the lcd |
OmarAlebiary | 11:d4aaa959bb20 | 117 | |
OmarAlebiary | 21:5c98996d1487 | 118 | Joystick_position(pad);//calls the method to get the joystick direction according to the user |
OmarAlebiary | 21:5c98996d1487 | 119 | player_position(lcd,Init_position);//calls the method to draw the sprites according to joystick postion |
OmarAlebiary | 11:d4aaa959bb20 | 120 | |
OmarAlebiary | 21:5c98996d1487 | 121 | Generate_New_Enemy();//generate 2 new enemies |
OmarAlebiary | 11:d4aaa959bb20 | 122 | |
OmarAlebiary | 11:d4aaa959bb20 | 123 | |
OmarAlebiary | 21:5c98996d1487 | 124 | enemy_position(lcd,first_enemy_position, enemy_phase);//places the first enemy according to the position |
OmarAlebiary | 9:edb39a8334ee | 125 | enemy_phase++; |
OmarAlebiary | 21:5c98996d1487 | 126 | enemy_position(lcd,second_enemy_position, enemy_phase);//places the second enemy according to the position |
OmarAlebiary | 9:edb39a8334ee | 127 | enemy_phase++; |
OmarAlebiary | 9:edb39a8334ee | 128 | |
OmarAlebiary | 21:5c98996d1487 | 129 | Check_Enemy_Dead(lcd,pad);//checks if enemies crossed the rocket |
OmarAlebiary | 11:d4aaa959bb20 | 130 | |
OmarAlebiary | 21:5c98996d1487 | 131 | Game_difficulty(pad); //adds difficulty to the game |
OmarAlebiary | 11:d4aaa959bb20 | 132 | |
OmarAlebiary | 21:5c98996d1487 | 133 | lcd.refresh();//refreshes the screen |
OmarAlebiary | 8:b547037f42be | 134 | } |
OmarAlebiary | 8:b547037f42be | 135 | |
OmarAlebiary | 9:edb39a8334ee | 136 | |
OmarAlebiary | 12:1d3b0218d8d0 | 137 | //adds difficulty to the game after proceeding with each level |
OmarAlebiary | 12:1d3b0218d8d0 | 138 | void RocketRacer::Game_difficulty(Gamepad &pad){ |
OmarAlebiary | 10:7323785c071c | 139 | if (score>=0 && score<=5){ |
OmarAlebiary | 21:5c98996d1487 | 140 | pad.led(1,1.0); game_speed = 1;//first led on to indicate level 1 |
OmarAlebiary | 21:5c98996d1487 | 141 | wait(0.09);//reduces the wait on each level |
OmarAlebiary | 20:1907ef5d29bb | 142 | }if (score>5 && score<=10){ |
OmarAlebiary | 21:5c98996d1487 | 143 | pad.led(2,1.0); game_speed = 2;//first&second leds on to indicate level 2 |
OmarAlebiary | 10:7323785c071c | 144 | wait(0.07); |
OmarAlebiary | 20:1907ef5d29bb | 145 | }if (score>10 && score<=20){ |
OmarAlebiary | 21:5c98996d1487 | 146 | pad.led(3,1.0); game_speed = 3;//1,2,3 leds on to indicate level 3 |
OmarAlebiary | 10:7323785c071c | 147 | wait(0.06); |
OmarAlebiary | 21:5c98996d1487 | 148 | }if (score>20 && score<=25){//1,2,3,4 leds on to indicate level 4 |
OmarAlebiary | 20:1907ef5d29bb | 149 | pad.led(4,1.0); game_speed = 4; |
OmarAlebiary | 10:7323785c071c | 150 | wait(0.05); |
OmarAlebiary | 21:5c98996d1487 | 151 | }if (score>25 && score<=30){//1,2,3,4,5 leds on to indicate level 5 |
OmarAlebiary | 20:1907ef5d29bb | 152 | pad.led(5,1.0); game_speed = 5; |
OmarAlebiary | 15:8a768106c297 | 153 | wait(0.04); |
OmarAlebiary | 21:5c98996d1487 | 154 | }if (score>30){//all 6 leds on to indicate level 6 |
OmarAlebiary | 20:1907ef5d29bb | 155 | pad.led(6,1.0); game_speed = 6; |
OmarAlebiary | 10:7323785c071c | 156 | wait(0.03); |
OmarAlebiary | 20:1907ef5d29bb | 157 | } |
OmarAlebiary | 21:5c98996d1487 | 158 | } |
OmarAlebiary | 9:edb39a8334ee | 159 | |
OmarAlebiary | 9:edb39a8334ee | 160 | |
OmarAlebiary | 9:edb39a8334ee | 161 | void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){ |
OmarAlebiary | 14:8df7e6fced07 | 162 | |
OmarAlebiary | 21:5c98996d1487 | 163 | if (place==1){//draws the enemy sprite at position 1 |
OmarAlebiary | 9:edb39a8334ee | 164 | lcd.drawSprite(2,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 165 | } |
OmarAlebiary | 9:edb39a8334ee | 166 | |
OmarAlebiary | 21:5c98996d1487 | 167 | if (place==2){//draws the enemy sprite at position 2 |
OmarAlebiary | 9:edb39a8334ee | 168 | lcd.drawSprite(18,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 169 | } |
OmarAlebiary | 9:edb39a8334ee | 170 | |
OmarAlebiary | 21:5c98996d1487 | 171 | if (place==3){//draws the enemy sprite at position 3 |
OmarAlebiary | 9:edb39a8334ee | 172 | lcd.drawSprite(34,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 173 | } |
OmarAlebiary | 9:edb39a8334ee | 174 | lcd.refresh(); |
OmarAlebiary | 9:edb39a8334ee | 175 | |
OmarAlebiary | 9:edb39a8334ee | 176 | } |
OmarAlebiary | 9:edb39a8334ee | 177 | |
OmarAlebiary | 9:edb39a8334ee | 178 | void RocketRacer::player_position(N5110 &lcd,char RocketPosition){ |
OmarAlebiary | 9:edb39a8334ee | 179 | |
OmarAlebiary | 21:5c98996d1487 | 180 | Main_Game_Display(lcd);//displays the game screen |
OmarAlebiary | 9:edb39a8334ee | 181 | |
OmarAlebiary | 21:5c98996d1487 | 182 | if (RocketPosition==1){//places the rocket at positon 1 |
OmarAlebiary | 9:edb39a8334ee | 183 | lcd.drawSprite(2,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 184 | } |
OmarAlebiary | 21:5c98996d1487 | 185 | if (RocketPosition==2){//places the rocket at positon 2 |
OmarAlebiary | 9:edb39a8334ee | 186 | lcd.drawSprite(18,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 187 | } |
OmarAlebiary | 21:5c98996d1487 | 188 | if (RocketPosition==3){//places the rocket at positon 3 |
OmarAlebiary | 9:edb39a8334ee | 189 | lcd.drawSprite(34,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 190 | } |
OmarAlebiary | 9:edb39a8334ee | 191 | lcd.refresh(); |
OmarAlebiary | 9:edb39a8334ee | 192 | } |
OmarAlebiary | 9:edb39a8334ee | 193 | |
OmarAlebiary | 9:edb39a8334ee | 194 | |
OmarAlebiary | 12:1d3b0218d8d0 | 195 | void RocketRacer::End_Game(Gamepad &pad,N5110 &lcd){ |
OmarAlebiary | 14:8df7e6fced07 | 196 | |
OmarAlebiary | 10:7323785c071c | 197 | lcd.clear(); |
OmarAlebiary | 10:7323785c071c | 198 | char buffer1[14]; |
OmarAlebiary | 21:5c98996d1487 | 199 | //prints the gameover to the lcd with the score achieved |
OmarAlebiary | 8:b547037f42be | 200 | lcd.printString("Game over!!!",5,0); |
OmarAlebiary | 10:7323785c071c | 201 | lcd.printString("Better Luck ",2,1); |
OmarAlebiary | 10:7323785c071c | 202 | lcd.printString("next time",2,2); |
OmarAlebiary | 10:7323785c071c | 203 | lcd.printString("High score:",2,3); |
OmarAlebiary | 10:7323785c071c | 204 | sprintf(buffer1,"%d",score); |
OmarAlebiary | 10:7323785c071c | 205 | lcd.printString(buffer1,20,4); |
OmarAlebiary | 14:8df7e6fced07 | 206 | |
OmarAlebiary | 21:5c98996d1487 | 207 | pad.leds_on();//turns all leds on |
OmarAlebiary | 21:5c98996d1487 | 208 | pad.tone(4500,1);//makes a tone to indicate gmeover |
OmarAlebiary | 14:8df7e6fced07 | 209 | |
OmarAlebiary | 10:7323785c071c | 210 | lcd.refresh(); |
OmarAlebiary | 10:7323785c071c | 211 | wait(500); |
OmarAlebiary | 7:7e50cac5e0f4 | 212 | |
OmarAlebiary | 7:7e50cac5e0f4 | 213 | } |