contains my game for the embedded systems project 2645
Dependencies: mbed FXOS8700CQQQ
GameEngine/RocketRacer.cpp@11:d4aaa959bb20, 2019-03-28 (annotated)
- Committer:
- OmarAlebiary
- Date:
- Thu Mar 28 11:53:01 2019 +0000
- Revision:
- 11:d4aaa959bb20
- Parent:
- 10:7323785c071c
- Child:
- 12:1d3b0218d8d0
added game difficulty
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 | 8:b547037f42be | 3 | Gamepad Mypad; |
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 | 9:edb39a8334ee | 18 | const int rocket[11][9] = { |
OmarAlebiary | 9:edb39a8334ee | 19 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 20 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 21 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 22 | { 0,1,1,1,1,1,1,1,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,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 26 | { 0,0,0,1,1,1,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 27 | { 0,0,0,0,1,0,0,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 28 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 29 | { 0,0,1,1,1,1,1,0,0 }, |
OmarAlebiary | 9:edb39a8334ee | 30 | }; |
OmarAlebiary | 9:edb39a8334ee | 31 | |
OmarAlebiary | 9:edb39a8334ee | 32 | int enemy_0_pos, enemy_1_pos, enemy_phase; |
OmarAlebiary | 9:edb39a8334ee | 33 | int Joy_X; |
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 | 7:7e50cac5e0f4 | 42 | void RocketRacer::MainGameDisplay(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 | 7:7e50cac5e0f4 | 46 | char buffer[14]; |
OmarAlebiary | 9:edb39a8334ee | 47 | char buffer1[14]; |
OmarAlebiary | 10:7323785c071c | 48 | char buffer2[14]; |
OmarAlebiary | 10:7323785c071c | 49 | char buffer3[14]; |
OmarAlebiary | 10:7323785c071c | 50 | |
OmarAlebiary | 10:7323785c071c | 51 | //dispaly score |
OmarAlebiary | 7:7e50cac5e0f4 | 52 | sprintf(buffer,"score"); |
OmarAlebiary | 7:7e50cac5e0f4 | 53 | lcd.printString(buffer,55,0); |
OmarAlebiary | 9:edb39a8334ee | 54 | sprintf(buffer1,"%d",score); |
OmarAlebiary | 9:edb39a8334ee | 55 | lcd.printString(buffer1,58,1); |
OmarAlebiary | 10:7323785c071c | 56 | //dispaly level |
OmarAlebiary | 10:7323785c071c | 57 | sprintf(buffer2,"Level"); |
OmarAlebiary | 10:7323785c071c | 58 | lcd.printString(buffer2,55,3); |
OmarAlebiary | 10:7323785c071c | 59 | sprintf(buffer3,"%d",game_speed); |
OmarAlebiary | 10:7323785c071c | 60 | lcd.printString(buffer3,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 | 9:edb39a8334ee | 81 | printf("its middle\n"); |
OmarAlebiary | 9:edb39a8334ee | 82 | } |
OmarAlebiary | 11:d4aaa959bb20 | 83 | } |
OmarAlebiary | 11:d4aaa959bb20 | 84 | void RocketRacer::Generate_New_Enemy(){ |
OmarAlebiary | 11:d4aaa959bb20 | 85 | srand(time(NULL)); |
OmarAlebiary | 9:edb39a8334ee | 86 | |
OmarAlebiary | 9:edb39a8334ee | 87 | if (enemy_dead){ |
OmarAlebiary | 9:edb39a8334ee | 88 | enemy_0_pos = POS; |
OmarAlebiary | 9:edb39a8334ee | 89 | enemy_1_pos = (rand() % 3)+1; |
OmarAlebiary | 9:edb39a8334ee | 90 | enemy_phase = 0; |
OmarAlebiary | 9:edb39a8334ee | 91 | enemy_dead = false; |
OmarAlebiary | 9:edb39a8334ee | 92 | } |
OmarAlebiary | 11:d4aaa959bb20 | 93 | } |
OmarAlebiary | 11:d4aaa959bb20 | 94 | |
OmarAlebiary | 11:d4aaa959bb20 | 95 | void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){ |
OmarAlebiary | 11:d4aaa959bb20 | 96 | |
OmarAlebiary | 11:d4aaa959bb20 | 97 | if (enemy_phase>22 && ((enemy_0_pos == POS) || (enemy_1_pos == POS)) ){ |
OmarAlebiary | 11:d4aaa959bb20 | 98 | EndGame(pad,lcd); |
OmarAlebiary | 11:d4aaa959bb20 | 99 | } |
OmarAlebiary | 11:d4aaa959bb20 | 100 | if (enemy_phase>40){ |
OmarAlebiary | 11:d4aaa959bb20 | 101 | enemy_dead = true; |
OmarAlebiary | 11:d4aaa959bb20 | 102 | score++; |
OmarAlebiary | 11:d4aaa959bb20 | 103 | } |
OmarAlebiary | 11:d4aaa959bb20 | 104 | } |
OmarAlebiary | 11:d4aaa959bb20 | 105 | |
OmarAlebiary | 11:d4aaa959bb20 | 106 | void RocketRacer::GameLoop(N5110 &lcd,Gamepad &pad){ |
OmarAlebiary | 11:d4aaa959bb20 | 107 | |
OmarAlebiary | 11:d4aaa959bb20 | 108 | lcd.clear(); |
OmarAlebiary | 11:d4aaa959bb20 | 109 | |
OmarAlebiary | 11:d4aaa959bb20 | 110 | Joystick_position(pad); |
OmarAlebiary | 11:d4aaa959bb20 | 111 | player_position(lcd,POS); |
OmarAlebiary | 11:d4aaa959bb20 | 112 | |
OmarAlebiary | 11:d4aaa959bb20 | 113 | Generate_New_Enemy(); |
OmarAlebiary | 11:d4aaa959bb20 | 114 | |
OmarAlebiary | 11:d4aaa959bb20 | 115 | |
OmarAlebiary | 9:edb39a8334ee | 116 | enemy_position(lcd,enemy_0_pos, enemy_phase); |
OmarAlebiary | 9:edb39a8334ee | 117 | enemy_phase++; |
OmarAlebiary | 9:edb39a8334ee | 118 | enemy_position(lcd,enemy_1_pos, enemy_phase); |
OmarAlebiary | 9:edb39a8334ee | 119 | enemy_phase++; |
OmarAlebiary | 9:edb39a8334ee | 120 | |
OmarAlebiary | 11:d4aaa959bb20 | 121 | Check_Enemy_Dead(lcd,pad); |
OmarAlebiary | 11:d4aaa959bb20 | 122 | |
OmarAlebiary | 11:d4aaa959bb20 | 123 | Game_difficulty(); |
OmarAlebiary | 11:d4aaa959bb20 | 124 | |
OmarAlebiary | 11:d4aaa959bb20 | 125 | lcd.refresh(); |
OmarAlebiary | 8:b547037f42be | 126 | } |
OmarAlebiary | 8:b547037f42be | 127 | |
OmarAlebiary | 9:edb39a8334ee | 128 | |
OmarAlebiary | 10:7323785c071c | 129 | //adds difficulty to the game after proceeding each level |
OmarAlebiary | 10:7323785c071c | 130 | void RocketRacer::Game_difficulty(){ |
OmarAlebiary | 10:7323785c071c | 131 | |
OmarAlebiary | 10:7323785c071c | 132 | if (score>=0 && score<=5){ |
OmarAlebiary | 10:7323785c071c | 133 | game_speed = 1; |
OmarAlebiary | 10:7323785c071c | 134 | wait(0.09); |
OmarAlebiary | 10:7323785c071c | 135 | } |
OmarAlebiary | 10:7323785c071c | 136 | if (score>5 && score<=10){ |
OmarAlebiary | 10:7323785c071c | 137 | game_speed = 2; |
OmarAlebiary | 10:7323785c071c | 138 | wait(0.07); |
OmarAlebiary | 10:7323785c071c | 139 | } |
OmarAlebiary | 10:7323785c071c | 140 | if (score>10 && score<=20){ |
OmarAlebiary | 10:7323785c071c | 141 | game_speed = 3; |
OmarAlebiary | 10:7323785c071c | 142 | wait(0.06); |
OmarAlebiary | 10:7323785c071c | 143 | } |
OmarAlebiary | 10:7323785c071c | 144 | if (score>20 && score<=30){ |
OmarAlebiary | 10:7323785c071c | 145 | game_speed = 4; |
OmarAlebiary | 10:7323785c071c | 146 | wait(0.05); |
OmarAlebiary | 10:7323785c071c | 147 | } |
OmarAlebiary | 10:7323785c071c | 148 | if (score>30){ |
OmarAlebiary | 10:7323785c071c | 149 | game_speed = 5; |
OmarAlebiary | 10:7323785c071c | 150 | wait(0.03); |
OmarAlebiary | 10:7323785c071c | 151 | } |
OmarAlebiary | 10:7323785c071c | 152 | } |
OmarAlebiary | 9:edb39a8334ee | 153 | |
OmarAlebiary | 9:edb39a8334ee | 154 | |
OmarAlebiary | 9:edb39a8334ee | 155 | void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){ |
OmarAlebiary | 9:edb39a8334ee | 156 | if (place==1){ |
OmarAlebiary | 9:edb39a8334ee | 157 | lcd.drawSprite(2,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 158 | } |
OmarAlebiary | 9:edb39a8334ee | 159 | |
OmarAlebiary | 9:edb39a8334ee | 160 | if (place==2){ |
OmarAlebiary | 9:edb39a8334ee | 161 | lcd.drawSprite(18,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 162 | } |
OmarAlebiary | 9:edb39a8334ee | 163 | |
OmarAlebiary | 9:edb39a8334ee | 164 | if (place==3){ |
OmarAlebiary | 9:edb39a8334ee | 165 | lcd.drawSprite(34,phase,11,9,(int *)enemy); |
OmarAlebiary | 9:edb39a8334ee | 166 | } |
OmarAlebiary | 9:edb39a8334ee | 167 | lcd.refresh(); |
OmarAlebiary | 9:edb39a8334ee | 168 | |
OmarAlebiary | 9:edb39a8334ee | 169 | } |
OmarAlebiary | 9:edb39a8334ee | 170 | |
OmarAlebiary | 9:edb39a8334ee | 171 | void RocketRacer::player_position(N5110 &lcd,char RocketPosition){ |
OmarAlebiary | 9:edb39a8334ee | 172 | |
OmarAlebiary | 9:edb39a8334ee | 173 | MainGameDisplay(lcd); |
OmarAlebiary | 9:edb39a8334ee | 174 | |
OmarAlebiary | 9:edb39a8334ee | 175 | |
OmarAlebiary | 9:edb39a8334ee | 176 | if (RocketPosition==1){ |
OmarAlebiary | 9:edb39a8334ee | 177 | lcd.drawSprite(2,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 178 | } |
OmarAlebiary | 9:edb39a8334ee | 179 | if (RocketPosition==2){ |
OmarAlebiary | 9:edb39a8334ee | 180 | lcd.drawSprite(18,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 181 | } |
OmarAlebiary | 9:edb39a8334ee | 182 | if (RocketPosition==3){ |
OmarAlebiary | 9:edb39a8334ee | 183 | lcd.drawSprite(34,34,11,9,(int *)rocket); |
OmarAlebiary | 9:edb39a8334ee | 184 | } |
OmarAlebiary | 9:edb39a8334ee | 185 | lcd.refresh(); |
OmarAlebiary | 9:edb39a8334ee | 186 | } |
OmarAlebiary | 9:edb39a8334ee | 187 | |
OmarAlebiary | 9:edb39a8334ee | 188 | |
OmarAlebiary | 10:7323785c071c | 189 | void RocketRacer::EndGame(Gamepad &pad,N5110 &lcd){ |
OmarAlebiary | 10:7323785c071c | 190 | lcd.clear(); |
OmarAlebiary | 10:7323785c071c | 191 | char buffer1[14]; |
OmarAlebiary | 8:b547037f42be | 192 | lcd.printString("Game over!!!",5,0); |
OmarAlebiary | 10:7323785c071c | 193 | lcd.printString("Better Luck ",2,1); |
OmarAlebiary | 10:7323785c071c | 194 | lcd.printString("next time",2,2); |
OmarAlebiary | 10:7323785c071c | 195 | lcd.printString("High score:",2,3); |
OmarAlebiary | 10:7323785c071c | 196 | |
OmarAlebiary | 10:7323785c071c | 197 | sprintf(buffer1,"%d",score); |
OmarAlebiary | 10:7323785c071c | 198 | lcd.printString(buffer1,20,4); |
OmarAlebiary | 10:7323785c071c | 199 | pad.leds_on(); |
OmarAlebiary | 10:7323785c071c | 200 | |
OmarAlebiary | 10:7323785c071c | 201 | lcd.refresh(); |
OmarAlebiary | 10:7323785c071c | 202 | wait(500); |
OmarAlebiary | 7:7e50cac5e0f4 | 203 | |
OmarAlebiary | 7:7e50cac5e0f4 | 204 | } |