contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Sun Apr 14 10:56:24 2019 +0000
Revision:
20:1907ef5d29bb
Parent:
19:ad9dc8e418c9
Child:
21:5c98996d1487
modified the min 20 lines per method

Who changed what in which revision?

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