contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Sun Apr 07 21:44:35 2019 +0000
Revision:
16:93a8147a4358
Parent:
15:8a768106c297
Child:
19:ad9dc8e418c9
fixed game difficulty method

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