contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Wed Apr 03 08:50:46 2019 +0000
Revision:
13:cec06eb1d7b0
Parent:
12:1d3b0218d8d0
Child:
14:8df7e6fced07
added control through right and left buttons

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