![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
contains my game for the embedded systems project 2645
Dependencies: mbed FXOS8700CQQQ
GameEngine/RocketRacer.cpp@19:ad9dc8e418c9, 2019-04-09 (annotated)
- Committer:
- OmarAlebiary
- Date:
- Tue Apr 09 15:33:52 2019 +0000
- Revision:
- 19:ad9dc8e418c9
- Parent:
- 16:93a8147a4358
- Child:
- 20:1907ef5d29bb
fixed check_event for buttonA
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 | 19:ad9dc8e418c9 | 3 | |
OmarAlebiary | 12:1d3b0218d8d0 | 4 | |
OmarAlebiary | 15:8a768106c297 | 5 | |
OmarAlebiary | 9:edb39a8334ee | 6 | const int enemy[11][9] = { |
OmarAlebiary | 9:edb39a8334ee | 7 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 8 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 9 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 10 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 11 | { 0,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 12 | { 0,1,1,1,1,1,1,1,0 }, |
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 | { 1,1,1,1,1,1,1,1,1 }, |
OmarAlebiary | 9:edb39a8334ee | 17 | { 1,1,1,1,1,1,1,1,1 }, |
OmarAlebiary | 9:edb39a8334ee | 18 | }; |
OmarAlebiary | 9:edb39a8334ee | 19 | |
OmarAlebiary | 9:edb39a8334ee | 20 | const int rocket[11][9] = { |
OmarAlebiary | 9:edb39a8334ee | 21 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 22 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 23 | { 0,0,1,1,1,1,1,0,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,1,1,1,1,1,1,1,0 }, |
OmarAlebiary | 9:edb39a8334ee | 27 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 28 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 29 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 30 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 31 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 32 | }; |
OmarAlebiary | 9:edb39a8334ee | 33 | |
OmarAlebiary | 13:cec06eb1d7b0 | 34 | |
OmarAlebiary | 15:8a768106c297 | 35 | RocketRacer::RocketRacer() |
OmarAlebiary | 15:8a768106c297 | 36 | :first_enemy_position(0),second_enemy_position(0),enemy_phase(0), |
OmarAlebiary | 15:8a768106c297 | 37 | game_speed (0),score(0),Init_position(2),enemy_dead(true),control(true) |
OmarAlebiary | 15:8a768106c297 | 38 | {} |
OmarAlebiary | 6:958376d55d70 | 39 | |
OmarAlebiary | 12:1d3b0218d8d0 | 40 | void RocketRacer::Main_Game_Display(N5110 &lcd){ |
OmarAlebiary | 7:7e50cac5e0f4 | 41 | |
OmarAlebiary | 6:958376d55d70 | 42 | lcd.clear(); |
OmarAlebiary | 9:edb39a8334ee | 43 | lcd.drawRect(0,0,50,47,FILL_TRANSPARENT); |
OmarAlebiary | 12:1d3b0218d8d0 | 44 | char score_buffer[14]; |
OmarAlebiary | 12:1d3b0218d8d0 | 45 | char score_buffer1[14]; |
OmarAlebiary | 12:1d3b0218d8d0 | 46 | char level_buffer[14]; |
OmarAlebiary | 12:1d3b0218d8d0 | 47 | char level_buffer1[14]; |
OmarAlebiary | 19:ad9dc8e418c9 | 48 | |
OmarAlebiary | 15:8a768106c297 | 49 | |
OmarAlebiary | 10:7323785c071c | 50 | //dispaly score |
OmarAlebiary | 12:1d3b0218d8d0 | 51 | sprintf(score_buffer,"score"); |
OmarAlebiary | 12:1d3b0218d8d0 | 52 | lcd.printString(score_buffer,55,0); |
OmarAlebiary | 12:1d3b0218d8d0 | 53 | sprintf(score_buffer1,"%d",score); |
OmarAlebiary | 12:1d3b0218d8d0 | 54 | lcd.printString(score_buffer1,58,1); |
OmarAlebiary | 10:7323785c071c | 55 | //dispaly level |
OmarAlebiary | 12:1d3b0218d8d0 | 56 | sprintf(level_buffer,"Level"); |
OmarAlebiary | 12:1d3b0218d8d0 | 57 | lcd.printString(level_buffer,55,3); |
OmarAlebiary | 12:1d3b0218d8d0 | 58 | sprintf(level_buffer1,"%d",game_speed); |
OmarAlebiary | 12:1d3b0218d8d0 | 59 | lcd.printString(level_buffer1,58,4); |
OmarAlebiary | 6:958376d55d70 | 60 | lcd.refresh(); |
OmarAlebiary | 7:7e50cac5e0f4 | 61 | |
OmarAlebiary | 7:7e50cac5e0f4 | 62 | } |
OmarAlebiary | 7:7e50cac5e0f4 | 63 | |
OmarAlebiary | 11:d4aaa959bb20 | 64 | |
OmarAlebiary | 11:d4aaa959bb20 | 65 | void RocketRacer::Joystick_position(Gamepad &pad){ |
OmarAlebiary | 14:8df7e6fced07 | 66 | |
OmarAlebiary | 8:b547037f42be | 67 | Direction d=pad.get_direction(); |
OmarAlebiary | 11:d4aaa959bb20 | 68 | |
OmarAlebiary | 19:ad9dc8e418c9 | 69 | if( (d==E||pad.check_event(Gamepad::R_PRESSED) == true ) |
OmarAlebiary | 19:ad9dc8e418c9 | 70 | && Init_position!=3 && control==true){ |
OmarAlebiary | 14:8df7e6fced07 | 71 | Init_position++; |
OmarAlebiary | 9:edb39a8334ee | 72 | control = false; |
OmarAlebiary | 15:8a768106c297 | 73 | // printf("its Right\n"); |
OmarAlebiary | 8:b547037f42be | 74 | } |
OmarAlebiary | 19:ad9dc8e418c9 | 75 | else if( (d==W ||pad.check_event(Gamepad::L_PRESSED) == true ) |
OmarAlebiary | 19:ad9dc8e418c9 | 76 | && Init_position!=1 && control==true){ |
OmarAlebiary | 14:8df7e6fced07 | 77 | Init_position--; |
OmarAlebiary | 9:edb39a8334ee | 78 | control = false; |
OmarAlebiary | 15:8a768106c297 | 79 | // printf("its left\n"); |
OmarAlebiary | 8:b547037f42be | 80 | } |
OmarAlebiary | 8:b547037f42be | 81 | else if(d==CENTRE){ |
OmarAlebiary | 9:edb39a8334ee | 82 | control = true; |
OmarAlebiary | 15:8a768106c297 | 83 | // printf("its center\n"); |
OmarAlebiary | 9:edb39a8334ee | 84 | } |
OmarAlebiary | 11:d4aaa959bb20 | 85 | } |
OmarAlebiary | 12:1d3b0218d8d0 | 86 | |
OmarAlebiary | 12:1d3b0218d8d0 | 87 | |
OmarAlebiary | 11:d4aaa959bb20 | 88 | void RocketRacer::Generate_New_Enemy(){ |
OmarAlebiary | 12:1d3b0218d8d0 | 89 | |
OmarAlebiary | 11:d4aaa959bb20 | 90 | srand(time(NULL)); |
OmarAlebiary | 9:edb39a8334ee | 91 | |
OmarAlebiary | 12:1d3b0218d8d0 | 92 | if (enemy_dead){ |
OmarAlebiary | 14:8df7e6fced07 | 93 | first_enemy_position = Init_position; |
OmarAlebiary | 12:1d3b0218d8d0 | 94 | second_enemy_position = (rand() % 3)+1; |
OmarAlebiary | 12:1d3b0218d8d0 | 95 | enemy_phase = 0; |
OmarAlebiary | 12:1d3b0218d8d0 | 96 | enemy_dead = false; |
OmarAlebiary | 11:d4aaa959bb20 | 97 | } |
OmarAlebiary | 12:1d3b0218d8d0 | 98 | } |
OmarAlebiary | 11:d4aaa959bb20 | 99 | |
OmarAlebiary | 11:d4aaa959bb20 | 100 | void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){ |
OmarAlebiary | 11:d4aaa959bb20 | 101 | |
OmarAlebiary | 19:ad9dc8e418c9 | 102 | if (enemy_phase>23 && ((first_enemy_position== Init_position) |
OmarAlebiary | 19:ad9dc8e418c9 | 103 | || (second_enemy_position == Init_position)) ){ |
OmarAlebiary | 15:8a768106c297 | 104 | End_Game(pad,lcd); |
OmarAlebiary | 12:1d3b0218d8d0 | 105 | } |
OmarAlebiary | 15:8a768106c297 | 106 | if (enemy_phase>39){ |
OmarAlebiary | 12:1d3b0218d8d0 | 107 | enemy_dead = true; |
OmarAlebiary | 12:1d3b0218d8d0 | 108 | score++; |
OmarAlebiary | 12:1d3b0218d8d0 | 109 | } |
OmarAlebiary | 11:d4aaa959bb20 | 110 | } |
OmarAlebiary | 11:d4aaa959bb20 | 111 | |
OmarAlebiary | 12:1d3b0218d8d0 | 112 | void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){ |
OmarAlebiary | 11:d4aaa959bb20 | 113 | |
OmarAlebiary | 11:d4aaa959bb20 | 114 | lcd.clear(); |
OmarAlebiary | 11:d4aaa959bb20 | 115 | |
OmarAlebiary | 11:d4aaa959bb20 | 116 | Joystick_position(pad); |
OmarAlebiary | 14:8df7e6fced07 | 117 | player_position(lcd,Init_position); |
OmarAlebiary | 11:d4aaa959bb20 | 118 | |
OmarAlebiary | 11:d4aaa959bb20 | 119 | Generate_New_Enemy(); |
OmarAlebiary | 11:d4aaa959bb20 | 120 | |
OmarAlebiary | 11:d4aaa959bb20 | 121 | |
OmarAlebiary | 12:1d3b0218d8d0 | 122 | enemy_position(lcd,first_enemy_position, enemy_phase); |
OmarAlebiary | 9:edb39a8334ee | 123 | enemy_phase++; |
OmarAlebiary | 12:1d3b0218d8d0 | 124 | enemy_position(lcd,second_enemy_position, enemy_phase); |
OmarAlebiary | 9:edb39a8334ee | 125 | enemy_phase++; |
OmarAlebiary | 9:edb39a8334ee | 126 | |
OmarAlebiary | 11:d4aaa959bb20 | 127 | Check_Enemy_Dead(lcd,pad); |
OmarAlebiary | 11:d4aaa959bb20 | 128 | |
OmarAlebiary | 12:1d3b0218d8d0 | 129 | Game_difficulty(pad); |
OmarAlebiary | 11:d4aaa959bb20 | 130 | |
OmarAlebiary | 11:d4aaa959bb20 | 131 | lcd.refresh(); |
OmarAlebiary | 8:b547037f42be | 132 | } |
OmarAlebiary | 8:b547037f42be | 133 | |
OmarAlebiary | 9:edb39a8334ee | 134 | |
OmarAlebiary | 12:1d3b0218d8d0 | 135 | //adds difficulty to the game after proceeding with each level |
OmarAlebiary | 12:1d3b0218d8d0 | 136 | void RocketRacer::Game_difficulty(Gamepad &pad){ |
OmarAlebiary | 14:8df7e6fced07 | 137 | |
OmarAlebiary | 10:7323785c071c | 138 | if (score>=0 && score<=5){ |
OmarAlebiary | 12:1d3b0218d8d0 | 139 | pad.led(1,1.0); |
OmarAlebiary | 12:1d3b0218d8d0 | 140 | game_speed = 1; |
OmarAlebiary | 10:7323785c071c | 141 | wait(0.09); |
OmarAlebiary | 12:1d3b0218d8d0 | 142 | } |
OmarAlebiary | 10:7323785c071c | 143 | if (score>5 && score<=10){ |
OmarAlebiary | 12:1d3b0218d8d0 | 144 | pad.led(2,1.0); |
OmarAlebiary | 10:7323785c071c | 145 | game_speed = 2; |
OmarAlebiary | 10:7323785c071c | 146 | wait(0.07); |
OmarAlebiary | 12:1d3b0218d8d0 | 147 | } |
OmarAlebiary | 10:7323785c071c | 148 | if (score>10 && score<=20){ |
OmarAlebiary | 12:1d3b0218d8d0 | 149 | pad.led(3,1.0); |
OmarAlebiary | 10:7323785c071c | 150 | game_speed = 3; |
OmarAlebiary | 10:7323785c071c | 151 | wait(0.06); |
OmarAlebiary | 10:7323785c071c | 152 | } |
OmarAlebiary | 12:1d3b0218d8d0 | 153 | if (score>20 && score<=25){ |
OmarAlebiary | 12:1d3b0218d8d0 | 154 | pad.led(4,1.0); |
OmarAlebiary | 10:7323785c071c | 155 | game_speed = 4; |
OmarAlebiary | 10:7323785c071c | 156 | wait(0.05); |
OmarAlebiary | 10:7323785c071c | 157 | } |
OmarAlebiary | 12:1d3b0218d8d0 | 158 | if (score>25 && score<=30){ |
OmarAlebiary | 12:1d3b0218d8d0 | 159 | pad.led(5,1.0); |
OmarAlebiary | 16:93a8147a4358 | 160 | game_speed = 5; |
OmarAlebiary | 15:8a768106c297 | 161 | wait(0.04); |
OmarAlebiary | 12:1d3b0218d8d0 | 162 | } |
OmarAlebiary | 10:7323785c071c | 163 | if (score>30){ |
OmarAlebiary | 12:1d3b0218d8d0 | 164 | pad.led(6,1.0); |
OmarAlebiary | 16:93a8147a4358 | 165 | game_speed = 6; |
OmarAlebiary | 10:7323785c071c | 166 | wait(0.03); |
OmarAlebiary | 10:7323785c071c | 167 | } |
OmarAlebiary | 10:7323785c071c | 168 | } |
OmarAlebiary | 9:edb39a8334ee | 169 | |
OmarAlebiary | 9:edb39a8334ee | 170 | |
OmarAlebiary | 9:edb39a8334ee | 171 | void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){ |
OmarAlebiary | 14:8df7e6fced07 | 172 | |
OmarAlebiary | 9:edb39a8334ee | 173 | if (place==1){ |
OmarAlebiary | 9:edb39a8334ee | 174 | lcd.drawSprite(2,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 175 | } |
OmarAlebiary | 9:edb39a8334ee | 176 | |
OmarAlebiary | 9:edb39a8334ee | 177 | if (place==2){ |
OmarAlebiary | 9:edb39a8334ee | 178 | lcd.drawSprite(18,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 179 | } |
OmarAlebiary | 9:edb39a8334ee | 180 | |
OmarAlebiary | 9:edb39a8334ee | 181 | if (place==3){ |
OmarAlebiary | 9:edb39a8334ee | 182 | lcd.drawSprite(34,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 183 | } |
OmarAlebiary | 9:edb39a8334ee | 184 | lcd.refresh(); |
OmarAlebiary | 9:edb39a8334ee | 185 | |
OmarAlebiary | 9:edb39a8334ee | 186 | } |
OmarAlebiary | 9:edb39a8334ee | 187 | |
OmarAlebiary | 9:edb39a8334ee | 188 | void RocketRacer::player_position(N5110 &lcd,char RocketPosition){ |
OmarAlebiary | 9:edb39a8334ee | 189 | |
OmarAlebiary | 12:1d3b0218d8d0 | 190 | Main_Game_Display(lcd); |
OmarAlebiary | 9:edb39a8334ee | 191 | |
OmarAlebiary | 9:edb39a8334ee | 192 | if (RocketPosition==1){ |
OmarAlebiary | 9:edb39a8334ee | 193 | lcd.drawSprite(2,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 194 | } |
OmarAlebiary | 9:edb39a8334ee | 195 | if (RocketPosition==2){ |
OmarAlebiary | 9:edb39a8334ee | 196 | lcd.drawSprite(18,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 197 | } |
OmarAlebiary | 9:edb39a8334ee | 198 | if (RocketPosition==3){ |
OmarAlebiary | 9:edb39a8334ee | 199 | lcd.drawSprite(34,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 200 | } |
OmarAlebiary | 9:edb39a8334ee | 201 | lcd.refresh(); |
OmarAlebiary | 9:edb39a8334ee | 202 | } |
OmarAlebiary | 9:edb39a8334ee | 203 | |
OmarAlebiary | 9:edb39a8334ee | 204 | |
OmarAlebiary | 12:1d3b0218d8d0 | 205 | void RocketRacer::End_Game(Gamepad &pad,N5110 &lcd){ |
OmarAlebiary | 14:8df7e6fced07 | 206 | |
OmarAlebiary | 10:7323785c071c | 207 | lcd.clear(); |
OmarAlebiary | 10:7323785c071c | 208 | char buffer1[14]; |
OmarAlebiary | 12:1d3b0218d8d0 | 209 | |
OmarAlebiary | 8:b547037f42be | 210 | lcd.printString("Game over!!!",5,0); |
OmarAlebiary | 10:7323785c071c | 211 | lcd.printString("Better Luck ",2,1); |
OmarAlebiary | 10:7323785c071c | 212 | lcd.printString("next time",2,2); |
OmarAlebiary | 10:7323785c071c | 213 | lcd.printString("High score:",2,3); |
OmarAlebiary | 10:7323785c071c | 214 | sprintf(buffer1,"%d",score); |
OmarAlebiary | 10:7323785c071c | 215 | lcd.printString(buffer1,20,4); |
OmarAlebiary | 14:8df7e6fced07 | 216 | |
OmarAlebiary | 10:7323785c071c | 217 | pad.leds_on(); |
OmarAlebiary | 14:8df7e6fced07 | 218 | pad.tone(4500,1); |
OmarAlebiary | 14:8df7e6fced07 | 219 | |
OmarAlebiary | 10:7323785c071c | 220 | lcd.refresh(); |
OmarAlebiary | 10:7323785c071c | 221 | wait(500); |
OmarAlebiary | 7:7e50cac5e0f4 | 222 | |
OmarAlebiary | 7:7e50cac5e0f4 | 223 | } |