contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Wed Apr 03 09:18:15 2019 +0000
Revision:
14:8df7e6fced07
Parent:
13:cec06eb1d7b0
Child:
15:8a768106c297
Final commit for the game code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OmarAlebiary 6:958376d55d70 1 #include "RocketRacer.h"
OmarAlebiary 6:958376d55d70 2
OmarAlebiary 13:cec06eb1d7b0 3 DigitalIn buttonL(PTB18);
OmarAlebiary 13:cec06eb1d7b0 4 DigitalIn buttonR(PTB3);
OmarAlebiary 12:1d3b0218d8d0 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 9:edb39a8334ee 35
OmarAlebiary 13:cec06eb1d7b0 36 RocketRacer::RocketRacer(){
OmarAlebiary 14:8df7e6fced07 37
OmarAlebiary 13:cec06eb1d7b0 38 first_enemy_position=0, second_enemy_position=0, enemy_phase=0;
OmarAlebiary 13:cec06eb1d7b0 39 game_speed = 0;
OmarAlebiary 13:cec06eb1d7b0 40 score = 0;
OmarAlebiary 14:8df7e6fced07 41 Init_position=2;
OmarAlebiary 13:cec06eb1d7b0 42 enemy_dead = true;
OmarAlebiary 13:cec06eb1d7b0 43 control = true;
OmarAlebiary 13:cec06eb1d7b0 44
OmarAlebiary 13:cec06eb1d7b0 45 }
OmarAlebiary 6:958376d55d70 46
OmarAlebiary 12:1d3b0218d8d0 47 void RocketRacer::Main_Game_Display(N5110 &lcd){
OmarAlebiary 7:7e50cac5e0f4 48
OmarAlebiary 6:958376d55d70 49 lcd.clear();
OmarAlebiary 9:edb39a8334ee 50 lcd.drawRect(0,0,50,47,FILL_TRANSPARENT);
OmarAlebiary 12:1d3b0218d8d0 51 char score_buffer[14];
OmarAlebiary 12:1d3b0218d8d0 52 char score_buffer1[14];
OmarAlebiary 12:1d3b0218d8d0 53 char level_buffer[14];
OmarAlebiary 12:1d3b0218d8d0 54 char level_buffer1[14];
OmarAlebiary 13:cec06eb1d7b0 55 buttonL.mode(PullDown);
OmarAlebiary 13:cec06eb1d7b0 56 buttonR.mode(PullDown);
OmarAlebiary 10:7323785c071c 57
OmarAlebiary 10:7323785c071c 58 //dispaly score
OmarAlebiary 12:1d3b0218d8d0 59 sprintf(score_buffer,"score");
OmarAlebiary 12:1d3b0218d8d0 60 lcd.printString(score_buffer,55,0);
OmarAlebiary 12:1d3b0218d8d0 61 sprintf(score_buffer1,"%d",score);
OmarAlebiary 12:1d3b0218d8d0 62 lcd.printString(score_buffer1,58,1);
OmarAlebiary 10:7323785c071c 63 //dispaly level
OmarAlebiary 12:1d3b0218d8d0 64 sprintf(level_buffer,"Level");
OmarAlebiary 12:1d3b0218d8d0 65 lcd.printString(level_buffer,55,3);
OmarAlebiary 12:1d3b0218d8d0 66 sprintf(level_buffer1,"%d",game_speed);
OmarAlebiary 12:1d3b0218d8d0 67 lcd.printString(level_buffer1,58,4);
OmarAlebiary 6:958376d55d70 68 lcd.refresh();
OmarAlebiary 7:7e50cac5e0f4 69
OmarAlebiary 7:7e50cac5e0f4 70 }
OmarAlebiary 7:7e50cac5e0f4 71
OmarAlebiary 11:d4aaa959bb20 72
OmarAlebiary 11:d4aaa959bb20 73 void RocketRacer::Joystick_position(Gamepad &pad){
OmarAlebiary 14:8df7e6fced07 74
OmarAlebiary 8:b547037f42be 75 Direction d=pad.get_direction();
OmarAlebiary 11:d4aaa959bb20 76
OmarAlebiary 14:8df7e6fced07 77 if((d==E||buttonR==true) && Init_position!=3 && control==true){
OmarAlebiary 14:8df7e6fced07 78 Init_position++;
OmarAlebiary 9:edb39a8334ee 79 control = false;
OmarAlebiary 9:edb39a8334ee 80 printf("its Right\n");
OmarAlebiary 8:b547037f42be 81 }
OmarAlebiary 14:8df7e6fced07 82 else if((d==W ||buttonL==true)&& Init_position!=1 && control==true){
OmarAlebiary 14:8df7e6fced07 83 Init_position--;
OmarAlebiary 9:edb39a8334ee 84 control = false;
OmarAlebiary 9:edb39a8334ee 85 printf("its left\n");
OmarAlebiary 8:b547037f42be 86 }
OmarAlebiary 8:b547037f42be 87 else if(d==CENTRE){
OmarAlebiary 9:edb39a8334ee 88 control = true;
OmarAlebiary 12:1d3b0218d8d0 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 11:d4aaa959bb20 96 srand(time(NULL));
OmarAlebiary 9:edb39a8334ee 97
OmarAlebiary 12:1d3b0218d8d0 98 if (enemy_dead){
OmarAlebiary 14:8df7e6fced07 99 first_enemy_position = Init_position;
OmarAlebiary 12:1d3b0218d8d0 100 second_enemy_position = (rand() % 3)+1;
OmarAlebiary 12:1d3b0218d8d0 101 enemy_phase = 0;
OmarAlebiary 12:1d3b0218d8d0 102 enemy_dead = false;
OmarAlebiary 11:d4aaa959bb20 103 }
OmarAlebiary 12:1d3b0218d8d0 104 }
OmarAlebiary 11:d4aaa959bb20 105
OmarAlebiary 11:d4aaa959bb20 106 void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){
OmarAlebiary 11:d4aaa959bb20 107
OmarAlebiary 14:8df7e6fced07 108 if (enemy_phase>22 && ((first_enemy_position== Init_position) || (second_enemy_position == Init_position)) ){
OmarAlebiary 12:1d3b0218d8d0 109 End_Game(pad,lcd);
OmarAlebiary 13:cec06eb1d7b0 110
OmarAlebiary 12:1d3b0218d8d0 111 }
OmarAlebiary 12:1d3b0218d8d0 112 if (enemy_phase>40){
OmarAlebiary 12:1d3b0218d8d0 113 enemy_dead = true;
OmarAlebiary 12:1d3b0218d8d0 114 score++;
OmarAlebiary 12:1d3b0218d8d0 115 }
OmarAlebiary 11:d4aaa959bb20 116 }
OmarAlebiary 11:d4aaa959bb20 117
OmarAlebiary 12:1d3b0218d8d0 118 void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){
OmarAlebiary 11:d4aaa959bb20 119
OmarAlebiary 11:d4aaa959bb20 120 lcd.clear();
OmarAlebiary 11:d4aaa959bb20 121
OmarAlebiary 11:d4aaa959bb20 122 Joystick_position(pad);
OmarAlebiary 14:8df7e6fced07 123 player_position(lcd,Init_position);
OmarAlebiary 11:d4aaa959bb20 124
OmarAlebiary 11:d4aaa959bb20 125 Generate_New_Enemy();
OmarAlebiary 11:d4aaa959bb20 126
OmarAlebiary 11:d4aaa959bb20 127
OmarAlebiary 12:1d3b0218d8d0 128 enemy_position(lcd,first_enemy_position, enemy_phase);
OmarAlebiary 9:edb39a8334ee 129 enemy_phase++;
OmarAlebiary 12:1d3b0218d8d0 130 enemy_position(lcd,second_enemy_position, enemy_phase);
OmarAlebiary 9:edb39a8334ee 131 enemy_phase++;
OmarAlebiary 9:edb39a8334ee 132
OmarAlebiary 11:d4aaa959bb20 133 Check_Enemy_Dead(lcd,pad);
OmarAlebiary 11:d4aaa959bb20 134
OmarAlebiary 12:1d3b0218d8d0 135 Game_difficulty(pad);
OmarAlebiary 11:d4aaa959bb20 136
OmarAlebiary 11:d4aaa959bb20 137 lcd.refresh();
OmarAlebiary 8:b547037f42be 138 }
OmarAlebiary 8:b547037f42be 139
OmarAlebiary 9:edb39a8334ee 140
OmarAlebiary 12:1d3b0218d8d0 141 //adds difficulty to the game after proceeding with each level
OmarAlebiary 12:1d3b0218d8d0 142 void RocketRacer::Game_difficulty(Gamepad &pad){
OmarAlebiary 14:8df7e6fced07 143
OmarAlebiary 10:7323785c071c 144 if (score>=0 && score<=5){
OmarAlebiary 12:1d3b0218d8d0 145 pad.led(1,1.0);
OmarAlebiary 12:1d3b0218d8d0 146 game_speed = 1;
OmarAlebiary 10:7323785c071c 147 wait(0.09);
OmarAlebiary 12:1d3b0218d8d0 148 }
OmarAlebiary 10:7323785c071c 149 if (score>5 && score<=10){
OmarAlebiary 12:1d3b0218d8d0 150 pad.led(2,1.0);
OmarAlebiary 10:7323785c071c 151 game_speed = 2;
OmarAlebiary 10:7323785c071c 152 wait(0.07);
OmarAlebiary 12:1d3b0218d8d0 153 }
OmarAlebiary 10:7323785c071c 154 if (score>10 && score<=20){
OmarAlebiary 12:1d3b0218d8d0 155 pad.led(3,1.0);
OmarAlebiary 10:7323785c071c 156 game_speed = 3;
OmarAlebiary 10:7323785c071c 157 wait(0.06);
OmarAlebiary 10:7323785c071c 158 }
OmarAlebiary 12:1d3b0218d8d0 159 if (score>20 && score<=25){
OmarAlebiary 12:1d3b0218d8d0 160 pad.led(4,1.0);
OmarAlebiary 10:7323785c071c 161 game_speed = 4;
OmarAlebiary 10:7323785c071c 162 wait(0.05);
OmarAlebiary 10:7323785c071c 163 }
OmarAlebiary 12:1d3b0218d8d0 164 if (score>25 && score<=30){
OmarAlebiary 12:1d3b0218d8d0 165 pad.led(5,1.0);
OmarAlebiary 12:1d3b0218d8d0 166 game_speed = 4;
OmarAlebiary 12:1d3b0218d8d0 167 wait(0.05);
OmarAlebiary 12:1d3b0218d8d0 168 }
OmarAlebiary 10:7323785c071c 169 if (score>30){
OmarAlebiary 12:1d3b0218d8d0 170 pad.led(6,1.0);
OmarAlebiary 10:7323785c071c 171 game_speed = 5;
OmarAlebiary 10:7323785c071c 172 wait(0.03);
OmarAlebiary 10:7323785c071c 173 }
OmarAlebiary 10:7323785c071c 174 }
OmarAlebiary 9:edb39a8334ee 175
OmarAlebiary 9:edb39a8334ee 176
OmarAlebiary 9:edb39a8334ee 177 void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
OmarAlebiary 14:8df7e6fced07 178
OmarAlebiary 9:edb39a8334ee 179 if (place==1){
OmarAlebiary 9:edb39a8334ee 180 lcd.drawSprite(2,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 181 }
OmarAlebiary 9:edb39a8334ee 182
OmarAlebiary 9:edb39a8334ee 183 if (place==2){
OmarAlebiary 9:edb39a8334ee 184 lcd.drawSprite(18,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 185 }
OmarAlebiary 9:edb39a8334ee 186
OmarAlebiary 9:edb39a8334ee 187 if (place==3){
OmarAlebiary 9:edb39a8334ee 188 lcd.drawSprite(34,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 189 }
OmarAlebiary 9:edb39a8334ee 190 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 191
OmarAlebiary 9:edb39a8334ee 192 }
OmarAlebiary 9:edb39a8334ee 193
OmarAlebiary 9:edb39a8334ee 194 void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
OmarAlebiary 9:edb39a8334ee 195
OmarAlebiary 12:1d3b0218d8d0 196 Main_Game_Display(lcd);
OmarAlebiary 9:edb39a8334ee 197
OmarAlebiary 9:edb39a8334ee 198
OmarAlebiary 9:edb39a8334ee 199 if (RocketPosition==1){
OmarAlebiary 9:edb39a8334ee 200 lcd.drawSprite(2,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 201 }
OmarAlebiary 9:edb39a8334ee 202 if (RocketPosition==2){
OmarAlebiary 9:edb39a8334ee 203 lcd.drawSprite(18,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 204 }
OmarAlebiary 9:edb39a8334ee 205 if (RocketPosition==3){
OmarAlebiary 9:edb39a8334ee 206 lcd.drawSprite(34,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 207 }
OmarAlebiary 9:edb39a8334ee 208 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 209 }
OmarAlebiary 9:edb39a8334ee 210
OmarAlebiary 9:edb39a8334ee 211
OmarAlebiary 12:1d3b0218d8d0 212 void RocketRacer::End_Game(Gamepad &pad,N5110 &lcd){
OmarAlebiary 14:8df7e6fced07 213
OmarAlebiary 10:7323785c071c 214 lcd.clear();
OmarAlebiary 10:7323785c071c 215 char buffer1[14];
OmarAlebiary 12:1d3b0218d8d0 216
OmarAlebiary 8:b547037f42be 217 lcd.printString("Game over!!!",5,0);
OmarAlebiary 10:7323785c071c 218 lcd.printString("Better Luck ",2,1);
OmarAlebiary 10:7323785c071c 219 lcd.printString("next time",2,2);
OmarAlebiary 10:7323785c071c 220 lcd.printString("High score:",2,3);
OmarAlebiary 10:7323785c071c 221 sprintf(buffer1,"%d",score);
OmarAlebiary 10:7323785c071c 222 lcd.printString(buffer1,20,4);
OmarAlebiary 14:8df7e6fced07 223
OmarAlebiary 10:7323785c071c 224 pad.leds_on();
OmarAlebiary 14:8df7e6fced07 225 pad.tone(4500,1);
OmarAlebiary 14:8df7e6fced07 226
OmarAlebiary 10:7323785c071c 227 lcd.refresh();
OmarAlebiary 10:7323785c071c 228 wait(500);
OmarAlebiary 7:7e50cac5e0f4 229
OmarAlebiary 7:7e50cac5e0f4 230 }