contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Sun Apr 21 11:26:12 2019 +0000
Revision:
27:771d186b1bc8
Parent:
25:7e3b6df93dd5
Child:
28:39607fb67e88
added the sprites in a separate .h file

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OmarAlebiary 6:958376d55d70 1 #include "RocketRacer.h"
OmarAlebiary 6:958376d55d70 2
OmarAlebiary 25:7e3b6df93dd5 3
OmarAlebiary 25:7e3b6df93dd5 4
OmarAlebiary 21:5c98996d1487 5 /*
OmarAlebiary 21:5c98996d1487 6 default constructor of the class and initializing th eprivate variables
OmarAlebiary 21:5c98996d1487 7 */
OmarAlebiary 15:8a768106c297 8 RocketRacer::RocketRacer()
OmarAlebiary 15:8a768106c297 9 :first_enemy_position(0),second_enemy_position(0),enemy_phase(0),
OmarAlebiary 15:8a768106c297 10 game_speed (0),score(0),Init_position(2),enemy_dead(true),control(true)
OmarAlebiary 15:8a768106c297 11 {}
OmarAlebiary 6:958376d55d70 12
OmarAlebiary 21:5c98996d1487 13
OmarAlebiary 21:5c98996d1487 14
OmarAlebiary 12:1d3b0218d8d0 15 void RocketRacer::Main_Game_Display(N5110 &lcd){
OmarAlebiary 21:5c98996d1487 16
OmarAlebiary 21:5c98996d1487 17 lcd.clear(); //clears the lcd
OmarAlebiary 24:a049cef2cc2e 18 lcd.drawRect(0,0,46,47,FILL_BLACK);//draws a transparent rectangle on the lcd
OmarAlebiary 21:5c98996d1487 19 char score_buffer[14];//buffer to store the score
OmarAlebiary 12:1d3b0218d8d0 20 char score_buffer1[14];
OmarAlebiary 21:5c98996d1487 21 char level_buffer[14];//buffer to store the current level
OmarAlebiary 24:a049cef2cc2e 22 char level_buffer1[14];
OmarAlebiary 21:5c98996d1487 23 //display score
OmarAlebiary 12:1d3b0218d8d0 24 sprintf(score_buffer,"score");
OmarAlebiary 12:1d3b0218d8d0 25 lcd.printString(score_buffer,55,0);
OmarAlebiary 12:1d3b0218d8d0 26 sprintf(score_buffer1,"%d",score);
OmarAlebiary 12:1d3b0218d8d0 27 lcd.printString(score_buffer1,58,1);
OmarAlebiary 24:a049cef2cc2e 28 lcd.drawLine(15, 2, 15,48,0);
OmarAlebiary 24:a049cef2cc2e 29 lcd.drawLine(30, 2, 30,48,0);
OmarAlebiary 21:5c98996d1487 30 //display level
OmarAlebiary 12:1d3b0218d8d0 31 sprintf(level_buffer,"Level");
OmarAlebiary 12:1d3b0218d8d0 32 lcd.printString(level_buffer,55,3);
OmarAlebiary 12:1d3b0218d8d0 33 sprintf(level_buffer1,"%d",game_speed);
OmarAlebiary 12:1d3b0218d8d0 34 lcd.printString(level_buffer1,58,4);
OmarAlebiary 24:a049cef2cc2e 35 lcd.refresh();//refreshes the lcd to render
OmarAlebiary 7:7e50cac5e0f4 36 }
OmarAlebiary 7:7e50cac5e0f4 37
OmarAlebiary 11:d4aaa959bb20 38
OmarAlebiary 11:d4aaa959bb20 39 void RocketRacer::Joystick_position(Gamepad &pad){
OmarAlebiary 14:8df7e6fced07 40
OmarAlebiary 21:5c98996d1487 41 Direction d=pad.get_direction();//assigning the object d to the method to get the joystick direction
OmarAlebiary 11:d4aaa959bb20 42
OmarAlebiary 19:ad9dc8e418c9 43 if( (d==E||pad.check_event(Gamepad::R_PRESSED) == true )
OmarAlebiary 21:5c98996d1487 44 && Init_position!=3 && control==true){//statement to check if the joystick moved right
OmarAlebiary 21:5c98996d1487 45 Init_position++;//increments the position of the player sprite
OmarAlebiary 21:5c98996d1487 46 control = false; //sets the flag to false
OmarAlebiary 23:2ca9735b16ef 47 wait(0.01);// small delay to prevent previous press being detected again
OmarAlebiary 15:8a768106c297 48 // printf("its Right\n");
OmarAlebiary 8:b547037f42be 49 }
OmarAlebiary 19:ad9dc8e418c9 50 else if( (d==W ||pad.check_event(Gamepad::L_PRESSED) == true )
OmarAlebiary 21:5c98996d1487 51 && Init_position!=1 && control==true){//statement to check if the joystick moved left
OmarAlebiary 21:5c98996d1487 52 Init_position--;//decrements the position of the player sprite
OmarAlebiary 21:5c98996d1487 53 control = false;//sets the flag to false
OmarAlebiary 23:2ca9735b16ef 54 wait(0.01);// small delay to prevent previous press being detected again
OmarAlebiary 15:8a768106c297 55 // printf("its left\n");
OmarAlebiary 8:b547037f42be 56 }
OmarAlebiary 21:5c98996d1487 57 else if(d==CENTRE){//statement to check if the joystick position is center
OmarAlebiary 21:5c98996d1487 58 control = true;//sets the flag to true
OmarAlebiary 23:2ca9735b16ef 59 wait(0.01);
OmarAlebiary 15:8a768106c297 60 // printf("its center\n");
OmarAlebiary 9:edb39a8334ee 61 }
OmarAlebiary 11:d4aaa959bb20 62 }
OmarAlebiary 12:1d3b0218d8d0 63
OmarAlebiary 12:1d3b0218d8d0 64
OmarAlebiary 11:d4aaa959bb20 65 void RocketRacer::Generate_New_Enemy(){
OmarAlebiary 12:1d3b0218d8d0 66
OmarAlebiary 21:5c98996d1487 67 srand(time(NULL));//seeding the random function
OmarAlebiary 9:edb39a8334ee 68
OmarAlebiary 21:5c98996d1487 69 if (enemy_dead){//statement to check if the enemy crossed the player sprite
OmarAlebiary 21:5c98996d1487 70 first_enemy_position = Init_position;//places the first enemy above the player's sprite
OmarAlebiary 21:5c98996d1487 71 second_enemy_position = (rand() % 3)+1;//generates a random number from 1 to 3
OmarAlebiary 12:1d3b0218d8d0 72 enemy_phase = 0;
OmarAlebiary 12:1d3b0218d8d0 73 enemy_dead = false;
OmarAlebiary 11:d4aaa959bb20 74 }
OmarAlebiary 12:1d3b0218d8d0 75 }
OmarAlebiary 11:d4aaa959bb20 76
OmarAlebiary 11:d4aaa959bb20 77 void RocketRacer::Check_Enemy_Dead(N5110 &lcd,Gamepad &pad){
OmarAlebiary 11:d4aaa959bb20 78
OmarAlebiary 19:ad9dc8e418c9 79 if (enemy_phase>23 && ((first_enemy_position== Init_position)
OmarAlebiary 21:5c98996d1487 80 || (second_enemy_position == Init_position)) ){//statement to check if the enemies collided with the rocket
OmarAlebiary 21:5c98996d1487 81 End_Game(pad,lcd);//calls the end game method that displays game over screen
OmarAlebiary 12:1d3b0218d8d0 82 }
OmarAlebiary 21:5c98996d1487 83 if (enemy_phase>39){//statement to check if the enemies crossed without colliding with the rocket
OmarAlebiary 12:1d3b0218d8d0 84 enemy_dead = true;
OmarAlebiary 21:5c98996d1487 85 score++;//increments the score
OmarAlebiary 12:1d3b0218d8d0 86 }
OmarAlebiary 11:d4aaa959bb20 87 }
OmarAlebiary 11:d4aaa959bb20 88
OmarAlebiary 12:1d3b0218d8d0 89 void RocketRacer::Game_Loop(N5110 &lcd,Gamepad &pad){
OmarAlebiary 11:d4aaa959bb20 90
OmarAlebiary 21:5c98996d1487 91 lcd.clear(); //clears the lcd
OmarAlebiary 11:d4aaa959bb20 92
OmarAlebiary 21:5c98996d1487 93 Joystick_position(pad);//calls the method to get the joystick direction according to the user
OmarAlebiary 21:5c98996d1487 94 player_position(lcd,Init_position);//calls the method to draw the sprites according to joystick postion
OmarAlebiary 11:d4aaa959bb20 95
OmarAlebiary 21:5c98996d1487 96 Generate_New_Enemy();//generate 2 new enemies
OmarAlebiary 11:d4aaa959bb20 97
OmarAlebiary 11:d4aaa959bb20 98
OmarAlebiary 21:5c98996d1487 99 enemy_position(lcd,first_enemy_position, enemy_phase);//places the first enemy according to the position
OmarAlebiary 9:edb39a8334ee 100 enemy_phase++;
OmarAlebiary 21:5c98996d1487 101 enemy_position(lcd,second_enemy_position, enemy_phase);//places the second enemy according to the position
OmarAlebiary 9:edb39a8334ee 102 enemy_phase++;
OmarAlebiary 9:edb39a8334ee 103
OmarAlebiary 21:5c98996d1487 104 Check_Enemy_Dead(lcd,pad);//checks if enemies crossed the rocket
OmarAlebiary 11:d4aaa959bb20 105
OmarAlebiary 21:5c98996d1487 106 Game_difficulty(pad); //adds difficulty to the game
OmarAlebiary 11:d4aaa959bb20 107
OmarAlebiary 21:5c98996d1487 108 lcd.refresh();//refreshes the screen
OmarAlebiary 8:b547037f42be 109 }
OmarAlebiary 8:b547037f42be 110
OmarAlebiary 9:edb39a8334ee 111
OmarAlebiary 12:1d3b0218d8d0 112 //adds difficulty to the game after proceeding with each level
OmarAlebiary 12:1d3b0218d8d0 113 void RocketRacer::Game_difficulty(Gamepad &pad){
OmarAlebiary 10:7323785c071c 114 if (score>=0 && score<=5){
OmarAlebiary 21:5c98996d1487 115 pad.led(1,1.0); game_speed = 1;//first led on to indicate level 1
OmarAlebiary 21:5c98996d1487 116 wait(0.09);//reduces the wait on each level
OmarAlebiary 20:1907ef5d29bb 117 }if (score>5 && score<=10){
OmarAlebiary 21:5c98996d1487 118 pad.led(2,1.0); game_speed = 2;//first&second leds on to indicate level 2
OmarAlebiary 10:7323785c071c 119 wait(0.07);
OmarAlebiary 20:1907ef5d29bb 120 }if (score>10 && score<=20){
OmarAlebiary 21:5c98996d1487 121 pad.led(3,1.0); game_speed = 3;//1,2,3 leds on to indicate level 3
OmarAlebiary 10:7323785c071c 122 wait(0.06);
OmarAlebiary 21:5c98996d1487 123 }if (score>20 && score<=25){//1,2,3,4 leds on to indicate level 4
OmarAlebiary 20:1907ef5d29bb 124 pad.led(4,1.0); game_speed = 4;
OmarAlebiary 10:7323785c071c 125 wait(0.05);
OmarAlebiary 21:5c98996d1487 126 }if (score>25 && score<=30){//1,2,3,4,5 leds on to indicate level 5
OmarAlebiary 20:1907ef5d29bb 127 pad.led(5,1.0); game_speed = 5;
OmarAlebiary 15:8a768106c297 128 wait(0.04);
OmarAlebiary 21:5c98996d1487 129 }if (score>30){//all 6 leds on to indicate level 6
OmarAlebiary 20:1907ef5d29bb 130 pad.led(6,1.0); game_speed = 6;
OmarAlebiary 10:7323785c071c 131 wait(0.03);
OmarAlebiary 20:1907ef5d29bb 132 }
OmarAlebiary 21:5c98996d1487 133 }
OmarAlebiary 9:edb39a8334ee 134
OmarAlebiary 9:edb39a8334ee 135
OmarAlebiary 9:edb39a8334ee 136 void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
OmarAlebiary 14:8df7e6fced07 137
OmarAlebiary 21:5c98996d1487 138 if (place==1){//draws the enemy sprite at position 1
OmarAlebiary 9:edb39a8334ee 139 lcd.drawSprite(2,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 140 }
OmarAlebiary 9:edb39a8334ee 141
OmarAlebiary 21:5c98996d1487 142 if (place==2){//draws the enemy sprite at position 2
OmarAlebiary 9:edb39a8334ee 143 lcd.drawSprite(18,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 144 }
OmarAlebiary 9:edb39a8334ee 145
OmarAlebiary 21:5c98996d1487 146 if (place==3){//draws the enemy sprite at position 3
OmarAlebiary 9:edb39a8334ee 147 lcd.drawSprite(34,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 148 }
OmarAlebiary 9:edb39a8334ee 149 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 150
OmarAlebiary 9:edb39a8334ee 151 }
OmarAlebiary 9:edb39a8334ee 152
OmarAlebiary 9:edb39a8334ee 153 void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
OmarAlebiary 9:edb39a8334ee 154
OmarAlebiary 21:5c98996d1487 155 Main_Game_Display(lcd);//displays the game screen
OmarAlebiary 9:edb39a8334ee 156
OmarAlebiary 21:5c98996d1487 157 if (RocketPosition==1){//places the rocket at positon 1
OmarAlebiary 9:edb39a8334ee 158 lcd.drawSprite(2,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 159 }
OmarAlebiary 21:5c98996d1487 160 if (RocketPosition==2){//places the rocket at positon 2
OmarAlebiary 9:edb39a8334ee 161 lcd.drawSprite(18,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 162 }
OmarAlebiary 21:5c98996d1487 163 if (RocketPosition==3){//places the rocket at positon 3
OmarAlebiary 9:edb39a8334ee 164 lcd.drawSprite(34,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 165 }
OmarAlebiary 9:edb39a8334ee 166 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 167 }
OmarAlebiary 9:edb39a8334ee 168
OmarAlebiary 9:edb39a8334ee 169
OmarAlebiary 12:1d3b0218d8d0 170 void RocketRacer::End_Game(Gamepad &pad,N5110 &lcd){
OmarAlebiary 14:8df7e6fced07 171
OmarAlebiary 10:7323785c071c 172 lcd.clear();
OmarAlebiary 10:7323785c071c 173 char buffer1[14];
OmarAlebiary 21:5c98996d1487 174 //prints the gameover to the lcd with the score achieved
OmarAlebiary 8:b547037f42be 175 lcd.printString("Game over!!!",5,0);
OmarAlebiary 10:7323785c071c 176 lcd.printString("Better Luck ",2,1);
OmarAlebiary 10:7323785c071c 177 lcd.printString("next time",2,2);
OmarAlebiary 10:7323785c071c 178 lcd.printString("High score:",2,3);
OmarAlebiary 10:7323785c071c 179 sprintf(buffer1,"%d",score);
OmarAlebiary 10:7323785c071c 180 lcd.printString(buffer1,20,4);
OmarAlebiary 14:8df7e6fced07 181
OmarAlebiary 25:7e3b6df93dd5 182 pad.leds_on();//turns all leds on
OmarAlebiary 10:7323785c071c 183 lcd.refresh();
OmarAlebiary 25:7e3b6df93dd5 184 End_Game_Melody(pad);
OmarAlebiary 25:7e3b6df93dd5 185
OmarAlebiary 10:7323785c071c 186 wait(500);
OmarAlebiary 7:7e50cac5e0f4 187
OmarAlebiary 25:7e3b6df93dd5 188
OmarAlebiary 25:7e3b6df93dd5 189
OmarAlebiary 25:7e3b6df93dd5 190 }
OmarAlebiary 25:7e3b6df93dd5 191
OmarAlebiary 25:7e3b6df93dd5 192 void RocketRacer::End_Game_Melody(Gamepad &pad){
OmarAlebiary 25:7e3b6df93dd5 193 pad.tone(300,1);//makes a tone to indicate gameover
OmarAlebiary 25:7e3b6df93dd5 194 wait(0.25);
OmarAlebiary 25:7e3b6df93dd5 195 pad.tone(450,1);//makes a tone to indicate gameover
OmarAlebiary 25:7e3b6df93dd5 196 wait(0.25);
OmarAlebiary 25:7e3b6df93dd5 197 pad.tone(300,1);//makes a tone to indicate gameover
OmarAlebiary 25:7e3b6df93dd5 198 wait(0.5);
OmarAlebiary 25:7e3b6df93dd5 199 pad.tone(450,1);//makes a tone to indicate gameover
OmarAlebiary 25:7e3b6df93dd5 200 wait(0.25);
OmarAlebiary 25:7e3b6df93dd5 201 pad.tone(300,1);//makes a tone to indicate gameover
OmarAlebiary 25:7e3b6df93dd5 202 wait(1);
OmarAlebiary 7:7e50cac5e0f4 203 }