Code for the space evader game.
Dependencies: N5110 PowerControl mbed
main.cpp@0:dd6685f1343e, 2015-05-10 (annotated)
- Committer:
- domplatypus
- Date:
- Sun May 10 16:48:46 2015 +0000
- Revision:
- 0:dd6685f1343e
- Child:
- 1:225522d0dd77
version 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
domplatypus | 0:dd6685f1343e | 1 | /** |
domplatypus | 0:dd6685f1343e | 2 | @file main.cpp |
domplatypus | 0:dd6685f1343e | 3 | @brief Header file containing functions prototypes, defines and global variables. |
domplatypus | 0:dd6685f1343e | 4 | @brief Main code for the running of the game with external libraries of the joystick and the lcd screen |
domplatypus | 0:dd6685f1343e | 5 | @author Dominic J. Platt |
domplatypus | 0:dd6685f1343e | 6 | @date April 2015 |
domplatypus | 0:dd6685f1343e | 7 | */ |
domplatypus | 0:dd6685f1343e | 8 | #include "mbed.h" |
domplatypus | 0:dd6685f1343e | 9 | #include "main.h" |
domplatypus | 0:dd6685f1343e | 10 | #include "N5110.h" //importing Craig Evans library for LCD pixel manipulation functions |
domplatypus | 0:dd6685f1343e | 11 | #include "joystick.h" //external joystick files defined for simplification within the main file |
domplatypus | 0:dd6685f1343e | 12 | //external variables printFlag,joystick,pollJoystick,serial and button used |
domplatypus | 0:dd6685f1343e | 13 | //button(p18),xPot(p19),yPot(p20) used with external joystick |
domplatypus | 0:dd6685f1343e | 14 | #include "PowerControl/PowerControl.h" |
domplatypus | 0:dd6685f1343e | 15 | #include "PowerControl/EthernetPowerControl.h" |
domplatypus | 0:dd6685f1343e | 16 | int main() |
domplatypus | 0:dd6685f1343e | 17 | { |
domplatypus | 0:dd6685f1343e | 18 | init(); |
domplatypus | 0:dd6685f1343e | 19 | void PHY_PowerDown(); //powerdown the ethernet interface on the Mbed system |
domplatypus | 0:dd6685f1343e | 20 | int counter = 0;//counter to count through the timer loops |
domplatypus | 0:dd6685f1343e | 21 | introInit(); |
domplatypus | 0:dd6685f1343e | 22 | timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 23 | while(1) { |
domplatypus | 0:dd6685f1343e | 24 | |
domplatypus | 0:dd6685f1343e | 25 | if(menuMode&timerFlag) { |
domplatypus | 0:dd6685f1343e | 26 | timerFlag = 0; |
domplatypus | 0:dd6685f1343e | 27 | if(counter==20) { |
domplatypus | 0:dd6685f1343e | 28 | menuSet();//runs the menu every 20 counts |
domplatypus | 0:dd6685f1343e | 29 | } |
domplatypus | 0:dd6685f1343e | 30 | if(soundOn) { |
domplatypus | 0:dd6685f1343e | 31 | soundActivate(); //activates sound |
domplatypus | 0:dd6685f1343e | 32 | } |
domplatypus | 0:dd6685f1343e | 33 | if(counter == 20) { |
domplatypus | 0:dd6685f1343e | 34 | counter=0;//reset the counter |
domplatypus | 0:dd6685f1343e | 35 | } |
domplatypus | 0:dd6685f1343e | 36 | counter++; |
domplatypus | 0:dd6685f1343e | 37 | //timer.detach(); |
domplatypus | 0:dd6685f1343e | 38 | //timer.attach(&timerExpired,0.01);//attaching the timer for the next frame |
domplatypus | 0:dd6685f1343e | 39 | } else if(gameOverMode&timerFlag) { |
domplatypus | 0:dd6685f1343e | 40 | //game over mode |
domplatypus | 0:dd6685f1343e | 41 | timerFlag=0; |
domplatypus | 0:dd6685f1343e | 42 | if(counter==20) { |
domplatypus | 0:dd6685f1343e | 43 | gameOverSet();//sets the game over frame every 20 counts |
domplatypus | 0:dd6685f1343e | 44 | } |
domplatypus | 0:dd6685f1343e | 45 | if(soundOn) { |
domplatypus | 0:dd6685f1343e | 46 | soundActivate(); //acitvates sound |
domplatypus | 0:dd6685f1343e | 47 | } |
domplatypus | 0:dd6685f1343e | 48 | if(counter == 20) { |
domplatypus | 0:dd6685f1343e | 49 | counter=0;//reset the counter |
domplatypus | 0:dd6685f1343e | 50 | } |
domplatypus | 0:dd6685f1343e | 51 | counter++; |
domplatypus | 0:dd6685f1343e | 52 | //timer.detach(); |
domplatypus | 0:dd6685f1343e | 53 | //timer.attach(&timerExpired,0.01);//attaching the timer for the next frame |
domplatypus | 0:dd6685f1343e | 54 | } else if(introMode&timerFlag) { |
domplatypus | 0:dd6685f1343e | 55 | introSet(); //sets the intro frame every count |
domplatypus | 0:dd6685f1343e | 56 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 57 | timer.attach(&timerExpired,0.01); //attaching the timer for the next frame |
domplatypus | 0:dd6685f1343e | 58 | } else if(helpMode&timerFlag) { |
domplatypus | 0:dd6685f1343e | 59 | timerFlag = 0; |
domplatypus | 0:dd6685f1343e | 60 | if(buttonFlag) { |
domplatypus | 0:dd6685f1343e | 61 | menuInit(); //if button pressed go back to the menu |
domplatypus | 0:dd6685f1343e | 62 | } |
domplatypus | 0:dd6685f1343e | 63 | buttonFlag = 0; |
domplatypus | 0:dd6685f1343e | 64 | if(counter == 20) { |
domplatypus | 0:dd6685f1343e | 65 | counter=0;//reset the counter |
domplatypus | 0:dd6685f1343e | 66 | } |
domplatypus | 0:dd6685f1343e | 67 | counter++; |
domplatypus | 0:dd6685f1343e | 68 | //timer.detach(); |
domplatypus | 0:dd6685f1343e | 69 | //timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 70 | } |
domplatypus | 0:dd6685f1343e | 71 | |
domplatypus | 0:dd6685f1343e | 72 | else if(highScoreMode&timerFlag) { |
domplatypus | 0:dd6685f1343e | 73 | if(counter==20) { |
domplatypus | 0:dd6685f1343e | 74 | timerFlag = 0; |
domplatypus | 0:dd6685f1343e | 75 | if(buttonFlag) { |
domplatypus | 0:dd6685f1343e | 76 | menuInit(); //if button pressed bring player back to menu |
domplatypus | 0:dd6685f1343e | 77 | } |
domplatypus | 0:dd6685f1343e | 78 | } |
domplatypus | 0:dd6685f1343e | 79 | if(counter == 20) { |
domplatypus | 0:dd6685f1343e | 80 | counter=0;//reset the counter |
domplatypus | 0:dd6685f1343e | 81 | } |
domplatypus | 0:dd6685f1343e | 82 | counter++; |
domplatypus | 0:dd6685f1343e | 83 | //timer.detach(); |
domplatypus | 0:dd6685f1343e | 84 | //timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 85 | } else if(settingsMode&timerFlag) { //runs settingsMode |
domplatypus | 0:dd6685f1343e | 86 | timerFlag=0; |
domplatypus | 0:dd6685f1343e | 87 | if(counter==20) { |
domplatypus | 0:dd6685f1343e | 88 | settingsSet();//runs the setting frame |
domplatypus | 0:dd6685f1343e | 89 | } |
domplatypus | 0:dd6685f1343e | 90 | if(soundOn) { |
domplatypus | 0:dd6685f1343e | 91 | soundActivate(); |
domplatypus | 0:dd6685f1343e | 92 | } |
domplatypus | 0:dd6685f1343e | 93 | if(counter == 20) { |
domplatypus | 0:dd6685f1343e | 94 | counter=0;//reset the counter |
domplatypus | 0:dd6685f1343e | 95 | } |
domplatypus | 0:dd6685f1343e | 96 | counter++; |
domplatypus | 0:dd6685f1343e | 97 | //timer.detach(); |
domplatypus | 0:dd6685f1343e | 98 | //timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 99 | } else if(timerFlag&gameMode) { //game run mode |
domplatypus | 0:dd6685f1343e | 100 | timerFlag = 0; |
domplatypus | 0:dd6685f1343e | 101 | if(roundFlag) { |
domplatypus | 0:dd6685f1343e | 102 | //if new round flag is set start the next round |
domplatypus | 0:dd6685f1343e | 103 | roundFlag =0; |
domplatypus | 0:dd6685f1343e | 104 | roundInit(); |
domplatypus | 0:dd6685f1343e | 105 | } |
domplatypus | 0:dd6685f1343e | 106 | if(soundOn) { |
domplatypus | 0:dd6685f1343e | 107 | soundActivate();//activates sound |
domplatypus | 0:dd6685f1343e | 108 | } |
domplatypus | 0:dd6685f1343e | 109 | if(item.active) { |
domplatypus | 0:dd6685f1343e | 110 | item.addSelf();//if item has selected add the item to the screen |
domplatypus | 0:dd6685f1343e | 111 | } |
domplatypus | 0:dd6685f1343e | 112 | if(speedState==0) { |
domplatypus | 0:dd6685f1343e | 113 | //controlling the rate of movement of the players ship and asteroids depending on the speedState |
domplatypus | 0:dd6685f1343e | 114 | if(counter == 5||counter == 10||counter == 15||counter == 20) { |
domplatypus | 0:dd6685f1343e | 115 | if(shipExplodeFlag) { |
domplatypus | 0:dd6685f1343e | 116 | shipExplode();//if is in explotion stage play explotion frame |
domplatypus | 0:dd6685f1343e | 117 | } else { |
domplatypus | 0:dd6685f1343e | 118 | //otherwise move the ship |
domplatypus | 0:dd6685f1343e | 119 | ship.moveSelf(joystick.direction[0],joystick.direction[1]); |
domplatypus | 0:dd6685f1343e | 120 | } |
domplatypus | 0:dd6685f1343e | 121 | for(int i=0; i<8; i++) { |
domplatypus | 0:dd6685f1343e | 122 | if(asteroidM[i].active) { |
domplatypus | 0:dd6685f1343e | 123 | //move the asteroids that are active |
domplatypus | 0:dd6685f1343e | 124 | asteroidM[i].moveSelf(); |
domplatypus | 0:dd6685f1343e | 125 | } |
domplatypus | 0:dd6685f1343e | 126 | } |
domplatypus | 0:dd6685f1343e | 127 | } |
domplatypus | 0:dd6685f1343e | 128 | } else if(speedState==1) { |
domplatypus | 0:dd6685f1343e | 129 | if(counter == 4||counter == 8||counter == 12||counter == 16||counter == 20) { |
domplatypus | 0:dd6685f1343e | 130 | if(shipExplodeFlag) { |
domplatypus | 0:dd6685f1343e | 131 | shipExplode(); |
domplatypus | 0:dd6685f1343e | 132 | } else { |
domplatypus | 0:dd6685f1343e | 133 | ship.moveSelf(joystick.direction[0],joystick.direction[1]); |
domplatypus | 0:dd6685f1343e | 134 | } |
domplatypus | 0:dd6685f1343e | 135 | for(int i=0; i<8; i++) { |
domplatypus | 0:dd6685f1343e | 136 | if(asteroidM[i].active) { |
domplatypus | 0:dd6685f1343e | 137 | asteroidM[i].moveSelf(); |
domplatypus | 0:dd6685f1343e | 138 | } |
domplatypus | 0:dd6685f1343e | 139 | } |
domplatypus | 0:dd6685f1343e | 140 | } |
domplatypus | 0:dd6685f1343e | 141 | } else if(speedState==2) { |
domplatypus | 0:dd6685f1343e | 142 | if(counter == 3||counter == 6||counter == 9||counter == 12||counter == 15||counter == 18||counter == 20) { |
domplatypus | 0:dd6685f1343e | 143 | if(shipExplodeFlag) { |
domplatypus | 0:dd6685f1343e | 144 | shipExplode(); |
domplatypus | 0:dd6685f1343e | 145 | } else { |
domplatypus | 0:dd6685f1343e | 146 | ship.moveSelf(joystick.direction[0],joystick.direction[1]); |
domplatypus | 0:dd6685f1343e | 147 | } |
domplatypus | 0:dd6685f1343e | 148 | for(int i=0; i<8; i++) { |
domplatypus | 0:dd6685f1343e | 149 | if(asteroidM[i].active) { |
domplatypus | 0:dd6685f1343e | 150 | asteroidM[i].moveSelf(); |
domplatypus | 0:dd6685f1343e | 151 | } |
domplatypus | 0:dd6685f1343e | 152 | } |
domplatypus | 0:dd6685f1343e | 153 | } |
domplatypus | 0:dd6685f1343e | 154 | } else if(speedState==3) { |
domplatypus | 0:dd6685f1343e | 155 | if(counter == 2||counter == 4||counter == 6||counter == 8||counter == 10||counter == 12||counter == 14||counter == 16||counter == 18||counter == 20) { |
domplatypus | 0:dd6685f1343e | 156 | if(shipExplodeFlag) { |
domplatypus | 0:dd6685f1343e | 157 | shipExplode(); |
domplatypus | 0:dd6685f1343e | 158 | } else { |
domplatypus | 0:dd6685f1343e | 159 | ship.moveSelf(joystick.direction[0],joystick.direction[1]); |
domplatypus | 0:dd6685f1343e | 160 | } |
domplatypus | 0:dd6685f1343e | 161 | for(int i=0; i<8; i++) { |
domplatypus | 0:dd6685f1343e | 162 | if(asteroidM[i].active) { |
domplatypus | 0:dd6685f1343e | 163 | asteroidM[i].moveSelf(); |
domplatypus | 0:dd6685f1343e | 164 | } |
domplatypus | 0:dd6685f1343e | 165 | } |
domplatypus | 0:dd6685f1343e | 166 | } |
domplatypus | 0:dd6685f1343e | 167 | } |
domplatypus | 0:dd6685f1343e | 168 | |
domplatypus | 0:dd6685f1343e | 169 | if(buttonFlag&laserClear) { // fire button pressed |
domplatypus | 0:dd6685f1343e | 170 | laserActivate(); |
domplatypus | 0:dd6685f1343e | 171 | } |
domplatypus | 0:dd6685f1343e | 172 | buttonFlag = 0; //reset the buttonFlag after everytime used |
domplatypus | 0:dd6685f1343e | 173 | laserMove(); //move the laser depending on that which are active |
domplatypus | 0:dd6685f1343e | 174 | check(); //checks the logic of the ships position against asteroids, asteroids against each other and walls, lasers against asteroids |
domplatypus | 0:dd6685f1343e | 175 | if(counter ==3||counter == 6|| counter ==9||counter == 12||counter == 15||counter == 18||counter == 20) { |
domplatypus | 0:dd6685f1343e | 176 | setScreen(); // refresh the screen once every three 'counts' |
domplatypus | 0:dd6685f1343e | 177 | } |
domplatypus | 0:dd6685f1343e | 178 | if(counter == 20) { |
domplatypus | 0:dd6685f1343e | 179 | counter=0;//reset the counter |
domplatypus | 0:dd6685f1343e | 180 | } |
domplatypus | 0:dd6685f1343e | 181 | counter++; |
domplatypus | 0:dd6685f1343e | 182 | //timer.detach(); |
domplatypus | 0:dd6685f1343e | 183 | //timer.attach(&timerExpired,0.005);//timerExpired function increased rate for the game |
domplatypus | 0:dd6685f1343e | 184 | } |
domplatypus | 0:dd6685f1343e | 185 | sleep();// put the mbed to sleep otherwise |
domplatypus | 0:dd6685f1343e | 186 | } |
domplatypus | 0:dd6685f1343e | 187 | } |
domplatypus | 0:dd6685f1343e | 188 | |
domplatypus | 0:dd6685f1343e | 189 | void setScreen() |
domplatypus | 0:dd6685f1343e | 190 | { |
domplatypus | 0:dd6685f1343e | 191 | for(int i = 0; i<84; i++) { |
domplatypus | 0:dd6685f1343e | 192 | for(int j = 0; j<48; j++) { //loop through each element of the array |
domplatypus | 0:dd6685f1343e | 193 | if(!(cellsCurrent==cellsBuffer)) {//if the currentCell element does not equal to the previous cell element update the display |
domplatypus | 0:dd6685f1343e | 194 | if (cellsCurrent[j][i] == 1) { |
domplatypus | 0:dd6685f1343e | 195 | lcd.setPixel(i,j); //set pixel if 1 |
domplatypus | 0:dd6685f1343e | 196 | } else { |
domplatypus | 0:dd6685f1343e | 197 | lcd.clearPixel(i,j); //clear pixel otherwise |
domplatypus | 0:dd6685f1343e | 198 | } |
domplatypus | 0:dd6685f1343e | 199 | cellsBuffer[j][i] = cellsCurrent[j][i]; //update the bufferArray |
domplatypus | 0:dd6685f1343e | 200 | } |
domplatypus | 0:dd6685f1343e | 201 | } |
domplatypus | 0:dd6685f1343e | 202 | } |
domplatypus | 0:dd6685f1343e | 203 | lcd.refresh(); //refreshing the screen for the new display |
domplatypus | 0:dd6685f1343e | 204 | } |
domplatypus | 0:dd6685f1343e | 205 | void init() |
domplatypus | 0:dd6685f1343e | 206 | { |
domplatypus | 0:dd6685f1343e | 207 | lcd.init(); |
domplatypus | 0:dd6685f1343e | 208 | debounce.start(); //start the debounce timer |
domplatypus | 0:dd6685f1343e | 209 | buttonFlag =0; |
domplatypus | 0:dd6685f1343e | 210 | walls = 0; // variable to declare whether asteroids can pass through walls |
domplatypus | 0:dd6685f1343e | 211 | settingsMode =0; |
domplatypus | 0:dd6685f1343e | 212 | speedState = 0; |
domplatypus | 0:dd6685f1343e | 213 | roundNumber = 1; |
domplatypus | 0:dd6685f1343e | 214 | asteroids = 0; |
domplatypus | 0:dd6685f1343e | 215 | directionMatrix[0] = -1; // the two available matrices for our asteroids direction |
domplatypus | 0:dd6685f1343e | 216 | directionMatrix[1] = 1; |
domplatypus | 0:dd6685f1343e | 217 | //declares when the player is able to shoot again |
domplatypus | 0:dd6685f1343e | 218 | laserClear = 1; |
domplatypus | 0:dd6685f1343e | 219 | //counter to count how long a laser has been active for |
domplatypus | 0:dd6685f1343e | 220 | laserCounter = 0; |
domplatypus | 0:dd6685f1343e | 221 | timerFlag = 0; //timer intially 0 |
domplatypus | 0:dd6685f1343e | 222 | laserCoolDown = 70; |
domplatypus | 0:dd6685f1343e | 223 | rapidFire = 0; |
domplatypus | 0:dd6685f1343e | 224 | abilityCounter = 0; |
domplatypus | 0:dd6685f1343e | 225 | titleSelected = 0; |
domplatypus | 0:dd6685f1343e | 226 | introMode = 1; |
domplatypus | 0:dd6685f1343e | 227 | lcd.init(); |
domplatypus | 0:dd6685f1343e | 228 | srand(time(NULL));//seedin the random function with the current time for more random functions |
domplatypus | 0:dd6685f1343e | 229 | joystickButton.rise(&buttonPressed); //function called when button is pressed |
domplatypus | 0:dd6685f1343e | 230 | serialTime.attach(&serialISR); //function called when string is sent to the mded |
domplatypus | 0:dd6685f1343e | 231 | calibrateJoystick(); //joystick must remain still during this stage |
domplatypus | 0:dd6685f1343e | 232 | pollJoystick.attach(&updateJoystick,1.0/10.0); //joystick checked every 10 s |
domplatypus | 0:dd6685f1343e | 233 | lcd.setBrightness(1.0);//set the brightness to maximum as sound interfere's with each other |
domplatypus | 0:dd6685f1343e | 234 | } |
domplatypus | 0:dd6685f1343e | 235 | void clearArray() |
domplatypus | 0:dd6685f1343e | 236 | { |
domplatypus | 0:dd6685f1343e | 237 | //clears the 'master' cell array so that all the pixels are nil |
domplatypus | 0:dd6685f1343e | 238 | for(int i = 0; i<48; i++) { |
domplatypus | 0:dd6685f1343e | 239 | for(int j = 0; j<84; j++) { |
domplatypus | 0:dd6685f1343e | 240 | cellsCurrent[i][j] =0; |
domplatypus | 0:dd6685f1343e | 241 | } |
domplatypus | 0:dd6685f1343e | 242 | } |
domplatypus | 0:dd6685f1343e | 243 | } |
domplatypus | 0:dd6685f1343e | 244 | void initSound() |
domplatypus | 0:dd6685f1343e | 245 | { |
domplatypus | 0:dd6685f1343e | 246 | if(sound) { |
domplatypus | 0:dd6685f1343e | 247 | soundOn = 1; |
domplatypus | 0:dd6685f1343e | 248 | soundCounter = 0; |
domplatypus | 0:dd6685f1343e | 249 | buzzer = 0.01; |
domplatypus | 0:dd6685f1343e | 250 | } |
domplatypus | 0:dd6685f1343e | 251 | } |
domplatypus | 0:dd6685f1343e | 252 | void soundActivate() |
domplatypus | 0:dd6685f1343e | 253 | { |
domplatypus | 0:dd6685f1343e | 254 | if(sound) { |
domplatypus | 0:dd6685f1343e | 255 | buzzer = 0.01; |
domplatypus | 0:dd6685f1343e | 256 | if(soundCounter>19) { |
domplatypus | 0:dd6685f1343e | 257 | soundOn = 0; |
domplatypus | 0:dd6685f1343e | 258 | soundCounter = 0; |
domplatypus | 0:dd6685f1343e | 259 | buzzer = 0; |
domplatypus | 0:dd6685f1343e | 260 | //sets the sound according to which soundType is active |
domplatypus | 0:dd6685f1343e | 261 | } else if(soundType ==1) { |
domplatypus | 0:dd6685f1343e | 262 | buzzer.period(1/(frequency[soundCounter]));//cycles through the frequency elements to produce a increasing frequency sound |
domplatypus | 0:dd6685f1343e | 263 | soundCounter++; |
domplatypus | 0:dd6685f1343e | 264 | } else if(soundType ==2) { |
domplatypus | 0:dd6685f1343e | 265 | buzzer.period(1/(frequency[19-soundCounter]));//moves backwards through the frequency elements to produce a decreasing frequency sound |
domplatypus | 0:dd6685f1343e | 266 | soundCounter = soundCounter+2; |
domplatypus | 0:dd6685f1343e | 267 | } else if(soundType ==3) { |
domplatypus | 0:dd6685f1343e | 268 | buzzer.period(0.005);//high frequency sound when asteroids bounce off one another |
domplatypus | 0:dd6685f1343e | 269 | soundCounter = soundCounter+10; |
domplatypus | 0:dd6685f1343e | 270 | } else if(soundType ==4) { |
domplatypus | 0:dd6685f1343e | 271 | buzzer.period(1/(frequency[19-soundCounter])); |
domplatypus | 0:dd6685f1343e | 272 | soundCounter = soundCounter+1; |
domplatypus | 0:dd6685f1343e | 273 | } |
domplatypus | 0:dd6685f1343e | 274 | } |
domplatypus | 0:dd6685f1343e | 275 | } |
domplatypus | 0:dd6685f1343e | 276 | void iDSet() |
domplatypus | 0:dd6685f1343e | 277 | { |
domplatypus | 0:dd6685f1343e | 278 | FILE *fp = fopen("/local/Highscores.csv", "r"); // open file for reading |
domplatypus | 0:dd6685f1343e | 279 | char stringBuff[100]; //creating our stringBuff to hold the characters read from the file |
domplatypus | 0:dd6685f1343e | 280 | if (fp == NULL) {//if file cannot be opened |
domplatypus | 0:dd6685f1343e | 281 | perror ("Error opening file"); |
domplatypus | 0:dd6685f1343e | 282 | for(int i = 0; i<5; i++) { |
domplatypus | 0:dd6685f1343e | 283 | iD[i].active = 0; //deactivate the iD classes |
domplatypus | 0:dd6685f1343e | 284 | } |
domplatypus | 0:dd6685f1343e | 285 | return; |
domplatypus | 0:dd6685f1343e | 286 | } else { |
domplatypus | 0:dd6685f1343e | 287 | if ( fgets (stringBuff , 100 , fp) != NULL ) |
domplatypus | 0:dd6685f1343e | 288 | puts (stringBuff); //sets the stringBuff to hold the characters of the fike |
domplatypus | 0:dd6685f1343e | 289 | fclose (fp); |
domplatypus | 0:dd6685f1343e | 290 | } |
domplatypus | 0:dd6685f1343e | 291 | int k = 0; //integer represents which char in the file the program is at |
domplatypus | 0:dd6685f1343e | 292 | for(int i=0; i<5 ; i++) { // 5 for loop to loop through all 5 iD objects |
domplatypus | 0:dd6685f1343e | 293 | if(stringBuff[k]=='*') { |
domplatypus | 0:dd6685f1343e | 294 | return; //exit loop when it reaches a '*' which is set to signify the end of the file |
domplatypus | 0:dd6685f1343e | 295 | } |
domplatypus | 0:dd6685f1343e | 296 | for(int j = 0; j<3; j++) { |
domplatypus | 0:dd6685f1343e | 297 | iD[i].name[j] = stringBuff[k];//3 characters of the string buff set to the iD |
domplatypus | 0:dd6685f1343e | 298 | k++;//increment counter |
domplatypus | 0:dd6685f1343e | 299 | } |
domplatypus | 0:dd6685f1343e | 300 | int scoreInt[5];//holds each digit of the score |
domplatypus | 0:dd6685f1343e | 301 | for(int j=0; j<5; j++) { |
domplatypus | 0:dd6685f1343e | 302 | scoreInt[j] = 0; // setting these elements to nil |
domplatypus | 0:dd6685f1343e | 303 | } |
domplatypus | 0:dd6685f1343e | 304 | k++;//increment counter as current character will be a ',' |
domplatypus | 0:dd6685f1343e | 305 | int count = 0;//count to count how many digits the score has |
domplatypus | 0:dd6685f1343e | 306 | for(int j = 0; (!(stringBuff[k]==',')); j++) { // loop while not at character ',' |
domplatypus | 0:dd6685f1343e | 307 | scoreInt[j] = stringBuff[k]- '0';//setting the score digit to be the number selected |
domplatypus | 0:dd6685f1343e | 308 | k++;//increment counter |
domplatypus | 0:dd6685f1343e | 309 | count = j; |
domplatypus | 0:dd6685f1343e | 310 | } |
domplatypus | 0:dd6685f1343e | 311 | //sets the score according to the digits obtained and the number of digits |
domplatypus | 0:dd6685f1343e | 312 | if(count ==1) { |
domplatypus | 0:dd6685f1343e | 313 | iD[i].score = scoreInt[0]*10; |
domplatypus | 0:dd6685f1343e | 314 | } else if(count==2) { |
domplatypus | 0:dd6685f1343e | 315 | iD[i].score = scoreInt[0]*100+scoreInt[1]*10; |
domplatypus | 0:dd6685f1343e | 316 | } else if(count==3) { |
domplatypus | 0:dd6685f1343e | 317 | iD[i].score = scoreInt[0]*1000+scoreInt[1]*100+scoreInt[2]*10; |
domplatypus | 0:dd6685f1343e | 318 | } else if(count ==4) { |
domplatypus | 0:dd6685f1343e | 319 | iD[i].score = scoreInt[0]*10000+scoreInt[1]*1000+scoreInt[2]*100+scoreInt[3]*10; |
domplatypus | 0:dd6685f1343e | 320 | } else if(count ==5) { |
domplatypus | 0:dd6685f1343e | 321 | iD[i].score = scoreInt[0]*100000+scoreInt[1]*10000+scoreInt[2]*1000+scoreInt[3]*100+scoreInt[4]*10; |
domplatypus | 0:dd6685f1343e | 322 | } |
domplatypus | 0:dd6685f1343e | 323 | k++;//increment counter |
domplatypus | 0:dd6685f1343e | 324 | for(int j = 0; (!(stringBuff[k]==',')); j++) {//for loop while character is not a ',' |
domplatypus | 0:dd6685f1343e | 325 | iD[i].date[j] = stringBuff[k]; // sets the data characters |
domplatypus | 0:dd6685f1343e | 326 | k++; |
domplatypus | 0:dd6685f1343e | 327 | } |
domplatypus | 0:dd6685f1343e | 328 | k++; |
domplatypus | 0:dd6685f1343e | 329 | iD[i].active = 1; // iD is now active |
domplatypus | 0:dd6685f1343e | 330 | if(stringBuff[k]=='*') { |
domplatypus | 0:dd6685f1343e | 331 | i=5; //exit loop when '*' is reached |
domplatypus | 0:dd6685f1343e | 332 | } |
domplatypus | 0:dd6685f1343e | 333 | } |
domplatypus | 0:dd6685f1343e | 334 | } |
domplatypus | 0:dd6685f1343e | 335 | |
domplatypus | 0:dd6685f1343e | 336 | void writeDataToFile(int score,char n1, char n2, char n3) |
domplatypus | 0:dd6685f1343e | 337 | { |
domplatypus | 0:dd6685f1343e | 338 | //code idea from http://www.cplusplus.com/reference/cstdio/fgets/ |
domplatypus | 0:dd6685f1343e | 339 | |
domplatypus | 0:dd6685f1343e | 340 | ID playerBuff; //ID object created to hold the players attributes to write into the file |
domplatypus | 0:dd6685f1343e | 341 | playerBuff.score = score; |
domplatypus | 0:dd6685f1343e | 342 | playerBuff.active = 1; |
domplatypus | 0:dd6685f1343e | 343 | sprintf(playerBuff.name,"%c%c%c",n1,n2,n3); |
domplatypus | 0:dd6685f1343e | 344 | time_t seconds = time(NULL); // get current time |
domplatypus | 0:dd6685f1343e | 345 | //format time into a string (time and date) |
domplatypus | 0:dd6685f1343e | 346 | strftime(playerBuff.date,6,"%b", localtime(&seconds)); |
domplatypus | 0:dd6685f1343e | 347 | iDSet();//gets the current players ID from the file |
domplatypus | 0:dd6685f1343e | 348 | for(int i = 0; i<5; i++) { |
domplatypus | 0:dd6685f1343e | 349 | //organises the current players score against the other one and shifts all iDs below downwards |
domplatypus | 0:dd6685f1343e | 350 | if(playerBuff.score>iD[i].score) { |
domplatypus | 0:dd6685f1343e | 351 | if(i<4) { |
domplatypus | 0:dd6685f1343e | 352 | iD[4] = iD[3]; |
domplatypus | 0:dd6685f1343e | 353 | } |
domplatypus | 0:dd6685f1343e | 354 | if(i<3) { |
domplatypus | 0:dd6685f1343e | 355 | iD[3] = iD[2]; |
domplatypus | 0:dd6685f1343e | 356 | } |
domplatypus | 0:dd6685f1343e | 357 | if(i<2) { |
domplatypus | 0:dd6685f1343e | 358 | iD[2] = iD[1]; |
domplatypus | 0:dd6685f1343e | 359 | } |
domplatypus | 0:dd6685f1343e | 360 | if(i<1) { |
domplatypus | 0:dd6685f1343e | 361 | iD[1] = iD[0]; |
domplatypus | 0:dd6685f1343e | 362 | } |
domplatypus | 0:dd6685f1343e | 363 | iD[i] = playerBuff; |
domplatypus | 0:dd6685f1343e | 364 | i=5; //exit for loop |
domplatypus | 0:dd6685f1343e | 365 | } |
domplatypus | 0:dd6685f1343e | 366 | } |
domplatypus | 0:dd6685f1343e | 367 | remove( "/local/Highscores.csv" ); //deletes the existing file |
domplatypus | 0:dd6685f1343e | 368 | FILE *fp0 = fopen("/local/Highscores.csv", "a"); // open 'log.csv' (excel file) for appending |
domplatypus | 0:dd6685f1343e | 369 | // creates a new file |
domplatypus | 0:dd6685f1343e | 370 | for(int i = 0; i<5; i++) { |
domplatypus | 0:dd6685f1343e | 371 | if(iD[i].active) { // writes the currents players iD into the file with a ',' to seperate the attributes |
domplatypus | 0:dd6685f1343e | 372 | fprintf(fp0,"%c%c%c,%d,%s,",iD[i].name[0],iD[i].name[1],iD[i].name[2],iD[i].score,iD[i].date); // print string to file "," represents break between name and score "/" represents a new line |
domplatypus | 0:dd6685f1343e | 373 | } |
domplatypus | 0:dd6685f1343e | 374 | } |
domplatypus | 0:dd6685f1343e | 375 | //writes a '*' to signify the end of the data |
domplatypus | 0:dd6685f1343e | 376 | fprintf(fp0,"*");//marks the end of the file |
domplatypus | 0:dd6685f1343e | 377 | fclose(fp0); // close file |
domplatypus | 0:dd6685f1343e | 378 | } |
domplatypus | 0:dd6685f1343e | 379 | void writeID() |
domplatypus | 0:dd6685f1343e | 380 | { |
domplatypus | 0:dd6685f1343e | 381 | lcd.clear(); |
domplatypus | 0:dd6685f1343e | 382 | //prints the column names |
domplatypus | 0:dd6685f1343e | 383 | lcd.printString("Name",0,0); |
domplatypus | 0:dd6685f1343e | 384 | lcd.printString("Score",26,0); |
domplatypus | 0:dd6685f1343e | 385 | lcd.printString("Date",56,0); |
domplatypus | 0:dd6685f1343e | 386 | //assigns the players iD from the flash drive |
domplatypus | 0:dd6685f1343e | 387 | iDSet(); |
domplatypus | 0:dd6685f1343e | 388 | for(int i = 0; i<5; i++) { |
domplatypus | 0:dd6685f1343e | 389 | //printing each repective ID on the screen |
domplatypus | 0:dd6685f1343e | 390 | if(iD[i].active) { //only if the ID is active do we want to display this |
domplatypus | 0:dd6685f1343e | 391 | char nameString[3]; |
domplatypus | 0:dd6685f1343e | 392 | char scoreString[5]; |
domplatypus | 0:dd6685f1343e | 393 | sprintf(scoreString,"%d",iD[i].score); |
domplatypus | 0:dd6685f1343e | 394 | sprintf(nameString,"%c%c%c",iD[i].name[0],iD[i].name[1],iD[i].name[2]); |
domplatypus | 0:dd6685f1343e | 395 | lcd.printString(nameString,0,i+1); |
domplatypus | 0:dd6685f1343e | 396 | lcd.printString(scoreString,26,i+1); |
domplatypus | 0:dd6685f1343e | 397 | lcd.printString(iD[i].date,56,i+1); |
domplatypus | 0:dd6685f1343e | 398 | } |
domplatypus | 0:dd6685f1343e | 399 | } |
domplatypus | 0:dd6685f1343e | 400 | } |
domplatypus | 0:dd6685f1343e | 401 | |
domplatypus | 0:dd6685f1343e | 402 | void initSpawn() |
domplatypus | 0:dd6685f1343e | 403 | { |
domplatypus | 0:dd6685f1343e | 404 | int count = 0; |
domplatypus | 0:dd6685f1343e | 405 | //defining the spawnLocations matrix |
domplatypus | 0:dd6685f1343e | 406 | for(int j = 0; j<4; j++) { |
domplatypus | 0:dd6685f1343e | 407 | for(int i= 0 ; i<8; i++) { |
domplatypus | 0:dd6685f1343e | 408 | if(i==7||i==0||j==0||j==3) { |
domplatypus | 0:dd6685f1343e | 409 | //defining the spawn locations to be along the borders of the screen |
domplatypus | 0:dd6685f1343e | 410 | spawnLocation[count][0] = i*10; |
domplatypus | 0:dd6685f1343e | 411 | spawnLocation[count][1] = j*10; |
domplatypus | 0:dd6685f1343e | 412 | if(j==3) { |
domplatypus | 0:dd6685f1343e | 413 | spawnLocation[count][1] = 42; |
domplatypus | 0:dd6685f1343e | 414 | } |
domplatypus | 0:dd6685f1343e | 415 | spawnLocation[count][2] = 1; |
domplatypus | 0:dd6685f1343e | 416 | count++; |
domplatypus | 0:dd6685f1343e | 417 | } |
domplatypus | 0:dd6685f1343e | 418 | } |
domplatypus | 0:dd6685f1343e | 419 | } |
domplatypus | 0:dd6685f1343e | 420 | if(ship.position[1]<10) { |
domplatypus | 0:dd6685f1343e | 421 | int x = ship.position[0]/10; |
domplatypus | 0:dd6685f1343e | 422 | spawnLocation[x][2] = 0; // not available |
domplatypus | 0:dd6685f1343e | 423 | } |
domplatypus | 0:dd6685f1343e | 424 | if((ship.position[1]>30)) { |
domplatypus | 0:dd6685f1343e | 425 | int x = ship.position[0]/10; |
domplatypus | 0:dd6685f1343e | 426 | spawnLocation[x+12][2] = 0; |
domplatypus | 0:dd6685f1343e | 427 | if(!((x+11)==11)) { |
domplatypus | 0:dd6685f1343e | 428 | spawnLocation[x+11][2] = 0; |
domplatypus | 0:dd6685f1343e | 429 | } |
domplatypus | 0:dd6685f1343e | 430 | if(!((x+13)==20)) { |
domplatypus | 0:dd6685f1343e | 431 | spawnLocation[x+13][2] = 0; |
domplatypus | 0:dd6685f1343e | 432 | } |
domplatypus | 0:dd6685f1343e | 433 | } |
domplatypus | 0:dd6685f1343e | 434 | if(ship.position[0]<10) { |
domplatypus | 0:dd6685f1343e | 435 | int x = ship.position[1]/10; |
domplatypus | 0:dd6685f1343e | 436 | if(x==0) { |
domplatypus | 0:dd6685f1343e | 437 | spawnLocation[0][2] = 0; |
domplatypus | 0:dd6685f1343e | 438 | } |
domplatypus | 0:dd6685f1343e | 439 | if(x==1) { |
domplatypus | 0:dd6685f1343e | 440 | spawnLocation[8][2] = 0; |
domplatypus | 0:dd6685f1343e | 441 | } |
domplatypus | 0:dd6685f1343e | 442 | if(x==2) { |
domplatypus | 0:dd6685f1343e | 443 | spawnLocation[10][2] = 0; |
domplatypus | 0:dd6685f1343e | 444 | } |
domplatypus | 0:dd6685f1343e | 445 | if(x==3) { |
domplatypus | 0:dd6685f1343e | 446 | spawnLocation[12][2] = 0; |
domplatypus | 0:dd6685f1343e | 447 | } |
domplatypus | 0:dd6685f1343e | 448 | } |
domplatypus | 0:dd6685f1343e | 449 | if(ship.position[0]>74) { |
domplatypus | 0:dd6685f1343e | 450 | int x = ship.position[1]/10; |
domplatypus | 0:dd6685f1343e | 451 | if(x==0) { |
domplatypus | 0:dd6685f1343e | 452 | spawnLocation[7][2] = 0; |
domplatypus | 0:dd6685f1343e | 453 | } |
domplatypus | 0:dd6685f1343e | 454 | if(x==1) { |
domplatypus | 0:dd6685f1343e | 455 | spawnLocation[9][2] = 0; |
domplatypus | 0:dd6685f1343e | 456 | } |
domplatypus | 0:dd6685f1343e | 457 | if(x==2) { |
domplatypus | 0:dd6685f1343e | 458 | spawnLocation[11][2] = 0; |
domplatypus | 0:dd6685f1343e | 459 | } |
domplatypus | 0:dd6685f1343e | 460 | if(x==3) { |
domplatypus | 0:dd6685f1343e | 461 | spawnLocation[19][2] = 0; |
domplatypus | 0:dd6685f1343e | 462 | } |
domplatypus | 0:dd6685f1343e | 463 | } |
domplatypus | 0:dd6685f1343e | 464 | } |
domplatypus | 0:dd6685f1343e | 465 | int getSpawnLocation() |
domplatypus | 0:dd6685f1343e | 466 | { |
domplatypus | 0:dd6685f1343e | 467 | |
domplatypus | 0:dd6685f1343e | 468 | while(1) {//while loop continues until function produces a return |
domplatypus | 0:dd6685f1343e | 469 | int x = rand() % 20; // produces a random number 0 to 19 |
domplatypus | 0:dd6685f1343e | 470 | if(spawnLocation[x][2]) { |
domplatypus | 0:dd6685f1343e | 471 | //if position is available |
domplatypus | 0:dd6685f1343e | 472 | spawnLocation[x][2] = 0;//position no longer available |
domplatypus | 0:dd6685f1343e | 473 | return x;//position is no longer availables |
domplatypus | 0:dd6685f1343e | 474 | } |
domplatypus | 0:dd6685f1343e | 475 | } |
domplatypus | 0:dd6685f1343e | 476 | } |
domplatypus | 0:dd6685f1343e | 477 | void timerExpired() |
domplatypus | 0:dd6685f1343e | 478 | { |
domplatypus | 0:dd6685f1343e | 479 | // timer function to control the frame rate, enemy speed, ship speed, laser speed |
domplatypus | 0:dd6685f1343e | 480 | timerFlag = 1; //turn flag on |
domplatypus | 0:dd6685f1343e | 481 | } |
domplatypus | 0:dd6685f1343e | 482 | |
domplatypus | 0:dd6685f1343e | 483 | Laser laserInit() //function to create a laser class with x y position sent |
domplatypus | 0:dd6685f1343e | 484 | { |
domplatypus | 0:dd6685f1343e | 485 | Laser laser; //laser object created of the Laser class |
domplatypus | 0:dd6685f1343e | 486 | laser.active = 0; // laser is not active |
domplatypus | 0:dd6685f1343e | 487 | laser.counterFlag = 0; |
domplatypus | 0:dd6685f1343e | 488 | for(int i =0; i< 5; i++) { |
domplatypus | 0:dd6685f1343e | 489 | laser.shape[i] = 1; // defining the laser matrix |
domplatypus | 0:dd6685f1343e | 490 | } |
domplatypus | 0:dd6685f1343e | 491 | return laser; //returning the laser class |
domplatypus | 0:dd6685f1343e | 492 | } |
domplatypus | 0:dd6685f1343e | 493 | |
domplatypus | 0:dd6685f1343e | 494 | void gameOverInit() |
domplatypus | 0:dd6685f1343e | 495 | { |
domplatypus | 0:dd6685f1343e | 496 | //initialises variables for the gameOver screen |
domplatypus | 0:dd6685f1343e | 497 | cSelected =0; |
domplatypus | 0:dd6685f1343e | 498 | c1 =0; |
domplatypus | 0:dd6685f1343e | 499 | c2 = 0; |
domplatypus | 0:dd6685f1343e | 500 | c3 = 0; |
domplatypus | 0:dd6685f1343e | 501 | gameMode = 0; |
domplatypus | 0:dd6685f1343e | 502 | gameOverMode =1; |
domplatypus | 0:dd6685f1343e | 503 | menuMode = 0; |
domplatypus | 0:dd6685f1343e | 504 | speedState = 0; |
domplatypus | 0:dd6685f1343e | 505 | //http://developer.mbed.org/questions/249/float-or-integer-to-char-or-string-conve/ |
domplatypus | 0:dd6685f1343e | 506 | char scoreString[10]; |
domplatypus | 0:dd6685f1343e | 507 | //converts score to string |
domplatypus | 0:dd6685f1343e | 508 | sprintf(scoreString,"%d",score); |
domplatypus | 0:dd6685f1343e | 509 | lcd.clear(); |
domplatypus | 0:dd6685f1343e | 510 | lcd.printString("Game Over",10,0); |
domplatypus | 0:dd6685f1343e | 511 | lcd.printString("Your score was",0,1); |
domplatypus | 0:dd6685f1343e | 512 | //score is printed |
domplatypus | 0:dd6685f1343e | 513 | lcd.printString(scoreString,10,2); |
domplatypus | 0:dd6685f1343e | 514 | lcd.printString("AAA",10,4); |
domplatypus | 0:dd6685f1343e | 515 | buttonFlag = 0; |
domplatypus | 0:dd6685f1343e | 516 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 517 | timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 518 | } |
domplatypus | 0:dd6685f1343e | 519 | void check() |
domplatypus | 0:dd6685f1343e | 520 | { |
domplatypus | 0:dd6685f1343e | 521 | //ship position check against asteroids |
domplatypus | 0:dd6685f1343e | 522 | for (int j = 0; j<8; j++) { |
domplatypus | 0:dd6685f1343e | 523 | //loops through every asteroid |
domplatypus | 0:dd6685f1343e | 524 | if((((asteroidM[j].position[0])>(ship.position[0]-5))&((asteroidM[j].position[0])<(ship.position[0]+5)) |
domplatypus | 0:dd6685f1343e | 525 | &((asteroidM[j].position[1])>(ship.position[1]-5))&((asteroidM[j].position[1])<(ship.position[1]+5)))&asteroidM[j].active) { |
domplatypus | 0:dd6685f1343e | 526 | //if an asteroid overlaps with a ships position |
domplatypus | 0:dd6685f1343e | 527 | if(ship.shield) {//if ship shields are up |
domplatypus | 0:dd6685f1343e | 528 | asteroidM[j].active = 0; |
domplatypus | 0:dd6685f1343e | 529 | asteroidM[j].deleteSelf(); //deactivating the asteroid |
domplatypus | 0:dd6685f1343e | 530 | asteroids--; |
domplatypus | 0:dd6685f1343e | 531 | if(score>50) {//player looses 50 points |
domplatypus | 0:dd6685f1343e | 532 | score = score - 50; |
domplatypus | 0:dd6685f1343e | 533 | } else { |
domplatypus | 0:dd6685f1343e | 534 | score = 0; |
domplatypus | 0:dd6685f1343e | 535 | } |
domplatypus | 0:dd6685f1343e | 536 | ship.shield =0; //shields are deactivated |
domplatypus | 0:dd6685f1343e | 537 | ship.shieldUpdate();//updates the players shield state |
domplatypus | 0:dd6685f1343e | 538 | initSound(); |
domplatypus | 0:dd6685f1343e | 539 | soundType =3; |
domplatypus | 0:dd6685f1343e | 540 | } else if((!shipExplodeFlag)&!ship.shield) { |
domplatypus | 0:dd6685f1343e | 541 | shipExplodeFlag = 1; |
domplatypus | 0:dd6685f1343e | 542 | explotionStage = 1; |
domplatypus | 0:dd6685f1343e | 543 | asteroidM[j].active = 0; |
domplatypus | 0:dd6685f1343e | 544 | asteroidM[j].deleteSelf(); |
domplatypus | 0:dd6685f1343e | 545 | initSound(); |
domplatypus | 0:dd6685f1343e | 546 | soundType =2; |
domplatypus | 0:dd6685f1343e | 547 | } |
domplatypus | 0:dd6685f1343e | 548 | } |
domplatypus | 0:dd6685f1343e | 549 | } |
domplatypus | 0:dd6685f1343e | 550 | // checking the asteroids position off each other so they can bounce off each other |
domplatypus | 0:dd6685f1343e | 551 | for(int i = 0; i<8; i++) { |
domplatypus | 0:dd6685f1343e | 552 | if(asteroidM[i].active) { //looping through all the active asteroids |
domplatypus | 0:dd6685f1343e | 553 | for(int j = 0; j<8; j++) { |
domplatypus | 0:dd6685f1343e | 554 | if(i==j) { //if asteroid checked is the actual asteroid of the first for loop move to the next one |
domplatypus | 0:dd6685f1343e | 555 | j++; |
domplatypus | 0:dd6685f1343e | 556 | } |
domplatypus | 0:dd6685f1343e | 557 | if(asteroidM[j].active) { |
domplatypus | 0:dd6685f1343e | 558 | if((asteroidM[i].position[0]>(asteroidM[j].position[0]-5))&(asteroidM[i].position[0]<(asteroidM[j].position[0]+5)) |
domplatypus | 0:dd6685f1343e | 559 | &(asteroidM[i].position[1]>(asteroidM[j].position[1]-5))&(asteroidM[i].position[1]<(asteroidM[j].position[1]+5))) { |
domplatypus | 0:dd6685f1343e | 560 | // if asteroids collide |
domplatypus | 0:dd6685f1343e | 561 | initSound(); |
domplatypus | 0:dd6685f1343e | 562 | soundType =3;// produce a sound |
domplatypus | 0:dd6685f1343e | 563 | if(asteroidM[i].position[0]>asteroidM[j].position[0]) {//if asteroids x position is greater than the other move forwards |
domplatypus | 0:dd6685f1343e | 564 | asteroidM[i].xDirection = 1; |
domplatypus | 0:dd6685f1343e | 565 | } else if(asteroidM[i].position[0]<=asteroidM[j].position[0]) { //if asteroid is behind the other asteroid in the x direction move to the left |
domplatypus | 0:dd6685f1343e | 566 | asteroidM[i].xDirection = -1; |
domplatypus | 0:dd6685f1343e | 567 | } |
domplatypus | 0:dd6685f1343e | 568 | if(asteroidM[i].position[1]>asteroidM[j].position[1]) {//if asteroid is below the other asteroid move down |
domplatypus | 0:dd6685f1343e | 569 | asteroidM[i].yDirection = 1; |
domplatypus | 0:dd6685f1343e | 570 | } else if(asteroidM[i].position[1]<=asteroidM[j].position[1])//if asteroid is above the other asteroid move up |
domplatypus | 0:dd6685f1343e | 571 | asteroidM[i].yDirection = -1; |
domplatypus | 0:dd6685f1343e | 572 | } |
domplatypus | 0:dd6685f1343e | 573 | } |
domplatypus | 0:dd6685f1343e | 574 | } |
domplatypus | 0:dd6685f1343e | 575 | } |
domplatypus | 0:dd6685f1343e | 576 | } |
domplatypus | 0:dd6685f1343e | 577 | //checking the laser positions against the asteroids |
domplatypus | 0:dd6685f1343e | 578 | for(int i = 0; i<4; i++) { |
domplatypus | 0:dd6685f1343e | 579 | for (int j = 0; j<8; j++) { |
domplatypus | 0:dd6685f1343e | 580 | if(((asteroidM[j].position[0]>(laser[i].position[0]-5))&(asteroidM[j].position[0]<(laser[i].position[0]+5)) |
domplatypus | 0:dd6685f1343e | 581 | &(asteroidM[j].position[1]>(laser[i].position[1]-5))&(asteroidM[j].position[1]<(laser[i].position[1]+5))&asteroidM[j].active&laser[i].active)) { |
domplatypus | 0:dd6685f1343e | 582 | //if both laser and asteroid is active and they collide inititiate destruction |
domplatypus | 0:dd6685f1343e | 583 | initSound(); |
domplatypus | 0:dd6685f1343e | 584 | soundType =2;//play sound |
domplatypus | 0:dd6685f1343e | 585 | asteroidM[j].active = 0; |
domplatypus | 0:dd6685f1343e | 586 | asteroidM[j].deleteSelf(); //deactivating the asteroid |
domplatypus | 0:dd6685f1343e | 587 | asteroids--;//marking one less asteroid |
domplatypus | 0:dd6685f1343e | 588 | score = score + 10;//player gains point |
domplatypus | 0:dd6685f1343e | 589 | laser[i].active = 0; // deactiviating the laser |
domplatypus | 0:dd6685f1343e | 590 | laser[i].deleteSelf(); |
domplatypus | 0:dd6685f1343e | 591 | if(!(item.active||rapidFire)) { |
domplatypus | 0:dd6685f1343e | 592 | int x = rand() %7; // 1 in 7 chance of an item drop |
domplatypus | 0:dd6685f1343e | 593 | if(x==0) { |
domplatypus | 0:dd6685f1343e | 594 | //puts the item to the asteroids position |
domplatypus | 0:dd6685f1343e | 595 | item.active = 1; |
domplatypus | 0:dd6685f1343e | 596 | item.position[0] = asteroidM[j].position[0]; |
domplatypus | 0:dd6685f1343e | 597 | item.position[1] = asteroidM[j].position[1]; |
domplatypus | 0:dd6685f1343e | 598 | item.addSelf(); |
domplatypus | 0:dd6685f1343e | 599 | } |
domplatypus | 0:dd6685f1343e | 600 | } |
domplatypus | 0:dd6685f1343e | 601 | } |
domplatypus | 0:dd6685f1343e | 602 | } |
domplatypus | 0:dd6685f1343e | 603 | } |
domplatypus | 0:dd6685f1343e | 604 | if(asteroids == 0) { |
domplatypus | 0:dd6685f1343e | 605 | //new round when all asteroids are destroyed |
domplatypus | 0:dd6685f1343e | 606 | roundFlag = 1; |
domplatypus | 0:dd6685f1343e | 607 | } |
domplatypus | 0:dd6685f1343e | 608 | if(rapidFire) { |
domplatypus | 0:dd6685f1343e | 609 | //rapidFire enabled when user touches item |
domplatypus | 0:dd6685f1343e | 610 | if(abilityCounter>1000) { //if the ability counter exceeds a certain number |
domplatypus | 0:dd6685f1343e | 611 | rapidFire=0;//deactivate rapidFire |
domplatypus | 0:dd6685f1343e | 612 | laserCoolDown = 70; //laser fire cool down set to normal |
domplatypus | 0:dd6685f1343e | 613 | } else { |
domplatypus | 0:dd6685f1343e | 614 | abilityCounter++; //increment counter |
domplatypus | 0:dd6685f1343e | 615 | } |
domplatypus | 0:dd6685f1343e | 616 | } |
domplatypus | 0:dd6685f1343e | 617 | if((((item.position[0])>(ship.position[0]-5))&((item.position[0])<(ship.position[0]+5)) |
domplatypus | 0:dd6685f1343e | 618 | &((item.position[1])>(ship.position[1]-5))&((item.position[1])<(ship.position[1]+5)))&item.active) { |
domplatypus | 0:dd6685f1343e | 619 | //if ship collides with the item |
domplatypus | 0:dd6685f1343e | 620 | //sets the laserCoolDown to be much lower for rapidFire |
domplatypus | 0:dd6685f1343e | 621 | laserCoolDown = 22; |
domplatypus | 0:dd6685f1343e | 622 | item.active = 0; //deactivate and remove the item from the matrix |
domplatypus | 0:dd6685f1343e | 623 | item.deleteSelf(); |
domplatypus | 0:dd6685f1343e | 624 | rapidFire = 1; |
domplatypus | 0:dd6685f1343e | 625 | abilityCounter = 0;//initiates counter |
domplatypus | 0:dd6685f1343e | 626 | } |
domplatypus | 0:dd6685f1343e | 627 | |
domplatypus | 0:dd6685f1343e | 628 | } |
domplatypus | 0:dd6685f1343e | 629 | |
domplatypus | 0:dd6685f1343e | 630 | void laserActivate() |
domplatypus | 0:dd6685f1343e | 631 | { |
domplatypus | 0:dd6685f1343e | 632 | laserClear = 0; // laser is not clear to fire |
domplatypus | 0:dd6685f1343e | 633 | if(!laser[0].active) {//if laser is not active activate this laser object |
domplatypus | 0:dd6685f1343e | 634 | laser[0].position[0] = ship.position[0] + 5; |
domplatypus | 0:dd6685f1343e | 635 | laser[0].position[1] = ship.position[1] + 2;//set the lasers position and activate |
domplatypus | 0:dd6685f1343e | 636 | laser[0].active =1; //laser is now active |
domplatypus | 0:dd6685f1343e | 637 | laser[0].counterFlag = 1; |
domplatypus | 0:dd6685f1343e | 638 | } else if(!laser[1].active) { |
domplatypus | 0:dd6685f1343e | 639 | laser[1].position[0] = ship.position[0] +5; |
domplatypus | 0:dd6685f1343e | 640 | laser[1].position[1] = ship.position[1] +2; |
domplatypus | 0:dd6685f1343e | 641 | laser[1].active = 1; |
domplatypus | 0:dd6685f1343e | 642 | laser[1].counterFlag = 1; |
domplatypus | 0:dd6685f1343e | 643 | } else if(!laser[2].active) { |
domplatypus | 0:dd6685f1343e | 644 | laser[2].position[0] = ship.position[0]+5; |
domplatypus | 0:dd6685f1343e | 645 | laser[2].position[1] = ship.position[1]+2; |
domplatypus | 0:dd6685f1343e | 646 | laser[2].active = 1; |
domplatypus | 0:dd6685f1343e | 647 | laser[2].counterFlag = 1; |
domplatypus | 0:dd6685f1343e | 648 | } else if(!laser[3].active) { |
domplatypus | 0:dd6685f1343e | 649 | laser[3].position[0] = ship.position[0]+5; |
domplatypus | 0:dd6685f1343e | 650 | laser[3].position[1] = ship.position[1]+2; |
domplatypus | 0:dd6685f1343e | 651 | laser[3].active = 1; |
domplatypus | 0:dd6685f1343e | 652 | laser[3].counterFlag = 1; |
domplatypus | 0:dd6685f1343e | 653 | } |
domplatypus | 0:dd6685f1343e | 654 | initSound();//initiate laser sound |
domplatypus | 0:dd6685f1343e | 655 | soundType =1; |
domplatypus | 0:dd6685f1343e | 656 | } |
domplatypus | 0:dd6685f1343e | 657 | void laserMove() |
domplatypus | 0:dd6685f1343e | 658 | { |
domplatypus | 0:dd6685f1343e | 659 | //moves the laser if it is active |
domplatypus | 0:dd6685f1343e | 660 | if(laser[0].active) { |
domplatypus | 0:dd6685f1343e | 661 | laser[0].moveSelf(1); |
domplatypus | 0:dd6685f1343e | 662 | } |
domplatypus | 0:dd6685f1343e | 663 | if(laser[1].active) { |
domplatypus | 0:dd6685f1343e | 664 | laser[1].moveSelf(1); |
domplatypus | 0:dd6685f1343e | 665 | } |
domplatypus | 0:dd6685f1343e | 666 | if(laser[2].active) { |
domplatypus | 0:dd6685f1343e | 667 | laser[2].moveSelf(1); |
domplatypus | 0:dd6685f1343e | 668 | } |
domplatypus | 0:dd6685f1343e | 669 | if(laser[3].active) { |
domplatypus | 0:dd6685f1343e | 670 | laser[3].moveSelf(1); |
domplatypus | 0:dd6685f1343e | 671 | } |
domplatypus | 0:dd6685f1343e | 672 | //if laser counterFlag is set, increment the laser Counter |
domplatypus | 0:dd6685f1343e | 673 | if(laser[0].counterFlag||laser[1].counterFlag||laser[2].counterFlag||laser[3].counterFlag) { |
domplatypus | 0:dd6685f1343e | 674 | //if the laserCounter exceeds the cooldown limit laser is clear to fire |
domplatypus | 0:dd6685f1343e | 675 | if(laserCounter>laserCoolDown) { |
domplatypus | 0:dd6685f1343e | 676 | laserCounter = 0; |
domplatypus | 0:dd6685f1343e | 677 | laserClear = 1; |
domplatypus | 0:dd6685f1343e | 678 | laser[0].counterFlag = 0; |
domplatypus | 0:dd6685f1343e | 679 | laser[1].counterFlag = 0; |
domplatypus | 0:dd6685f1343e | 680 | laser[2].counterFlag = 0; |
domplatypus | 0:dd6685f1343e | 681 | laser[3].counterFlag = 0; |
domplatypus | 0:dd6685f1343e | 682 | } |
domplatypus | 0:dd6685f1343e | 683 | laserCounter++; |
domplatypus | 0:dd6685f1343e | 684 | } |
domplatypus | 0:dd6685f1343e | 685 | } |
domplatypus | 0:dd6685f1343e | 686 | void gameStart() |
domplatypus | 0:dd6685f1343e | 687 | { |
domplatypus | 0:dd6685f1343e | 688 | for(int i = 0; i<8; i++) { |
domplatypus | 0:dd6685f1343e | 689 | asteroidM[i].active = 0; |
domplatypus | 0:dd6685f1343e | 690 | } |
domplatypus | 0:dd6685f1343e | 691 | item.active = 0; |
domplatypus | 0:dd6685f1343e | 692 | clearArray(); |
domplatypus | 0:dd6685f1343e | 693 | asteroids = 0; |
domplatypus | 0:dd6685f1343e | 694 | roundNumber = 1; |
domplatypus | 0:dd6685f1343e | 695 | score = 0; |
domplatypus | 0:dd6685f1343e | 696 | menuMode =0; |
domplatypus | 0:dd6685f1343e | 697 | gameMode =1; |
domplatypus | 0:dd6685f1343e | 698 | highScoreMode = 0; |
domplatypus | 0:dd6685f1343e | 699 | lcd.clear(); |
domplatypus | 0:dd6685f1343e | 700 | clearArray(); |
domplatypus | 0:dd6685f1343e | 701 | ship.init(); |
domplatypus | 0:dd6685f1343e | 702 | ship.addSelf(); |
domplatypus | 0:dd6685f1343e | 703 | item.init(); |
domplatypus | 0:dd6685f1343e | 704 | roundFlag = 1; |
domplatypus | 0:dd6685f1343e | 705 | gameMode = 1; |
domplatypus | 0:dd6685f1343e | 706 | buttonFlag =0; |
domplatypus | 0:dd6685f1343e | 707 | shipExplodeFlag = 0; |
domplatypus | 0:dd6685f1343e | 708 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 709 | timer.attach(&timerExpired,0.005); |
domplatypus | 0:dd6685f1343e | 710 | } |
domplatypus | 0:dd6685f1343e | 711 | void highScoreSet() |
domplatypus | 0:dd6685f1343e | 712 | { |
domplatypus | 0:dd6685f1343e | 713 | menuMode = 0; |
domplatypus | 0:dd6685f1343e | 714 | gameMode = 0; |
domplatypus | 0:dd6685f1343e | 715 | introMode = 0; |
domplatypus | 0:dd6685f1343e | 716 | highScoreMode = 1; |
domplatypus | 0:dd6685f1343e | 717 | gameOverMode = 0; |
domplatypus | 0:dd6685f1343e | 718 | iDSet(); |
domplatypus | 0:dd6685f1343e | 719 | writeID(); |
domplatypus | 0:dd6685f1343e | 720 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 721 | timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 722 | } |
domplatypus | 0:dd6685f1343e | 723 | void menuSet() |
domplatypus | 0:dd6685f1343e | 724 | { |
domplatypus | 0:dd6685f1343e | 725 | //sets the option selected depending on the joystick y direction |
domplatypus | 0:dd6685f1343e | 726 | titleSelected = titleSelected + joystick.direction[1]; |
domplatypus | 0:dd6685f1343e | 727 | //loops around the menu |
domplatypus | 0:dd6685f1343e | 728 | if(titleSelected>3) { |
domplatypus | 0:dd6685f1343e | 729 | titleSelected = 0; |
domplatypus | 0:dd6685f1343e | 730 | } else if(titleSelected<0) { |
domplatypus | 0:dd6685f1343e | 731 | titleSelected = 3; |
domplatypus | 0:dd6685f1343e | 732 | } |
domplatypus | 0:dd6685f1343e | 733 | if((joystick.direction[1]==1)||(joystick.direction[1]==-1)) { |
domplatypus | 0:dd6685f1343e | 734 | //if joystick moved initiate sound |
domplatypus | 0:dd6685f1343e | 735 | soundType =3; |
domplatypus | 0:dd6685f1343e | 736 | initSound(); |
domplatypus | 0:dd6685f1343e | 737 | } |
domplatypus | 0:dd6685f1343e | 738 | lcd.clear(); |
domplatypus | 0:dd6685f1343e | 739 | for(int i = 0; i<84; i++) { |
domplatypus | 0:dd6685f1343e | 740 | cellsCurrent[8][i] = 1;//draws a line under the title |
domplatypus | 0:dd6685f1343e | 741 | } |
domplatypus | 0:dd6685f1343e | 742 | ship.addSelf(); // adds the ship as a logo to the screen |
domplatypus | 0:dd6685f1343e | 743 | setScreen(); // sets the screen |
domplatypus | 0:dd6685f1343e | 744 | lcd.printString("Space Evader",0,0); //text overlayed on the screen |
domplatypus | 0:dd6685f1343e | 745 | lcd.printString("Start Game",10,2); |
domplatypus | 0:dd6685f1343e | 746 | lcd.printString("HighScores",10,3); |
domplatypus | 0:dd6685f1343e | 747 | lcd.printString("Help",10,4); |
domplatypus | 0:dd6685f1343e | 748 | lcd.printString("Settings",10,5); |
domplatypus | 0:dd6685f1343e | 749 | lcd.printString(">",0,titleSelected+2); // array set according to which titleSelected |
domplatypus | 0:dd6685f1343e | 750 | if(buttonFlag&(titleSelected == 0)) { |
domplatypus | 0:dd6685f1343e | 751 | //start option selected |
domplatypus | 0:dd6685f1343e | 752 | gameStart(); |
domplatypus | 0:dd6685f1343e | 753 | } else if(buttonFlag&(titleSelected == 1)) { |
domplatypus | 0:dd6685f1343e | 754 | //instruction option selected |
domplatypus | 0:dd6685f1343e | 755 | highScoreSet(); |
domplatypus | 0:dd6685f1343e | 756 | } else if(buttonFlag&(titleSelected == 2)) { |
domplatypus | 0:dd6685f1343e | 757 | //instruction option selected |
domplatypus | 0:dd6685f1343e | 758 | helpInit(); |
domplatypus | 0:dd6685f1343e | 759 | } else if(buttonFlag&(titleSelected == 3)) { |
domplatypus | 0:dd6685f1343e | 760 | //settings option selected |
domplatypus | 0:dd6685f1343e | 761 | settingsInit(); |
domplatypus | 0:dd6685f1343e | 762 | } |
domplatypus | 0:dd6685f1343e | 763 | //setting the buttonFlag off so it is not set again until the joystick is pressed |
domplatypus | 0:dd6685f1343e | 764 | buttonFlag = 0; |
domplatypus | 0:dd6685f1343e | 765 | } |
domplatypus | 0:dd6685f1343e | 766 | |
domplatypus | 0:dd6685f1343e | 767 | void gameOverSet() |
domplatypus | 0:dd6685f1343e | 768 | { |
domplatypus | 0:dd6685f1343e | 769 | if((joystick.direction[0]==1)||(joystick.direction[0]==-1)||(joystick.direction[1]==1)||(joystick.direction[1]==-1)) { |
domplatypus | 0:dd6685f1343e | 770 | //sound activate when joystick moved |
domplatypus | 0:dd6685f1343e | 771 | initSound(); |
domplatypus | 0:dd6685f1343e | 772 | soundType =3; |
domplatypus | 0:dd6685f1343e | 773 | } |
domplatypus | 0:dd6685f1343e | 774 | char scoreString[10]; //stores the score |
domplatypus | 0:dd6685f1343e | 775 | sprintf(scoreString,"%d",score);//converts the users score from an int to a string |
domplatypus | 0:dd6685f1343e | 776 | char alphabet [27] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; //alphabet matrix defined |
domplatypus | 0:dd6685f1343e | 777 | cSelected = cSelected +joystick.direction[0]; //character selected moved depending on the joystick |
domplatypus | 0:dd6685f1343e | 778 | if(cSelected>2) { //loops through the characters selected at the boundaries |
domplatypus | 0:dd6685f1343e | 779 | cSelected = 0; |
domplatypus | 0:dd6685f1343e | 780 | } else if(cSelected <0) { |
domplatypus | 0:dd6685f1343e | 781 | cSelected = 2; |
domplatypus | 0:dd6685f1343e | 782 | }//sets the character selected depending to the one chosen |
domplatypus | 0:dd6685f1343e | 783 | if(cSelected ==0) { |
domplatypus | 0:dd6685f1343e | 784 | c1 = c1+ joystick.direction[1]; |
domplatypus | 0:dd6685f1343e | 785 | if(c1>25) { |
domplatypus | 0:dd6685f1343e | 786 | c1 = 0; |
domplatypus | 0:dd6685f1343e | 787 | } |
domplatypus | 0:dd6685f1343e | 788 | if(c1<0) { |
domplatypus | 0:dd6685f1343e | 789 | c1 = 25; |
domplatypus | 0:dd6685f1343e | 790 | } |
domplatypus | 0:dd6685f1343e | 791 | } |
domplatypus | 0:dd6685f1343e | 792 | if(cSelected ==1) { |
domplatypus | 0:dd6685f1343e | 793 | c2 = c2+joystick.direction[1]; |
domplatypus | 0:dd6685f1343e | 794 | if(c2>25) { |
domplatypus | 0:dd6685f1343e | 795 | c2 = 0; |
domplatypus | 0:dd6685f1343e | 796 | } |
domplatypus | 0:dd6685f1343e | 797 | if(c2<0) { |
domplatypus | 0:dd6685f1343e | 798 | c2 = 25; |
domplatypus | 0:dd6685f1343e | 799 | } |
domplatypus | 0:dd6685f1343e | 800 | } |
domplatypus | 0:dd6685f1343e | 801 | if(cSelected ==2) { |
domplatypus | 0:dd6685f1343e | 802 | c3 = c3 + joystick.direction[1]; |
domplatypus | 0:dd6685f1343e | 803 | if(c3>25) { |
domplatypus | 0:dd6685f1343e | 804 | c3 = 0; |
domplatypus | 0:dd6685f1343e | 805 | } |
domplatypus | 0:dd6685f1343e | 806 | if(c3<0) { |
domplatypus | 0:dd6685f1343e | 807 | c3 = 25; |
domplatypus | 0:dd6685f1343e | 808 | } |
domplatypus | 0:dd6685f1343e | 809 | } |
domplatypus | 0:dd6685f1343e | 810 | char name[3]; |
domplatypus | 0:dd6685f1343e | 811 | //converts the name chars into a string |
domplatypus | 0:dd6685f1343e | 812 | sprintf(name,"%c%c%c",alphabet[c1],alphabet[c2],alphabet[c3]); |
domplatypus | 0:dd6685f1343e | 813 | lcd.clear(); |
domplatypus | 0:dd6685f1343e | 814 | //sets the characters |
domplatypus | 0:dd6685f1343e | 815 | lcd.printString("Game Over",10,0); |
domplatypus | 0:dd6685f1343e | 816 | lcd.printString("Your score was",0,1); |
domplatypus | 0:dd6685f1343e | 817 | lcd.printString(scoreString,25,2); |
domplatypus | 0:dd6685f1343e | 818 | lcd.printString(name,25,4); |
domplatypus | 0:dd6685f1343e | 819 | for(int i = 0; i<5; i++) { |
domplatypus | 0:dd6685f1343e | 820 | ///draws a line under the selected char |
domplatypus | 0:dd6685f1343e | 821 | lcd.setPixel(25+i+cSelected*6,40); |
domplatypus | 0:dd6685f1343e | 822 | } |
domplatypus | 0:dd6685f1343e | 823 | lcd.refresh(); |
domplatypus | 0:dd6685f1343e | 824 | if(buttonFlag) { |
domplatypus | 0:dd6685f1343e | 825 | //if button is pressed write the players ID to the drive |
domplatypus | 0:dd6685f1343e | 826 | writeDataToFile(score,alphabet[c1],alphabet[c2],alphabet[c3]); |
domplatypus | 0:dd6685f1343e | 827 | //open the menu |
domplatypus | 0:dd6685f1343e | 828 | menuInit(); |
domplatypus | 0:dd6685f1343e | 829 | } |
domplatypus | 0:dd6685f1343e | 830 | //reset flag |
domplatypus | 0:dd6685f1343e | 831 | buttonFlag = 0; |
domplatypus | 0:dd6685f1343e | 832 | } |
domplatypus | 0:dd6685f1343e | 833 | void helpInit() |
domplatypus | 0:dd6685f1343e | 834 | { |
domplatypus | 0:dd6685f1343e | 835 | //clear the array |
domplatypus | 0:dd6685f1343e | 836 | clearArray(); |
domplatypus | 0:dd6685f1343e | 837 | //clear the lcd |
domplatypus | 0:dd6685f1343e | 838 | lcd.clear(); |
domplatypus | 0:dd6685f1343e | 839 | //initialise the spawn position, although spawn positions are not needed this function is required to initialise an asteroid object |
domplatypus | 0:dd6685f1343e | 840 | initSpawn(); |
domplatypus | 0:dd6685f1343e | 841 | ship.init(); |
domplatypus | 0:dd6685f1343e | 842 | asteroidM[0].init(); |
domplatypus | 0:dd6685f1343e | 843 | item.init(); |
domplatypus | 0:dd6685f1343e | 844 | menuMode = 0; |
domplatypus | 0:dd6685f1343e | 845 | gameOverMode = 0; |
domplatypus | 0:dd6685f1343e | 846 | helpMode =1; |
domplatypus | 0:dd6685f1343e | 847 | gameOverMode =0; |
domplatypus | 0:dd6685f1343e | 848 | highScoreMode =0; |
domplatypus | 0:dd6685f1343e | 849 | //adding the asteroid, ship and item objects to the screen |
domplatypus | 0:dd6685f1343e | 850 | ship.position[0] = 0; |
domplatypus | 0:dd6685f1343e | 851 | ship.position[1] = 0; |
domplatypus | 0:dd6685f1343e | 852 | ship.addSelf(); |
domplatypus | 0:dd6685f1343e | 853 | ship.shield = 0; |
domplatypus | 0:dd6685f1343e | 854 | ship.shieldUpdate(); |
domplatypus | 0:dd6685f1343e | 855 | ship.position[0] = 0; |
domplatypus | 0:dd6685f1343e | 856 | ship.position[1] = 8; |
domplatypus | 0:dd6685f1343e | 857 | ship.addSelf(); |
domplatypus | 0:dd6685f1343e | 858 | asteroidM[0].position[0] = 0; |
domplatypus | 0:dd6685f1343e | 859 | asteroidM[0].position[1] = 16; |
domplatypus | 0:dd6685f1343e | 860 | asteroidM[0].addSelf(); |
domplatypus | 0:dd6685f1343e | 861 | item.position[0] = 0; |
domplatypus | 0:dd6685f1343e | 862 | item.position[1] = 24; |
domplatypus | 0:dd6685f1343e | 863 | item.addSelf(); |
domplatypus | 0:dd6685f1343e | 864 | setScreen();//setting the matrix |
domplatypus | 0:dd6685f1343e | 865 | lcd.printString("Player ship",10,0); //adding text to label the objects |
domplatypus | 0:dd6685f1343e | 866 | lcd.printString("Vulnerable",10,1); |
domplatypus | 0:dd6685f1343e | 867 | lcd.printString("Asteroid",10,2); |
domplatypus | 0:dd6685f1343e | 868 | lcd.printString("Rapid fire",10,3); |
domplatypus | 0:dd6685f1343e | 869 | lcd.printString("Avoid and kill",0,4); |
domplatypus | 0:dd6685f1343e | 870 | lcd.printString("the asteroids",0,5); |
domplatypus | 0:dd6685f1343e | 871 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 872 | timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 873 | } |
domplatypus | 0:dd6685f1343e | 874 | void menuInit() |
domplatypus | 0:dd6685f1343e | 875 | { |
domplatypus | 0:dd6685f1343e | 876 | clearArray(); |
domplatypus | 0:dd6685f1343e | 877 | ship.init(); |
domplatypus | 0:dd6685f1343e | 878 | ship.position[0] = 75; |
domplatypus | 0:dd6685f1343e | 879 | ship.position[1] = 2; |
domplatypus | 0:dd6685f1343e | 880 | ship.addSelf(); |
domplatypus | 0:dd6685f1343e | 881 | setScreen(); |
domplatypus | 0:dd6685f1343e | 882 | lcd.printString("Space Evader",0,0); |
domplatypus | 0:dd6685f1343e | 883 | lcd.printString("Start Game",10,2); |
domplatypus | 0:dd6685f1343e | 884 | lcd.printString("HighScores",10,3); |
domplatypus | 0:dd6685f1343e | 885 | lcd.printString("Help",10,4); |
domplatypus | 0:dd6685f1343e | 886 | lcd.printString("Settings",10,5); |
domplatypus | 0:dd6685f1343e | 887 | settingsMode = 0; |
domplatypus | 0:dd6685f1343e | 888 | gameOverMode = 0; |
domplatypus | 0:dd6685f1343e | 889 | gameMode = 0; |
domplatypus | 0:dd6685f1343e | 890 | menuMode = 1; |
domplatypus | 0:dd6685f1343e | 891 | introMode = 0; |
domplatypus | 0:dd6685f1343e | 892 | helpMode = 0; |
domplatypus | 0:dd6685f1343e | 893 | buttonFlag =0; |
domplatypus | 0:dd6685f1343e | 894 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 895 | timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 896 | } |
domplatypus | 0:dd6685f1343e | 897 | void introInit() |
domplatypus | 0:dd6685f1343e | 898 | { |
domplatypus | 0:dd6685f1343e | 899 | introCounter= 0; |
domplatypus | 0:dd6685f1343e | 900 | introMode =1; |
domplatypus | 0:dd6685f1343e | 901 | settingsMode = 0; |
domplatypus | 0:dd6685f1343e | 902 | gameOverMode = 0; |
domplatypus | 0:dd6685f1343e | 903 | gameMode = 0; |
domplatypus | 0:dd6685f1343e | 904 | menuMode = 0; |
domplatypus | 0:dd6685f1343e | 905 | helpMode = 0; |
domplatypus | 0:dd6685f1343e | 906 | buttonFlag = 0; |
domplatypus | 0:dd6685f1343e | 907 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 908 | timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 909 | } |
domplatypus | 0:dd6685f1343e | 910 | void introSet() //intro mode frame set |
domplatypus | 0:dd6685f1343e | 911 | { |
domplatypus | 0:dd6685f1343e | 912 | if(introCounter<=100) { |
domplatypus | 0:dd6685f1343e | 913 | //play this for first 100 frames |
domplatypus | 0:dd6685f1343e | 914 | lcd.printString("Dominic Platt",0,0); |
domplatypus | 0:dd6685f1343e | 915 | lcd.printString("Presents...",12,1); |
domplatypus | 0:dd6685f1343e | 916 | } |
domplatypus | 0:dd6685f1343e | 917 | if(introCounter==100) { |
domplatypus | 0:dd6685f1343e | 918 | //initialise and set ship |
domplatypus | 0:dd6685f1343e | 919 | ship.init(); |
domplatypus | 0:dd6685f1343e | 920 | ship.position[0] = 0; |
domplatypus | 0:dd6685f1343e | 921 | ship.position[1] =34; |
domplatypus | 0:dd6685f1343e | 922 | ship.addSelf(); |
domplatypus | 0:dd6685f1343e | 923 | } |
domplatypus | 0:dd6685f1343e | 924 | if(introCounter>100&introCounter<180) { |
domplatypus | 0:dd6685f1343e | 925 | //move ship for next 80 frames |
domplatypus | 0:dd6685f1343e | 926 | ship.moveSelf(1,0); |
domplatypus | 0:dd6685f1343e | 927 | setScreen(); |
domplatypus | 0:dd6685f1343e | 928 | lcd.printString("Space Evader",5,0); |
domplatypus | 0:dd6685f1343e | 929 | |
domplatypus | 0:dd6685f1343e | 930 | } |
domplatypus | 0:dd6685f1343e | 931 | if(introCounter==180||buttonFlag) { // if timer up or button pressed |
domplatypus | 0:dd6685f1343e | 932 | //starts menu |
domplatypus | 0:dd6685f1343e | 933 | ship.deleteSelf(); |
domplatypus | 0:dd6685f1343e | 934 | menuInit(); |
domplatypus | 0:dd6685f1343e | 935 | } |
domplatypus | 0:dd6685f1343e | 936 | introCounter++; //introCounter |
domplatypus | 0:dd6685f1343e | 937 | } |
domplatypus | 0:dd6685f1343e | 938 | void roundInit() |
domplatypus | 0:dd6685f1343e | 939 | { |
domplatypus | 0:dd6685f1343e | 940 | //initialises a round |
domplatypus | 0:dd6685f1343e | 941 | if(roundNumber>8) { |
domplatypus | 0:dd6685f1343e | 942 | roundNumber = 1; // reset the roundNumber |
domplatypus | 0:dd6685f1343e | 943 | if(speedState<4) { |
domplatypus | 0:dd6685f1343e | 944 | speedState++;//increases the speed of the game if it hasn't exceeded its max value |
domplatypus | 0:dd6685f1343e | 945 | } |
domplatypus | 0:dd6685f1343e | 946 | ship.shield=1; // regenerate shields when new round initiates |
domplatypus | 0:dd6685f1343e | 947 | ship.shieldUpdate(); |
domplatypus | 0:dd6685f1343e | 948 | } |
domplatypus | 0:dd6685f1343e | 949 | initSpawn(); // initialises spawn positions |
domplatypus | 0:dd6685f1343e | 950 | for(int i = 0; i<roundNumber; i++) { |
domplatypus | 0:dd6685f1343e | 951 | asteroidM[i].init();//initiates asteroids equal to the round number |
domplatypus | 0:dd6685f1343e | 952 | asteroidM[i].active = 1; |
domplatypus | 0:dd6685f1343e | 953 | asteroids++; // add one to the counter |
domplatypus | 0:dd6685f1343e | 954 | } |
domplatypus | 0:dd6685f1343e | 955 | roundNumber++; //increment roundNumber |
domplatypus | 0:dd6685f1343e | 956 | } |
domplatypus | 0:dd6685f1343e | 957 | void shipExplode() |
domplatypus | 0:dd6685f1343e | 958 | { |
domplatypus | 0:dd6685f1343e | 959 | //sets the explotionn frame of the ship |
domplatypus | 0:dd6685f1343e | 960 | ship.destroySelf(explotionStage); |
domplatypus | 0:dd6685f1343e | 961 | explotionStage++;//increment the explotionStage |
domplatypus | 0:dd6685f1343e | 962 | if(explotionStage>12) { |
domplatypus | 0:dd6685f1343e | 963 | //explotion finished gameOverScreenSet |
domplatypus | 0:dd6685f1343e | 964 | shipExplodeFlag = 0; |
domplatypus | 0:dd6685f1343e | 965 | gameOverInit(); |
domplatypus | 0:dd6685f1343e | 966 | } |
domplatypus | 0:dd6685f1343e | 967 | } |
domplatypus | 0:dd6685f1343e | 968 | void settingsSet() |
domplatypus | 0:dd6685f1343e | 969 | { |
domplatypus | 0:dd6685f1343e | 970 | //sets the title selected with the joystick |
domplatypus | 0:dd6685f1343e | 971 | titleSelected = titleSelected + joystick.direction[1]; |
domplatypus | 0:dd6685f1343e | 972 | if(titleSelected>3) { |
domplatypus | 0:dd6685f1343e | 973 | titleSelected = 0; |
domplatypus | 0:dd6685f1343e | 974 | } else if(titleSelected<0) { |
domplatypus | 0:dd6685f1343e | 975 | titleSelected = 3; |
domplatypus | 0:dd6685f1343e | 976 | } |
domplatypus | 0:dd6685f1343e | 977 | if((joystick.direction[1]==1)||(joystick.direction[1]==-1)) { |
domplatypus | 0:dd6685f1343e | 978 | soundType =3; //sound when joystick moved |
domplatypus | 0:dd6685f1343e | 979 | initSound(); |
domplatypus | 0:dd6685f1343e | 980 | } |
domplatypus | 0:dd6685f1343e | 981 | lcd.clear(); |
domplatypus | 0:dd6685f1343e | 982 | lcd.printString("Sound",7,0); |
domplatypus | 0:dd6685f1343e | 983 | lcd.printString("On",45,0); |
domplatypus | 0:dd6685f1343e | 984 | lcd.printString("Off",65,0); |
domplatypus | 0:dd6685f1343e | 985 | if(sound) {//sets the marker depending on whether sound is on or not |
domplatypus | 0:dd6685f1343e | 986 | lcd.printString(" ",59,0); |
domplatypus | 0:dd6685f1343e | 987 | lcd.printString("-",39,0); |
domplatypus | 0:dd6685f1343e | 988 | } else { |
domplatypus | 0:dd6685f1343e | 989 | lcd.printString(" ",39,0); |
domplatypus | 0:dd6685f1343e | 990 | lcd.printString("-",59,0); |
domplatypus | 0:dd6685f1343e | 991 | } |
domplatypus | 0:dd6685f1343e | 992 | lcd.printString("Speed",7,1); |
domplatypus | 0:dd6685f1343e | 993 | char myChar[8]; |
domplatypus | 0:dd6685f1343e | 994 | sprintf(myChar,"%dx",1+speedState); //speed marked |
domplatypus | 0:dd6685f1343e | 995 | lcd.printString(myChar,60,1); |
domplatypus | 0:dd6685f1343e | 996 | lcd.printString("Walls",7,2); |
domplatypus | 0:dd6685f1343e | 997 | lcd.printString(">",0,titleSelected); //selector depending on option selected |
domplatypus | 0:dd6685f1343e | 998 | char bufferDate[14]; //presents the time and date |
domplatypus | 0:dd6685f1343e | 999 | char bufferTime[14]; |
domplatypus | 0:dd6685f1343e | 1000 | lcd.printString("Back",7,3); |
domplatypus | 0:dd6685f1343e | 1001 | time_t seconds = time(NULL); // get current time |
domplatypus | 0:dd6685f1343e | 1002 | //format time into a string (time and date) |
domplatypus | 0:dd6685f1343e | 1003 | strftime(bufferDate,14,"%D", localtime(&seconds)); |
domplatypus | 0:dd6685f1343e | 1004 | strftime(bufferTime,14,"%T", localtime(&seconds)); |
domplatypus | 0:dd6685f1343e | 1005 | lcd.printString(bufferDate,7,4); |
domplatypus | 0:dd6685f1343e | 1006 | lcd.printString(bufferTime,7,5); |
domplatypus | 0:dd6685f1343e | 1007 | if(buttonFlag&(titleSelected == 0)) { |
domplatypus | 0:dd6685f1343e | 1008 | sound = !sound; |
domplatypus | 0:dd6685f1343e | 1009 | } else if(buttonFlag&(titleSelected == 1)) { |
domplatypus | 0:dd6685f1343e | 1010 | //increments the speed counter if that option is selected |
domplatypus | 0:dd6685f1343e | 1011 | speedState++; |
domplatypus | 0:dd6685f1343e | 1012 | if(speedState>3) { |
domplatypus | 0:dd6685f1343e | 1013 | //reset speedState if to high |
domplatypus | 0:dd6685f1343e | 1014 | speedState=0; |
domplatypus | 0:dd6685f1343e | 1015 | } |
domplatypus | 0:dd6685f1343e | 1016 | } else if(buttonFlag&(titleSelected == 3)) { |
domplatypus | 0:dd6685f1343e | 1017 | //back to menu |
domplatypus | 0:dd6685f1343e | 1018 | menuInit(); |
domplatypus | 0:dd6685f1343e | 1019 | } else if(buttonFlag&(titleSelected == 2)) { |
domplatypus | 0:dd6685f1343e | 1020 | //turn on/off the walls |
domplatypus | 0:dd6685f1343e | 1021 | walls = !walls; |
domplatypus | 0:dd6685f1343e | 1022 | } |
domplatypus | 0:dd6685f1343e | 1023 | if(walls) {//presents whether the walls are active |
domplatypus | 0:dd6685f1343e | 1024 | lcd.printString("On",50,2); |
domplatypus | 0:dd6685f1343e | 1025 | } else { |
domplatypus | 0:dd6685f1343e | 1026 | lcd.printString("Off",50,2); |
domplatypus | 0:dd6685f1343e | 1027 | } |
domplatypus | 0:dd6685f1343e | 1028 | //reset the button flag |
domplatypus | 0:dd6685f1343e | 1029 | buttonFlag = 0; |
domplatypus | 0:dd6685f1343e | 1030 | } |
domplatypus | 0:dd6685f1343e | 1031 | void settingsInit() |
domplatypus | 0:dd6685f1343e | 1032 | { |
domplatypus | 0:dd6685f1343e | 1033 | //initialises the setting screen |
domplatypus | 0:dd6685f1343e | 1034 | menuMode = 0; |
domplatypus | 0:dd6685f1343e | 1035 | settingsMode = 1; |
domplatypus | 0:dd6685f1343e | 1036 | titleSelected = 0; |
domplatypus | 0:dd6685f1343e | 1037 | buttonFlag =0; |
domplatypus | 0:dd6685f1343e | 1038 | settingsSet(); |
domplatypus | 0:dd6685f1343e | 1039 | timer.detach(); |
domplatypus | 0:dd6685f1343e | 1040 | timer.attach(&timerExpired,0.01); |
domplatypus | 0:dd6685f1343e | 1041 | } |
domplatypus | 0:dd6685f1343e | 1042 | void serialISR() |
domplatypus | 0:dd6685f1343e | 1043 | { |
domplatypus | 0:dd6685f1343e | 1044 | //sets UNIX time |
domplatypus | 0:dd6685f1343e | 1045 | set_time(1430595271); |
domplatypus | 0:dd6685f1343e | 1046 | } |
domplatypus | 0:dd6685f1343e | 1047 | void buttonPressed() // toggle direction in ISR |
domplatypus | 0:dd6685f1343e | 1048 | { |
domplatypus | 0:dd6685f1343e | 1049 | if(debounce.read_ms()>150) {//only set flag 150 ms after the last flag was set to filter out the 'bounces' |
domplatypus | 0:dd6685f1343e | 1050 | //buttonFlag set when button is pressed |
domplatypus | 0:dd6685f1343e | 1051 | buttonFlag = 1; |
domplatypus | 0:dd6685f1343e | 1052 | debounce.reset();//reset the debounce timer |
domplatypus | 0:dd6685f1343e | 1053 | } |
domplatypus | 0:dd6685f1343e | 1054 | } |