Louis Wray / Mbed 2 deprecated L2_2645_project

Dependencies:   DebounceIn N5110 PowerControl mbed

Committer:
wray2303
Date:
Fri May 08 11:30:43 2015 +0000
Revision:
2:8d28a2f491eb
Parent:
0:6eba0e66ce01
Child:
3:39c95d65bee5
revision 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wray2303 0:6eba0e66ce01 1 /**
wray2303 0:6eba0e66ce01 2 @file main.cpp
wray2303 0:6eba0e66ce01 3
wray2303 0:6eba0e66ce01 4 @brief Program implementation
wray2303 0:6eba0e66ce01 5
wray2303 0:6eba0e66ce01 6 @author Louis Wray
wray2303 0:6eba0e66ce01 7
wray2303 0:6eba0e66ce01 8 @date 14th April 2015
wray2303 0:6eba0e66ce01 9 */
wray2303 0:6eba0e66ce01 10
wray2303 0:6eba0e66ce01 11 #include "main.h"
wray2303 0:6eba0e66ce01 12
wray2303 0:6eba0e66ce01 13 /// timer to regularly read the joystick
wray2303 0:6eba0e66ce01 14 Ticker pollJoystick;
wray2303 0:6eba0e66ce01 15 /// timer to regularly call the read bullet function
wray2303 0:6eba0e66ce01 16 Ticker readShoot;
wray2303 0:6eba0e66ce01 17 /// timer to regularly call new asteroids
wray2303 0:6eba0e66ce01 18 Ticker readTheAsteroid;
wray2303 0:6eba0e66ce01 19 /// timeout to create the asteroids
wray2303 0:6eba0e66ce01 20 Timer shootSoundtimer;
wray2303 0:6eba0e66ce01 21
wray2303 0:6eba0e66ce01 22 /**
wray2303 0:6eba0e66ce01 23 create enumerated type (0,1,2,3 etc. for direction)
wray2303 0:6eba0e66ce01 24 */
wray2303 0:6eba0e66ce01 25 enum DirectionName {
wray2303 0:6eba0e66ce01 26 UP,
wray2303 0:6eba0e66ce01 27 DOWN,
wray2303 0:6eba0e66ce01 28 LEFT,
wray2303 0:6eba0e66ce01 29 RIGHT,
wray2303 0:6eba0e66ce01 30 CENTRE,
wray2303 0:6eba0e66ce01 31 UNKNOWN
wray2303 0:6eba0e66ce01 32 };
wray2303 0:6eba0e66ce01 33
wray2303 0:6eba0e66ce01 34 /**
wray2303 0:6eba0e66ce01 35 struct for Joystick
wray2303 0:6eba0e66ce01 36 */
wray2303 0:6eba0e66ce01 37 typedef struct JoyStick Joystick;
wray2303 0:6eba0e66ce01 38 struct JoyStick {
wray2303 0:6eba0e66ce01 39 float x; /// current x value
wray2303 0:6eba0e66ce01 40 float x0; /// 'centred' x value
wray2303 0:6eba0e66ce01 41 float y; /// current y value
wray2303 0:6eba0e66ce01 42 float y0; /// 'centred' y value
wray2303 0:6eba0e66ce01 43 int button; /// button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
wray2303 0:6eba0e66ce01 44 DirectionName direction; /// current direction
wray2303 0:6eba0e66ce01 45 };
wray2303 0:6eba0e66ce01 46 /// create struct variable
wray2303 0:6eba0e66ce01 47 Joystick joystick;
wray2303 0:6eba0e66ce01 48
wray2303 0:6eba0e66ce01 49 int printFlag =0; /**< flag for calling joystick value printing */
wray2303 0:6eba0e66ce01 50 int state = 0; /**< integer value for what state the game is in.
wray2303 0:6eba0e66ce01 51 for use of the switch statement */
wray2303 0:6eba0e66ce01 52 int GenAsteroidFlag = 0; /// asteroid generation flag
wray2303 0:6eba0e66ce01 53 int AsteroidCreatedFlag = 0; /// flag to note asteroid is created
wray2303 0:6eba0e66ce01 54 int BulletExistsFlag = 0; /// flag to note bullet has been fired and is on screen
wray2303 2:8d28a2f491eb 55 int PauseNumber = 0; /// pause flag
wray2303 2:8d28a2f491eb 56 int HighScore = 0; /// integer set for highscore
wray2303 0:6eba0e66ce01 57
wray2303 0:6eba0e66ce01 58 int main()
wray2303 0:6eba0e66ce01 59 {
wray2303 0:6eba0e66ce01 60 asteroid.nscore = 0;
wray2303 0:6eba0e66ce01 61 srand(15000*randomPin); /// seeding random function
wray2303 0:6eba0e66ce01 62 calibrateJoystick(); /// set's centred values of joystick
wray2303 0:6eba0e66ce01 63 pollJoystick.attach(&updateJoystick,1/45.0); /// read joystick 45 times per second
wray2303 0:6eba0e66ce01 64 ship.SetValues(42,39); /// initialise ships starting x,y coordinates
wray2303 0:6eba0e66ce01 65
wray2303 0:6eba0e66ce01 66 PHY_PowerDown(); /// power down ethernet connection
wray2303 0:6eba0e66ce01 67
wray2303 0:6eba0e66ce01 68 while(1) {
wray2303 0:6eba0e66ce01 69
wray2303 0:6eba0e66ce01 70 switch(state) { /// switch statement this enables the game to switch between states giving a start state
wray2303 0:6eba0e66ce01 71 /// a game play state and subsequently a game over state giving choice to start again.
wray2303 0:6eba0e66ce01 72
wray2303 0:6eba0e66ce01 73 case 0:
wray2303 0:6eba0e66ce01 74 startUpDisplay(); /// call start up display function
wray2303 0:6eba0e66ce01 75 state=1;
wray2303 0:6eba0e66ce01 76 break;
wray2303 0:6eba0e66ce01 77
wray2303 0:6eba0e66ce01 78 case 1:
wray2303 0:6eba0e66ce01 79 Game(); /// game play
wray2303 0:6eba0e66ce01 80 break;
wray2303 0:6eba0e66ce01 81
wray2303 0:6eba0e66ce01 82 case 2:
wray2303 2:8d28a2f491eb 83 lcd.clear(); /// clears display
wray2303 2:8d28a2f491eb 84 PauseDisplay(); /// applies pause screen
wray2303 0:6eba0e66ce01 85 break;
wray2303 0:6eba0e66ce01 86
wray2303 0:6eba0e66ce01 87 case 3:
wray2303 0:6eba0e66ce01 88 EndDisplay(); /// end of game display
wray2303 0:6eba0e66ce01 89 break;
wray2303 0:6eba0e66ce01 90 }
wray2303 0:6eba0e66ce01 91
wray2303 0:6eba0e66ce01 92 Sleep(); /// sleep function when nothings happening mbed can sleep and save power consumption
wray2303 0:6eba0e66ce01 93 }
wray2303 0:6eba0e66ce01 94 }
wray2303 0:6eba0e66ce01 95
wray2303 2:8d28a2f491eb 96
wray2303 2:8d28a2f491eb 97 void Read()
wray2303 2:8d28a2f491eb 98 {
wray2303 2:8d28a2f491eb 99 FILE *fp = fopen("/local/HSCORES.txt","r"); /// open file or create then open if not already made. and read value
wray2303 2:8d28a2f491eb 100 fscanf(fp, "%i", &HighScore); /// look in the local file system
wray2303 2:8d28a2f491eb 101 fclose(fp); /// close file
wray2303 2:8d28a2f491eb 102 }
wray2303 2:8d28a2f491eb 103
wray2303 2:8d28a2f491eb 104 void Write()
wray2303 2:8d28a2f491eb 105 {
wray2303 2:8d28a2f491eb 106 FILE *fp = fopen("/local/HSCORES.txt","w"); /// open file or create then open if not already made. make ready for writing to the file
wray2303 2:8d28a2f491eb 107 fprintf(fp, "%i\r\n", HighScore); /// print value to the local file
wray2303 2:8d28a2f491eb 108 fclose(fp); /// close file
wray2303 2:8d28a2f491eb 109 }
wray2303 2:8d28a2f491eb 110
wray2303 2:8d28a2f491eb 111
wray2303 0:6eba0e66ce01 112 void startUpDisplay()
wray2303 0:6eba0e66ce01 113 {
wray2303 0:6eba0e66ce01 114 lcd.init(); /// initialise the display
wray2303 0:6eba0e66ce01 115 lcd.clear();
wray2303 0:6eba0e66ce01 116 lcd.printString("Space-Teroid",6,1); /// opening message
wray2303 0:6eba0e66ce01 117 lcd.printString("Press S5 to",12,3);
wray2303 0:6eba0e66ce01 118 lcd.printString("Play",30,4);
wray2303 0:6eba0e66ce01 119 lcd.drawLine(0,0,0,47,1); /// draw outline
wray2303 0:6eba0e66ce01 120 lcd.drawLine(83,0,83,47,1);
wray2303 0:6eba0e66ce01 121 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 122 lcd.drawLine(0,47,83,47,1);
wray2303 0:6eba0e66ce01 123 lcd.refresh(); /// refesh LCD to create this display
wray2303 0:6eba0e66ce01 124
wray2303 0:6eba0e66ce01 125 while(!buttonP) { /// while the pause flag or pause button has not been set or used , the mbed will sleep saving power and allowing
wray2303 0:6eba0e66ce01 126 /// the user to start when they are ready
wray2303 0:6eba0e66ce01 127 sleep();
wray2303 0:6eba0e66ce01 128 }
wray2303 0:6eba0e66ce01 129 GenAsteroidFlag = 1;
wray2303 0:6eba0e66ce01 130 lcd.clear(); /// when flag has been set while loop has been broken and start up display will be escaped
wray2303 0:6eba0e66ce01 131 ship.shape(); /// the ships shape is drawn and and the game can begin
wray2303 0:6eba0e66ce01 132 }
wray2303 0:6eba0e66ce01 133
wray2303 2:8d28a2f491eb 134
wray2303 0:6eba0e66ce01 135 void Game()
wray2303 0:6eba0e66ce01 136 {
wray2303 0:6eba0e66ce01 137 lcd.drawLine(0,0,0,47,1); /// draw outline repeatedly when asteroid is falling
wray2303 0:6eba0e66ce01 138 lcd.drawLine(83,0,83,47,1);
wray2303 0:6eba0e66ce01 139 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 140 lcd.drawLine(0,47,83,47,1);
wray2303 2:8d28a2f491eb 141
wray2303 0:6eba0e66ce01 142 if (printFlag) { /// if print flag set, clear flag and use joysticks horizontal position
wray2303 0:6eba0e66ce01 143 printFlag = 0;
wray2303 0:6eba0e66ce01 144
wray2303 0:6eba0e66ce01 145 if (joystick.direction == LEFT && ship.xs>=7) { /// 7 represents the left most boundary
wray2303 0:6eba0e66ce01 146 ship.ShipLeft(); /// if joystick moves left, call ship moving left function
wray2303 0:6eba0e66ce01 147 }
wray2303 0:6eba0e66ce01 148 if (joystick.direction == RIGHT && ship.xs<=76) { /// 76 represents the right most boundary
wray2303 0:6eba0e66ce01 149 ship.ShipRight(); /// if joystick moves right, call the ship moving right function
wray2303 0:6eba0e66ce01 150 } else {} /// else do nothing
wray2303 0:6eba0e66ce01 151 }
wray2303 0:6eba0e66ce01 152
wray2303 0:6eba0e66ce01 153 if(button) {
wray2303 0:6eba0e66ce01 154 AsteroidCreatedFlag = 0;
wray2303 0:6eba0e66ce01 155 GenAsteroidFlag = 0;
wray2303 0:6eba0e66ce01 156 asteroid.destroyAsteroid();
wray2303 0:6eba0e66ce01 157 readTheAsteroid.detach();
wray2303 0:6eba0e66ce01 158 PWM1 = 0;
wray2303 0:6eba0e66ce01 159 state = 2;
wray2303 0:6eba0e66ce01 160
wray2303 0:6eba0e66ce01 161 } else {}
wray2303 0:6eba0e66ce01 162
wray2303 0:6eba0e66ce01 163 if(BulletExistsFlag==0) { /// check if there is already a bullet on the screen, a rule of the game one bullet at a time!
wray2303 0:6eba0e66ce01 164 if(buttonS) { /// if shoot button is pressed
wray2303 0:6eba0e66ce01 165
wray2303 0:6eba0e66ce01 166 shootSoundtimer.start(); /// start timer for shooting sound
wray2303 0:6eba0e66ce01 167 PWM1.period(2e-3); /// set the period of the PWM
wray2303 0:6eba0e66ce01 168 PWM1 = 0.5; /// set the duty cycle
wray2303 0:6eba0e66ce01 169 BulletExistsFlag=1; /// bullet exists flag set
wray2303 0:6eba0e66ce01 170 bullet.SetValues(ship.xs,ship.ys-1); /// set the bullets values according to where the ship is
wray2303 0:6eba0e66ce01 171
wray2303 0:6eba0e66ce01 172 bullet.collision(); /// chec for bullet collision
wray2303 0:6eba0e66ce01 173 if(bullet.collision()==true) {
wray2303 0:6eba0e66ce01 174 readShoot.detach();
wray2303 0:6eba0e66ce01 175 bullet.clearBullet();
wray2303 0:6eba0e66ce01 176 BulletExistsFlag=0;
wray2303 0:6eba0e66ce01 177 /** if bullet colision happens during sound being made then
wray2303 2:8d28a2f491eb 178 *buzzer doesnt stop, thisis because of the interrupt the button makes
wray2303 2:8d28a2f491eb 179 *here we reset the buzzer manually */
wray2303 0:6eba0e66ce01 180 shootSoundtimer.stop(); /// stop the timer
wray2303 0:6eba0e66ce01 181 shootSoundtimer.reset(); /// reset the timer
wray2303 0:6eba0e66ce01 182 PWM1 = 0.0; /// turn of the PWM
wray2303 0:6eba0e66ce01 183 }
wray2303 0:6eba0e66ce01 184
wray2303 0:6eba0e66ce01 185 else if(bullet.collision()==false) { /// if bullet co-ordinates are not off the screen
wray2303 0:6eba0e66ce01 186 bullet.createBullet(); /// create the bullet
wray2303 2:8d28a2f491eb 187 readShoot.attach(&ReadBullet,0.045); /// set ticker to read the position of the bullet
wray2303 0:6eba0e66ce01 188 } else {}
wray2303 0:6eba0e66ce01 189
wray2303 0:6eba0e66ce01 190 if(AsteroidCreatedFlag==1) { /// check if an asteroid has been created
wray2303 2:8d28a2f491eb 191 readTheAsteroid.attach(&ReadAsteroid,0.045); /// set ticker for asteroid movement
wray2303 0:6eba0e66ce01 192 } else {}
wray2303 0:6eba0e66ce01 193 } else {}
wray2303 0:6eba0e66ce01 194 }
wray2303 0:6eba0e66ce01 195
wray2303 0:6eba0e66ce01 196 if(GenAsteroidFlag == 1) { /// check if an asteroid is to be generated
wray2303 0:6eba0e66ce01 197 int A = rand() % 75 + 6; /// returns random value between 6 and 7
wray2303 0:6eba0e66ce01 198 asteroid.SetAsteroidValue(A,0); /// set asteroid position according to randomly generated value
wray2303 0:6eba0e66ce01 199 asteroid.createAsteroid(); /// create asteroid
wray2303 0:6eba0e66ce01 200 AsteroidCreatedFlag = 1; /// set flag to state an asteroid has been created and is on screen
wray2303 0:6eba0e66ce01 201 GenAsteroidFlag=0; /// reset Generate asteroid flag so no more asteroid are created
wray2303 0:6eba0e66ce01 202 if(AsteroidCreatedFlag==1) { /// if asteroid has been created
wray2303 2:8d28a2f491eb 203 readTheAsteroid.attach(&ReadAsteroid,0.045); /// set ticker to read and move asteroid
wray2303 0:6eba0e66ce01 204 } else {}
wray2303 2:8d28a2f491eb 205 } else {}
wray2303 2:8d28a2f491eb 206 }
wray2303 0:6eba0e66ce01 207
wray2303 0:6eba0e66ce01 208
wray2303 0:6eba0e66ce01 209 void ReadAsteroid()
wray2303 0:6eba0e66ce01 210 {
wray2303 0:6eba0e66ce01 211 bullet.collision(); /// check if the bullet has made a collision this must mean an asteroid has been shot
wray2303 0:6eba0e66ce01 212 if(bullet.collision()==false) { /// if no collision and the asteroid y position is less than the end of the screen move the asteroid
wray2303 0:6eba0e66ce01 213 asteroid.ya=asteroid.ya+1; /// move asteroids y position down the screen
wray2303 0:6eba0e66ce01 214 asteroid.erasePrevAsteroid(); /// erase the previous asteroid
wray2303 0:6eba0e66ce01 215 asteroid.createAsteroid(); /// create the asteroid in its new position
wray2303 0:6eba0e66ce01 216 } else {}
wray2303 0:6eba0e66ce01 217
wray2303 0:6eba0e66ce01 218 ////////// using a double check seems to make the game run smoother //////////////
wray2303 0:6eba0e66ce01 219 bullet.collision(); /// check if the bullet has made a collision this must mean an asteroid has been shot
wray2303 0:6eba0e66ce01 220 if(bullet.collision()==true) { /// if a collision is made
wray2303 0:6eba0e66ce01 221 BulletExistsFlag=0; /// reset the bullet exists flag
wray2303 0:6eba0e66ce01 222 readShoot.detach();
wray2303 0:6eba0e66ce01 223 readTheAsteroid.detach(); /// detach the read asteroid function from the ticker
wray2303 0:6eba0e66ce01 224 bullet.clearBullet(); /// clear the bullet from the screen
wray2303 0:6eba0e66ce01 225 AsteroidCreatedFlag=0; /// reset Asteroid created Flag to state there is no longer an asteroid on screen
wray2303 0:6eba0e66ce01 226 GenAsteroidFlag=1; /// set the Generate asteroid flag
wray2303 0:6eba0e66ce01 227 asteroid.destroyAsteroid(); /// destroy on screen asteroid
wray2303 2:8d28a2f491eb 228
wray2303 2:8d28a2f491eb 229 /** if bullet colision happens during sound being made then
wray2303 2:8d28a2f491eb 230 buzzer doesnt stop, thisis because of the interrupt the button makes
wray2303 2:8d28a2f491eb 231 here we reset the buzzer manually*/
wray2303 2:8d28a2f491eb 232 shootSoundtimer.stop(); /// stop the timer
wray2303 2:8d28a2f491eb 233 shootSoundtimer.reset(); /// reset the timer
wray2303 2:8d28a2f491eb 234 PWM1 = 0.0; /// turn of the PWM
wray2303 0:6eba0e66ce01 235 } else {}
wray2303 0:6eba0e66ce01 236
wray2303 0:6eba0e66ce01 237 asteroid.armageddon(); /// check if asteroid has reached the bottom of the screen
wray2303 0:6eba0e66ce01 238 if(asteroid.armageddon() == true) { /// if asteroid has it the bottom of the screen ... RUN!!!
wray2303 0:6eba0e66ce01 239 /// armageddon takes place and the game is over
wray2303 0:6eba0e66ce01 240 bullet.clearBullet(); /// clear the existing bullet
wray2303 0:6eba0e66ce01 241 readTheAsteroid.detach(); /// detach the read asteroid function
wray2303 0:6eba0e66ce01 242 asteroid.destroyAsteroid(); /// destroy asteroid in place
wray2303 0:6eba0e66ce01 243 asteroid.nscore=asteroid.nscore-1; /// minus one point for the asteroid that was detroyed in losing
wray2303 0:6eba0e66ce01 244 state=3; /// go to state 3 (end screen)
wray2303 0:6eba0e66ce01 245 } else {}
wray2303 0:6eba0e66ce01 246
wray2303 2:8d28a2f491eb 247 lcd.drawLine(0,0,83,0,1); /// keep re-drawing top line as this is damaged while asteroid moves accross it
wray2303 0:6eba0e66ce01 248 }
wray2303 0:6eba0e66ce01 249
wray2303 2:8d28a2f491eb 250
wray2303 0:6eba0e66ce01 251 void ReadBullet()
wray2303 0:6eba0e66ce01 252 {
wray2303 2:8d28a2f491eb 253 if(shootSoundtimer.read() > 0.1) { /// check when the sound timer reaches 0.1 sec
wray2303 0:6eba0e66ce01 254 shootSoundtimer.stop(); /// stop the timer
wray2303 0:6eba0e66ce01 255 shootSoundtimer.reset(); /// reset the timer
wray2303 0:6eba0e66ce01 256 PWM1 = 0.0; /// turn of the PWM
wray2303 0:6eba0e66ce01 257 } else {}
wray2303 0:6eba0e66ce01 258
wray2303 0:6eba0e66ce01 259 if(button) {
wray2303 0:6eba0e66ce01 260 AsteroidCreatedFlag = 0;
wray2303 0:6eba0e66ce01 261 GenAsteroidFlag = 0;
wray2303 0:6eba0e66ce01 262 asteroid.destroyAsteroid();
wray2303 0:6eba0e66ce01 263 readTheAsteroid.detach();
wray2303 0:6eba0e66ce01 264 PWM1 = 0;
wray2303 0:6eba0e66ce01 265 state = 2;
wray2303 0:6eba0e66ce01 266
wray2303 0:6eba0e66ce01 267 } else {}
wray2303 0:6eba0e66ce01 268 bullet.collision(); /// check if the bullet has collided with anything
wray2303 0:6eba0e66ce01 269 bullet.offScreen(); /// check if the bullet is off screen
wray2303 0:6eba0e66ce01 270 if (bullet.collision() == true||bullet.offScreen()==true) { /// if a collision is made or the bullet goes off the screen
wray2303 0:6eba0e66ce01 271 BulletExistsFlag=0;
wray2303 0:6eba0e66ce01 272 bullet.clearBullet(); /// clear the bullet
wray2303 0:6eba0e66ce01 273 readShoot.detach(); /// detach the ticker
wray2303 0:6eba0e66ce01 274
wray2303 0:6eba0e66ce01 275 /** if bullet colision happens during sound being made then
wray2303 0:6eba0e66ce01 276 buzzer doesnt stop, thisis because of the interrupt the button makes
wray2303 0:6eba0e66ce01 277 here we reset the buzzer manually*/
wray2303 0:6eba0e66ce01 278 shootSoundtimer.stop(); /// stop the timer
wray2303 0:6eba0e66ce01 279 shootSoundtimer.reset(); /// reset the timer
wray2303 0:6eba0e66ce01 280 PWM1 = 0.0; /// turn of the PWM
wray2303 0:6eba0e66ce01 281 } else {}
wray2303 0:6eba0e66ce01 282
wray2303 0:6eba0e66ce01 283 bullet.y=bullet.y-1; /// move the bullet up i.e. move its y coordinate
wray2303 0:6eba0e66ce01 284 bullet.createBullet(); /// draw new bullet
wray2303 0:6eba0e66ce01 285 bullet.erasePrevBullet(); /// erase previous position
wray2303 0:6eba0e66ce01 286
wray2303 0:6eba0e66ce01 287 ////////// double check this seems to help rid some bugs ////////////
wray2303 0:6eba0e66ce01 288 bullet.offScreen(); /// check if the bullet is off screen
wray2303 0:6eba0e66ce01 289 bullet.collision(); /// check if the bullet has collided with anything
wray2303 0:6eba0e66ce01 290 if (bullet.collision() == true||bullet.offScreen()==true) { /// if a collision is made or the bullet goes off the screen
wray2303 0:6eba0e66ce01 291 BulletExistsFlag=0;
wray2303 0:6eba0e66ce01 292 bullet.clearBullet(); /// clear the bullet
wray2303 0:6eba0e66ce01 293 readShoot.detach(); /// detach the ticker
wray2303 0:6eba0e66ce01 294 } else {}
wray2303 0:6eba0e66ce01 295
wray2303 0:6eba0e66ce01 296 if (printFlag) { /// here we are checking the position of the joystick, so the user can move while the bullet is shooting else where
wray2303 0:6eba0e66ce01 297 printFlag = 0;
wray2303 0:6eba0e66ce01 298 if (joystick.direction == LEFT && ship.xs>=7) {
wray2303 0:6eba0e66ce01 299 ship.ShipLeft();
wray2303 0:6eba0e66ce01 300 }
wray2303 0:6eba0e66ce01 301 if (joystick.direction == RIGHT && ship.xs<=76) {
wray2303 0:6eba0e66ce01 302 ship.ShipRight();
wray2303 0:6eba0e66ce01 303 } else {}
wray2303 0:6eba0e66ce01 304 }
wray2303 0:6eba0e66ce01 305 }
wray2303 0:6eba0e66ce01 306
wray2303 2:8d28a2f491eb 307
wray2303 0:6eba0e66ce01 308 void EndDisplay()
wray2303 0:6eba0e66ce01 309 {
wray2303 0:6eba0e66ce01 310 lcd.clear(); /// clear entire display to make way for end screen
wray2303 0:6eba0e66ce01 311 asteroid.nscore = asteroid.nscore-PauseNumber; /// takes points off for the asteroid destroyed when pausing game and destroy asteroid command sent
wray2303 0:6eba0e66ce01 312 char buffer[14]; /// set buffer to 14, screen 84 pixels long , 6 pixel per char , 84/6 = 14
wray2303 0:6eba0e66ce01 313 int score = sprintf(buffer,"Score: %d",asteroid.nscore); /// print formatted data to buffer
wray2303 0:6eba0e66ce01 314 if (score <= 14) { /// if string will fit on display
wray2303 2:8d28a2f491eb 315 lcd.printString(buffer,20,5); /// display on screen
wray2303 0:6eba0e66ce01 316 }
wray2303 0:6eba0e66ce01 317
wray2303 2:8d28a2f491eb 318 Read(); /// read file to compare values
wray2303 2:8d28a2f491eb 319 if(asteroid.nscore>HighScore) { /// compare current score against highscore
wray2303 2:8d28a2f491eb 320 HighScore = asteroid.nscore; /// if current score is larger than highscore = current score
wray2303 2:8d28a2f491eb 321 Write(); /// write this to file
wray2303 2:8d28a2f491eb 322 }
wray2303 2:8d28a2f491eb 323 Read(); /// read the file to then print to lcd
wray2303 2:8d28a2f491eb 324 char buffer1[14]; /// set buffer to 14, screen 84 pixels long , 6 pixel per char , 84/6 = 14
wray2303 2:8d28a2f491eb 325 int Hscore = sprintf(buffer1,"HighScore:%d",HighScore); /// print formatted data to buffer
wray2303 0:6eba0e66ce01 326
wray2303 2:8d28a2f491eb 327 if (Hscore <= 14) { /// if string will fit on display
wray2303 2:8d28a2f491eb 328 lcd.printString(buffer1,7,4); /// display on screen
wray2303 2:8d28a2f491eb 329 }
wray2303 2:8d28a2f491eb 330 lcd.printString("game over !",12,0); /// closing message
wray2303 2:8d28a2f491eb 331 lcd.printString("Press S4 to",10,1);
wray2303 2:8d28a2f491eb 332 lcd.printString("Play again",12,2);
wray2303 0:6eba0e66ce01 333 lcd.drawLine(0,0,0,47,1); /// draw outline
wray2303 0:6eba0e66ce01 334 lcd.drawLine(83,0,83,47,1);
wray2303 0:6eba0e66ce01 335 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 336 lcd.drawLine(0,47,83,47,1);
wray2303 0:6eba0e66ce01 337 lcd.refresh(); /// refesh LCD to create this display
wray2303 0:6eba0e66ce01 338
wray2303 0:6eba0e66ce01 339 while(!buttonS) { /// while button b has not been pressed sleep (save power)
wray2303 0:6eba0e66ce01 340 Sleep();
wray2303 0:6eba0e66ce01 341 }
wray2303 0:6eba0e66ce01 342 /// when button s pressed
wray2303 0:6eba0e66ce01 343 state = 0; /// set the state to 0 (intital game display
wray2303 0:6eba0e66ce01 344 asteroid.nscore=0; /// reset the score
wray2303 0:6eba0e66ce01 345 }
wray2303 0:6eba0e66ce01 346
wray2303 2:8d28a2f491eb 347
wray2303 0:6eba0e66ce01 348 void PauseDisplay()
wray2303 0:6eba0e66ce01 349 {
wray2303 0:6eba0e66ce01 350 lcd.printString("PAUSE",28,1); /// print pause display message
wray2303 0:6eba0e66ce01 351 lcd.printString("press S5 to",11,3);
wray2303 0:6eba0e66ce01 352 lcd.printString("resume",24,4);
wray2303 0:6eba0e66ce01 353 lcd.drawLine(0,0,0,47,1); /// draw outline
wray2303 0:6eba0e66ce01 354 lcd.drawLine(83,0,83,47,1);
wray2303 0:6eba0e66ce01 355 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 356 lcd.drawLine(0,47,83,47,1);
wray2303 0:6eba0e66ce01 357 lcd.refresh();
wray2303 0:6eba0e66ce01 358 while(!buttonP) { /// while button P is not pressed Sleep() to save power
wray2303 0:6eba0e66ce01 359 Sleep();
wray2303 0:6eba0e66ce01 360 if(buttonP) { /// if button P is pressed
wray2303 0:6eba0e66ce01 361 lcd.clear(); /// clear LCD
wray2303 0:6eba0e66ce01 362 ship.shape(); /// draw the ship back on in position it was left
wray2303 0:6eba0e66ce01 363 GenAsteroidFlag = 1; /// reset the Generate asteroid flag
wray2303 0:6eba0e66ce01 364 PauseNumber = PauseNumber+1; /// increment pause amount counter
wray2303 0:6eba0e66ce01 365 state=1; /// state = 1 means back to the game in the switch statement
wray2303 0:6eba0e66ce01 366 }
wray2303 0:6eba0e66ce01 367 }
wray2303 0:6eba0e66ce01 368 }
wray2303 0:6eba0e66ce01 369
wray2303 2:8d28a2f491eb 370
wray2303 0:6eba0e66ce01 371 void calibrateJoystick()
wray2303 0:6eba0e66ce01 372 {
wray2303 0:6eba0e66ce01 373 button.mode(PullDown);
wray2303 0:6eba0e66ce01 374 /// must not move during calibration
wray2303 0:6eba0e66ce01 375 joystick.x0 = xPot; /// initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
wray2303 0:6eba0e66ce01 376 joystick.y0 = yPot;
wray2303 0:6eba0e66ce01 377 }
wray2303 0:6eba0e66ce01 378
wray2303 2:8d28a2f491eb 379
wray2303 0:6eba0e66ce01 380 void updateJoystick()
wray2303 0:6eba0e66ce01 381 {
wray2303 0:6eba0e66ce01 382 /// reading the joystick position 0.0 is centered returns a value in the range -0.5 and 0.5
wray2303 0:6eba0e66ce01 383 joystick.x = xPot - joystick.x0;
wray2303 0:6eba0e66ce01 384 joystick.y = yPot - joystick.y0;
wray2303 0:6eba0e66ce01 385 /// read button state
wray2303 0:6eba0e66ce01 386 joystick.button = button;
wray2303 0:6eba0e66ce01 387
wray2303 0:6eba0e66ce01 388 /// calculate direction depending on x,y values
wray2303 0:6eba0e66ce01 389 /// tolerance allows a little lee-way in case joystick not exactly in the stated direction
wray2303 0:6eba0e66ce01 390 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 391 joystick.direction = CENTRE;
wray2303 0:6eba0e66ce01 392 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 393 joystick.direction = UP;
wray2303 0:6eba0e66ce01 394 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 395 joystick.direction = DOWN;
wray2303 0:6eba0e66ce01 396 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 397 joystick.direction = RIGHT;
wray2303 0:6eba0e66ce01 398 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 399 joystick.direction = LEFT;
wray2303 0:6eba0e66ce01 400 } else {
wray2303 0:6eba0e66ce01 401 joystick.direction = UNKNOWN;
wray2303 0:6eba0e66ce01 402 }
wray2303 0:6eba0e66ce01 403 /// set flag for use of the joystick position
wray2303 0:6eba0e66ce01 404 printFlag = 1;
wray2303 0:6eba0e66ce01 405 }