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