A project that creates falling asteroids at random positions along the top of the screen for a space ship at the bottom to shoot, using a Nokia N5110 LCD, joystick and pushbuttons - A revised name from my earlier project L2_2645

Dependencies:   DebounceIn N5110 PowerControl mbed

Fork of L2_2645_project by Louis Wray

Committer:
wray2303
Date:
Fri May 08 09:24:50 2015 +0000
Revision:
0:6eba0e66ce01
Child:
2:8d28a2f491eb
pre-update

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 0:6eba0e66ce01 55 int PauseNumber = 0;
wray2303 0:6eba0e66ce01 56
wray2303 0:6eba0e66ce01 57 int main()
wray2303 0:6eba0e66ce01 58 {
wray2303 0:6eba0e66ce01 59 asteroid.nscore = 0;
wray2303 0:6eba0e66ce01 60 srand(15000*randomPin); /// seeding random function
wray2303 0:6eba0e66ce01 61 calibrateJoystick(); /// set's centred values of joystick
wray2303 0:6eba0e66ce01 62 pollJoystick.attach(&updateJoystick,1/45.0); /// read joystick 45 times per second
wray2303 0:6eba0e66ce01 63 ship.SetValues(42,39); /// initialise ships starting x,y coordinates
wray2303 0:6eba0e66ce01 64
wray2303 0:6eba0e66ce01 65 PHY_PowerDown(); /// power down ethernet connection
wray2303 0:6eba0e66ce01 66
wray2303 0:6eba0e66ce01 67 while(1) {
wray2303 0:6eba0e66ce01 68
wray2303 0:6eba0e66ce01 69 switch(state) { /// switch statement this enables the game to switch between states giving a start state
wray2303 0:6eba0e66ce01 70 /// a game play state and subsequently a game over state giving choice to start again.
wray2303 0:6eba0e66ce01 71
wray2303 0:6eba0e66ce01 72 case 0:
wray2303 0:6eba0e66ce01 73 startUpDisplay(); /// call start up display function
wray2303 0:6eba0e66ce01 74 state=1;
wray2303 0:6eba0e66ce01 75 break;
wray2303 0:6eba0e66ce01 76
wray2303 0:6eba0e66ce01 77 case 1:
wray2303 0:6eba0e66ce01 78 Game(); /// game play
wray2303 0:6eba0e66ce01 79 break;
wray2303 0:6eba0e66ce01 80
wray2303 0:6eba0e66ce01 81 case 2:
wray2303 0:6eba0e66ce01 82 lcd.clear();
wray2303 0:6eba0e66ce01 83 PauseDisplay();
wray2303 0:6eba0e66ce01 84 break;
wray2303 0:6eba0e66ce01 85
wray2303 0:6eba0e66ce01 86 case 3:
wray2303 0:6eba0e66ce01 87 EndDisplay(); /// end of game display
wray2303 0:6eba0e66ce01 88 break;
wray2303 0:6eba0e66ce01 89 }
wray2303 0:6eba0e66ce01 90
wray2303 0:6eba0e66ce01 91 Sleep(); /// sleep function when nothings happening mbed can sleep and save power consumption
wray2303 0:6eba0e66ce01 92 }
wray2303 0:6eba0e66ce01 93 }
wray2303 0:6eba0e66ce01 94
wray2303 0:6eba0e66ce01 95 void startUpDisplay()
wray2303 0:6eba0e66ce01 96 {
wray2303 0:6eba0e66ce01 97 lcd.init(); /// initialise the display
wray2303 0:6eba0e66ce01 98 lcd.clear();
wray2303 0:6eba0e66ce01 99 lcd.printString("Space-Teroid",6,1); /// opening message
wray2303 0:6eba0e66ce01 100 lcd.printString("Press S5 to",12,3);
wray2303 0:6eba0e66ce01 101 lcd.printString("Play",30,4);
wray2303 0:6eba0e66ce01 102 lcd.drawLine(0,0,0,47,1); /// draw outline
wray2303 0:6eba0e66ce01 103 lcd.drawLine(83,0,83,47,1);
wray2303 0:6eba0e66ce01 104 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 105 lcd.drawLine(0,47,83,47,1);
wray2303 0:6eba0e66ce01 106 lcd.refresh(); /// refesh LCD to create this display
wray2303 0:6eba0e66ce01 107
wray2303 0:6eba0e66ce01 108 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 109 /// the user to start when they are ready
wray2303 0:6eba0e66ce01 110 sleep();
wray2303 0:6eba0e66ce01 111 }
wray2303 0:6eba0e66ce01 112 GenAsteroidFlag = 1;
wray2303 0:6eba0e66ce01 113 lcd.clear(); /// when flag has been set while loop has been broken and start up display will be escaped
wray2303 0:6eba0e66ce01 114 ship.shape(); /// the ships shape is drawn and and the game can begin
wray2303 0:6eba0e66ce01 115 }
wray2303 0:6eba0e66ce01 116
wray2303 0:6eba0e66ce01 117 void Game()
wray2303 0:6eba0e66ce01 118 {
wray2303 0:6eba0e66ce01 119 lcd.drawLine(0,0,0,47,1); /// draw outline repeatedly when asteroid is falling
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
wray2303 0:6eba0e66ce01 124 if (printFlag) { /// if print flag set, clear flag and use joysticks horizontal position
wray2303 0:6eba0e66ce01 125 printFlag = 0;
wray2303 0:6eba0e66ce01 126
wray2303 0:6eba0e66ce01 127 if (joystick.direction == LEFT && ship.xs>=7) { /// 7 represents the left most boundary
wray2303 0:6eba0e66ce01 128 ship.ShipLeft(); /// if joystick moves left, call ship moving left function
wray2303 0:6eba0e66ce01 129 }
wray2303 0:6eba0e66ce01 130 if (joystick.direction == RIGHT && ship.xs<=76) { /// 76 represents the right most boundary
wray2303 0:6eba0e66ce01 131 ship.ShipRight(); /// if joystick moves right, call the ship moving right function
wray2303 0:6eba0e66ce01 132 } else {} /// else do nothing
wray2303 0:6eba0e66ce01 133 }
wray2303 0:6eba0e66ce01 134
wray2303 0:6eba0e66ce01 135 if(button) {
wray2303 0:6eba0e66ce01 136 AsteroidCreatedFlag = 0;
wray2303 0:6eba0e66ce01 137 GenAsteroidFlag = 0;
wray2303 0:6eba0e66ce01 138 asteroid.destroyAsteroid();
wray2303 0:6eba0e66ce01 139 readTheAsteroid.detach();
wray2303 0:6eba0e66ce01 140 PWM1 = 0;
wray2303 0:6eba0e66ce01 141 state = 2;
wray2303 0:6eba0e66ce01 142
wray2303 0:6eba0e66ce01 143 } else {}
wray2303 0:6eba0e66ce01 144
wray2303 0:6eba0e66ce01 145
wray2303 0:6eba0e66ce01 146 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 147 if(buttonS) { /// if shoot button is pressed
wray2303 0:6eba0e66ce01 148
wray2303 0:6eba0e66ce01 149 shootSoundtimer.start(); /// start timer for shooting sound
wray2303 0:6eba0e66ce01 150 PWM1.period(2e-3); /// set the period of the PWM
wray2303 0:6eba0e66ce01 151 PWM1 = 0.5; /// set the duty cycle
wray2303 0:6eba0e66ce01 152 BulletExistsFlag=1; /// bullet exists flag set
wray2303 0:6eba0e66ce01 153 bullet.SetValues(ship.xs,ship.ys-1); /// set the bullets values according to where the ship is
wray2303 0:6eba0e66ce01 154
wray2303 0:6eba0e66ce01 155 bullet.collision(); /// chec for bullet collision
wray2303 0:6eba0e66ce01 156 if(bullet.collision()==true) {
wray2303 0:6eba0e66ce01 157 readShoot.detach();
wray2303 0:6eba0e66ce01 158 bullet.clearBullet();
wray2303 0:6eba0e66ce01 159 BulletExistsFlag=0;
wray2303 0:6eba0e66ce01 160
wray2303 0:6eba0e66ce01 161 /** if bullet colision happens during sound being made then
wray2303 0:6eba0e66ce01 162 buzzer doesnt stop, thisis because of the interrupt the button makes
wray2303 0:6eba0e66ce01 163 here we reset the buzzer manually */
wray2303 0:6eba0e66ce01 164 shootSoundtimer.stop(); /// stop the timer
wray2303 0:6eba0e66ce01 165 shootSoundtimer.reset(); /// reset the timer
wray2303 0:6eba0e66ce01 166 PWM1 = 0.0; /// turn of the PWM
wray2303 0:6eba0e66ce01 167 }
wray2303 0:6eba0e66ce01 168
wray2303 0:6eba0e66ce01 169 else if(bullet.collision()==false) { /// if bullet co-ordinates are not off the screen
wray2303 0:6eba0e66ce01 170 bullet.createBullet(); /// create the bullet
wray2303 0:6eba0e66ce01 171 readShoot.attach(&ReadBullet,0.05); /// set ticker to read the position of the bullet
wray2303 0:6eba0e66ce01 172 } else {}
wray2303 0:6eba0e66ce01 173
wray2303 0:6eba0e66ce01 174 if(AsteroidCreatedFlag==1) { /// check if an asteroid has been created
wray2303 0:6eba0e66ce01 175 readTheAsteroid.attach(&ReadAsteroid,0.05); /// set ticker for asteroid movement
wray2303 0:6eba0e66ce01 176 } else {}
wray2303 0:6eba0e66ce01 177 } else {}
wray2303 0:6eba0e66ce01 178 }
wray2303 0:6eba0e66ce01 179
wray2303 0:6eba0e66ce01 180 if(GenAsteroidFlag == 1) { /// check if an asteroid is to be generated
wray2303 0:6eba0e66ce01 181 int A = rand() % 75 + 6; /// returns random value between 6 and 7
wray2303 0:6eba0e66ce01 182 asteroid.SetAsteroidValue(A,0); /// set asteroid position according to randomly generated value
wray2303 0:6eba0e66ce01 183 asteroid.createAsteroid(); /// create asteroid
wray2303 0:6eba0e66ce01 184 AsteroidCreatedFlag = 1; /// set flag to state an asteroid has been created and is on screen
wray2303 0:6eba0e66ce01 185 GenAsteroidFlag=0; /// reset Generate asteroid flag so no more asteroid are created
wray2303 0:6eba0e66ce01 186 if(AsteroidCreatedFlag==1) { /// if asteroid has been created
wray2303 0:6eba0e66ce01 187 readTheAsteroid.attach(&ReadAsteroid,0.05); /// set ticker to read and move asteroid
wray2303 0:6eba0e66ce01 188 } else {}
wray2303 0:6eba0e66ce01 189
wray2303 0:6eba0e66ce01 190 } else {}
wray2303 0:6eba0e66ce01 191
wray2303 0:6eba0e66ce01 192 }
wray2303 0:6eba0e66ce01 193
wray2303 0:6eba0e66ce01 194 void ReadAsteroid()
wray2303 0:6eba0e66ce01 195 {
wray2303 0:6eba0e66ce01 196
wray2303 0:6eba0e66ce01 197 bullet.collision(); /// check if the bullet has made a collision this must mean an asteroid has been shot
wray2303 0:6eba0e66ce01 198 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 199 asteroid.ya=asteroid.ya+1; /// move asteroids y position down the screen
wray2303 0:6eba0e66ce01 200 asteroid.erasePrevAsteroid(); /// erase the previous asteroid
wray2303 0:6eba0e66ce01 201 asteroid.createAsteroid(); /// create the asteroid in its new position
wray2303 0:6eba0e66ce01 202 } else {}
wray2303 0:6eba0e66ce01 203
wray2303 0:6eba0e66ce01 204 ////////// using a double check seems to make the game run smoother //////////////
wray2303 0:6eba0e66ce01 205 bullet.collision(); /// check if the bullet has made a collision this must mean an asteroid has been shot
wray2303 0:6eba0e66ce01 206 if(bullet.collision()==true) { /// if a collision is made
wray2303 0:6eba0e66ce01 207 BulletExistsFlag=0; /// reset the bullet exists flag
wray2303 0:6eba0e66ce01 208 readShoot.detach();
wray2303 0:6eba0e66ce01 209 readTheAsteroid.detach(); /// detach the read asteroid function from the ticker
wray2303 0:6eba0e66ce01 210 bullet.clearBullet(); /// clear the bullet from the screen
wray2303 0:6eba0e66ce01 211 AsteroidCreatedFlag=0; /// reset Asteroid created Flag to state there is no longer an asteroid on screen
wray2303 0:6eba0e66ce01 212 GenAsteroidFlag=1; /// set the Generate asteroid flag
wray2303 0:6eba0e66ce01 213 asteroid.destroyAsteroid(); /// destroy on screen asteroid
wray2303 0:6eba0e66ce01 214 } else {}
wray2303 0:6eba0e66ce01 215
wray2303 0:6eba0e66ce01 216 asteroid.armageddon(); /// check if asteroid has reached the bottom of the screen
wray2303 0:6eba0e66ce01 217 if(asteroid.armageddon() == true) { /// if asteroid has it the bottom of the screen ... RUN!!!
wray2303 0:6eba0e66ce01 218 /// armageddon takes place and the game is over
wray2303 0:6eba0e66ce01 219 bullet.clearBullet(); /// clear the existing bullet
wray2303 0:6eba0e66ce01 220 readTheAsteroid.detach(); /// detach the read asteroid function
wray2303 0:6eba0e66ce01 221 asteroid.destroyAsteroid(); /// destroy asteroid in place
wray2303 0:6eba0e66ce01 222 asteroid.nscore=asteroid.nscore-1; /// minus one point for the asteroid that was detroyed in losing
wray2303 0:6eba0e66ce01 223 state=3; /// go to state 3 (end screen)
wray2303 0:6eba0e66ce01 224 } else {}
wray2303 0:6eba0e66ce01 225
wray2303 0:6eba0e66ce01 226 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 227
wray2303 0:6eba0e66ce01 228 }
wray2303 0:6eba0e66ce01 229
wray2303 0:6eba0e66ce01 230 void ReadBullet()
wray2303 0:6eba0e66ce01 231 {
wray2303 0:6eba0e66ce01 232 if(shootSoundtimer.read() > 0.1) { /// check when the sound timer reaches 0.1 sec
wray2303 0:6eba0e66ce01 233 shootSoundtimer.stop(); /// stop the timer
wray2303 0:6eba0e66ce01 234 shootSoundtimer.reset(); /// reset the timer
wray2303 0:6eba0e66ce01 235 PWM1 = 0.0; /// turn of the PWM
wray2303 0:6eba0e66ce01 236 } else {}
wray2303 0:6eba0e66ce01 237
wray2303 0:6eba0e66ce01 238 if(button) {
wray2303 0:6eba0e66ce01 239 AsteroidCreatedFlag = 0;
wray2303 0:6eba0e66ce01 240 GenAsteroidFlag = 0;
wray2303 0:6eba0e66ce01 241 asteroid.destroyAsteroid();
wray2303 0:6eba0e66ce01 242 readTheAsteroid.detach();
wray2303 0:6eba0e66ce01 243 PWM1 = 0;
wray2303 0:6eba0e66ce01 244 state = 2;
wray2303 0:6eba0e66ce01 245
wray2303 0:6eba0e66ce01 246 } else {}
wray2303 0:6eba0e66ce01 247 bullet.collision(); /// check if the bullet has collided with anything
wray2303 0:6eba0e66ce01 248 bullet.offScreen(); /// check if the bullet is off screen
wray2303 0:6eba0e66ce01 249 if (bullet.collision() == true||bullet.offScreen()==true) { /// if a collision is made or the bullet goes off the screen
wray2303 0:6eba0e66ce01 250 BulletExistsFlag=0;
wray2303 0:6eba0e66ce01 251 bullet.clearBullet(); /// clear the bullet
wray2303 0:6eba0e66ce01 252 readShoot.detach(); /// detach the ticker
wray2303 0:6eba0e66ce01 253
wray2303 0:6eba0e66ce01 254 /** if bullet colision happens during sound being made then
wray2303 0:6eba0e66ce01 255 buzzer doesnt stop, thisis because of the interrupt the button makes
wray2303 0:6eba0e66ce01 256 here we reset the buzzer manually*/
wray2303 0:6eba0e66ce01 257 shootSoundtimer.stop(); /// stop the timer
wray2303 0:6eba0e66ce01 258 shootSoundtimer.reset(); /// reset the timer
wray2303 0:6eba0e66ce01 259 PWM1 = 0.0; /// turn of the PWM
wray2303 0:6eba0e66ce01 260 } else {}
wray2303 0:6eba0e66ce01 261
wray2303 0:6eba0e66ce01 262 bullet.y=bullet.y-1; /// move the bullet up i.e. move its y coordinate
wray2303 0:6eba0e66ce01 263 bullet.createBullet(); /// draw new bullet
wray2303 0:6eba0e66ce01 264 bullet.erasePrevBullet(); /// erase previous position
wray2303 0:6eba0e66ce01 265
wray2303 0:6eba0e66ce01 266 ////////// double check this seems to help rid some bugs ////////////
wray2303 0:6eba0e66ce01 267
wray2303 0:6eba0e66ce01 268 bullet.offScreen(); /// check if the bullet is off screen
wray2303 0:6eba0e66ce01 269 bullet.collision(); /// check if the bullet has collided with anything
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 } else {}
wray2303 0:6eba0e66ce01 275
wray2303 0:6eba0e66ce01 276
wray2303 0:6eba0e66ce01 277 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 278 printFlag = 0;
wray2303 0:6eba0e66ce01 279 if (joystick.direction == LEFT && ship.xs>=7) {
wray2303 0:6eba0e66ce01 280 ship.ShipLeft();
wray2303 0:6eba0e66ce01 281 }
wray2303 0:6eba0e66ce01 282 if (joystick.direction == RIGHT && ship.xs<=76) {
wray2303 0:6eba0e66ce01 283 ship.ShipRight();
wray2303 0:6eba0e66ce01 284 } else {}
wray2303 0:6eba0e66ce01 285 }
wray2303 0:6eba0e66ce01 286 }
wray2303 0:6eba0e66ce01 287
wray2303 0:6eba0e66ce01 288 void EndDisplay()
wray2303 0:6eba0e66ce01 289 {
wray2303 0:6eba0e66ce01 290 lcd.clear(); /// clear entire display to make way for end screen
wray2303 0:6eba0e66ce01 291 asteroid.nscore = asteroid.nscore-PauseNumber; /// takes points off for the asteroid destroyed when pausing game and destroy asteroid command sent
wray2303 0:6eba0e66ce01 292 char buffer[14]; /// set buffer to 14, screen 84 pixels long , 6 pixel per char , 84/6 = 14
wray2303 0:6eba0e66ce01 293 int score = sprintf(buffer,"Score: %d",asteroid.nscore); /// print formatted data to buffer
wray2303 0:6eba0e66ce01 294 if (score <= 14) { /// if string will fit on display
wray2303 0:6eba0e66ce01 295 lcd.printString(buffer,10,5); /// display on screen
wray2303 0:6eba0e66ce01 296 }
wray2303 0:6eba0e66ce01 297
wray2303 0:6eba0e66ce01 298
wray2303 0:6eba0e66ce01 299
wray2303 0:6eba0e66ce01 300 lcd.printString("Game Over !",12,1); /// closing message
wray2303 0:6eba0e66ce01 301 lcd.printString("Press S4 to",11,2);
wray2303 0:6eba0e66ce01 302 lcd.printString("Play again",12,3);
wray2303 0:6eba0e66ce01 303 lcd.drawLine(0,0,0,47,1); /// draw outline
wray2303 0:6eba0e66ce01 304 lcd.drawLine(83,0,83,47,1);
wray2303 0:6eba0e66ce01 305 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 306 lcd.drawLine(0,47,83,47,1);
wray2303 0:6eba0e66ce01 307 lcd.refresh(); /// refesh LCD to create this display
wray2303 0:6eba0e66ce01 308
wray2303 0:6eba0e66ce01 309 while(!buttonS) { /// while button b has not been pressed sleep (save power)
wray2303 0:6eba0e66ce01 310 Sleep();
wray2303 0:6eba0e66ce01 311 }
wray2303 0:6eba0e66ce01 312 /// when button s pressed
wray2303 0:6eba0e66ce01 313 state = 0; /// set the state to 0 (intital game display
wray2303 0:6eba0e66ce01 314 asteroid.nscore=0; /// reset the score
wray2303 0:6eba0e66ce01 315 }
wray2303 0:6eba0e66ce01 316
wray2303 0:6eba0e66ce01 317 void PauseDisplay()
wray2303 0:6eba0e66ce01 318 {
wray2303 0:6eba0e66ce01 319 lcd.printString("PAUSE",28,1); /// print pause display message
wray2303 0:6eba0e66ce01 320 lcd.printString("press S5 to",11,3);
wray2303 0:6eba0e66ce01 321 lcd.printString("resume",24,4);
wray2303 0:6eba0e66ce01 322 lcd.drawLine(0,0,0,47,1); /// draw outline
wray2303 0:6eba0e66ce01 323 lcd.drawLine(83,0,83,47,1);
wray2303 0:6eba0e66ce01 324 lcd.drawLine(0,0,83,0,1);
wray2303 0:6eba0e66ce01 325 lcd.drawLine(0,47,83,47,1);
wray2303 0:6eba0e66ce01 326 lcd.refresh();
wray2303 0:6eba0e66ce01 327 while(!buttonP) { /// while button P is not pressed Sleep() to save power
wray2303 0:6eba0e66ce01 328 Sleep();
wray2303 0:6eba0e66ce01 329 if(buttonP) { /// if button P is pressed
wray2303 0:6eba0e66ce01 330 lcd.clear(); /// clear LCD
wray2303 0:6eba0e66ce01 331 ship.shape(); /// draw the ship back on in position it was left
wray2303 0:6eba0e66ce01 332 GenAsteroidFlag = 1; /// reset the Generate asteroid flag
wray2303 0:6eba0e66ce01 333 PauseNumber = PauseNumber+1; /// increment pause amount counter
wray2303 0:6eba0e66ce01 334 state=1; /// state = 1 means back to the game in the switch statement
wray2303 0:6eba0e66ce01 335 }
wray2303 0:6eba0e66ce01 336 }
wray2303 0:6eba0e66ce01 337 }
wray2303 0:6eba0e66ce01 338
wray2303 0:6eba0e66ce01 339 void calibrateJoystick()
wray2303 0:6eba0e66ce01 340 {
wray2303 0:6eba0e66ce01 341 button.mode(PullDown);
wray2303 0:6eba0e66ce01 342 /// must not move during calibration
wray2303 0:6eba0e66ce01 343 joystick.x0 = xPot; /// initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
wray2303 0:6eba0e66ce01 344 joystick.y0 = yPot;
wray2303 0:6eba0e66ce01 345 }
wray2303 0:6eba0e66ce01 346
wray2303 0:6eba0e66ce01 347 void updateJoystick()
wray2303 0:6eba0e66ce01 348 {
wray2303 0:6eba0e66ce01 349 /// reading the joystick position 0.0 is centered returns a value in the range -0.5 and 0.5
wray2303 0:6eba0e66ce01 350 joystick.x = xPot - joystick.x0;
wray2303 0:6eba0e66ce01 351 joystick.y = yPot - joystick.y0;
wray2303 0:6eba0e66ce01 352 /// read button state
wray2303 0:6eba0e66ce01 353 joystick.button = button;
wray2303 0:6eba0e66ce01 354
wray2303 0:6eba0e66ce01 355 /// calculate direction depending on x,y values
wray2303 0:6eba0e66ce01 356 /// tolerance allows a little lee-way in case joystick not exactly in the stated direction
wray2303 0:6eba0e66ce01 357 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 358 joystick.direction = CENTRE;
wray2303 0:6eba0e66ce01 359 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 360 joystick.direction = UP;
wray2303 0:6eba0e66ce01 361 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 362 joystick.direction = DOWN;
wray2303 0:6eba0e66ce01 363 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 364 joystick.direction = RIGHT;
wray2303 0:6eba0e66ce01 365 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
wray2303 0:6eba0e66ce01 366 joystick.direction = LEFT;
wray2303 0:6eba0e66ce01 367 } else {
wray2303 0:6eba0e66ce01 368 joystick.direction = UNKNOWN;
wray2303 0:6eba0e66ce01 369 }
wray2303 0:6eba0e66ce01 370
wray2303 0:6eba0e66ce01 371 /// set flag for use of the joystick position
wray2303 0:6eba0e66ce01 372 printFlag = 1;
wray2303 0:6eba0e66ce01 373 }