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