![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
main.cpp@5:1bf7c83f86cc, 2017-05-04 (annotated)
- Committer:
- el15ss
- Date:
- Thu May 04 09:50:18 2017 +0000
- Revision:
- 5:1bf7c83f86cc
- Parent:
- 4:2fdafb53eac2
- Child:
- 6:bf601a65cb27
Got rid of wait() functions and added sleep(). Cleaned up the code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15ss | 1:db9ff66f67c8 | 1 | /***************************************************** |
el15ss | 1:db9ff66f67c8 | 2 | Libraries and modules used * |
el15ss | 1:db9ff66f67c8 | 3 | ******************************************************/ |
el15ss | 0:12cfe63faa6a | 4 | #include "mbed.h" |
el15ss | 0:12cfe63faa6a | 5 | #include "Gamepad.h" |
el15ss | 0:12cfe63faa6a | 6 | #include "N5110.h" |
el15ss | 0:12cfe63faa6a | 7 | #include "Character.h" |
el15ss | 0:12cfe63faa6a | 8 | #include "Obstacles.h" |
el15ss | 0:12cfe63faa6a | 9 | #include "Gems.h" |
el15ss | 2:98a41609c827 | 10 | #include "SDFileSystem.h" |
el15ss | 2:98a41609c827 | 11 | |
el15ss | 0:12cfe63faa6a | 12 | |
el15ss | 1:db9ff66f67c8 | 13 | #define No_OBS 8 |
el15ss | 1:db9ff66f67c8 | 14 | #define No_GEMS 4 |
el15ss | 0:12cfe63faa6a | 15 | |
el15ss | 3:0c690f1c04d8 | 16 | //Variables |
el15ss | 3:0c690f1c04d8 | 17 | // i - to loop through the obstacles |
el15ss | 3:0c690f1c04d8 | 18 | // j - to loop through the gems |
el15ss | 3:0c690f1c04d8 | 19 | // counter - to keep track of score |
el15ss | 3:0c690f1c04d8 | 20 | // highScore - to store high score |
el15ss | 2:98a41609c827 | 21 | int i,j,counter,highScore; |
el15ss | 3:0c690f1c04d8 | 22 | |
el15ss | 3:0c690f1c04d8 | 23 | //To helo convert counter(int) to string to display on the screen |
el15ss | 2:98a41609c827 | 24 | char score[50]; |
el15ss | 0:12cfe63faa6a | 25 | |
el15ss | 1:db9ff66f67c8 | 26 | //Structs |
el15ss | 5:1bf7c83f86cc | 27 | struct UserInput |
el15ss | 5:1bf7c83f86cc | 28 | { |
el15ss | 0:12cfe63faa6a | 29 | Direction d; |
el15ss | 0:12cfe63faa6a | 30 | float mag; |
el15ss | 0:12cfe63faa6a | 31 | }; |
el15ss | 1:db9ff66f67c8 | 32 | /* Class Objects */ |
el15ss | 0:12cfe63faa6a | 33 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el15ss | 0:12cfe63faa6a | 34 | Gamepad pad; |
el15ss | 0:12cfe63faa6a | 35 | Character c; |
el15ss | 1:db9ff66f67c8 | 36 | Obstacles obstacle[No_OBS]; |
el15ss | 3:0c690f1c04d8 | 37 | Gems gems[No_GEMS]; |
el15ss | 0:12cfe63faa6a | 38 | |
el15ss | 2:98a41609c827 | 39 | SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd"); |
el15ss | 2:98a41609c827 | 40 | FILE *file; |
el15ss | 2:98a41609c827 | 41 | |
el15ss | 0:12cfe63faa6a | 42 | |
el15ss | 1:db9ff66f67c8 | 43 | /* Function Prototypes */ |
el15ss | 0:12cfe63faa6a | 44 | void init(); |
el15ss | 0:12cfe63faa6a | 45 | void update_game(UserInput input); |
el15ss | 0:12cfe63faa6a | 46 | void render(); |
el15ss | 0:12cfe63faa6a | 47 | void welcome(); |
el15ss | 0:12cfe63faa6a | 48 | void menu(); |
el15ss | 0:12cfe63faa6a | 49 | void over(); |
el15ss | 0:12cfe63faa6a | 50 | void Instructions(); |
el15ss | 3:0c690f1c04d8 | 51 | void stickRunner(); |
el15ss | 3:0c690f1c04d8 | 52 | void displayHighScore(); |
el15ss | 1:db9ff66f67c8 | 53 | /* Functions */ |
el15ss | 1:db9ff66f67c8 | 54 | |
el15ss | 0:12cfe63faa6a | 55 | int main() |
el15ss | 0:12cfe63faa6a | 56 | { |
el15ss | 3:0c690f1c04d8 | 57 | |
el15ss | 0:12cfe63faa6a | 58 | |
el15ss | 1:db9ff66f67c8 | 59 | /* Intialization */ |
el15ss | 0:12cfe63faa6a | 60 | init(); |
el15ss | 1:db9ff66f67c8 | 61 | |
el15ss | 5:1bf7c83f86cc | 62 | /* Drawing the initial frame */ |
el15ss | 0:12cfe63faa6a | 63 | welcome(); |
el15ss | 3:0c690f1c04d8 | 64 | |
el15ss | 3:0c690f1c04d8 | 65 | } |
el15ss | 3:0c690f1c04d8 | 66 | |
el15ss | 3:0c690f1c04d8 | 67 | |
el15ss | 3:0c690f1c04d8 | 68 | void init() |
el15ss | 3:0c690f1c04d8 | 69 | { |
el15ss | 3:0c690f1c04d8 | 70 | //Need to initialize the lcd and gamepad |
el15ss | 3:0c690f1c04d8 | 71 | lcd.init(); |
el15ss | 3:0c690f1c04d8 | 72 | pad.init(); |
el15ss | 3:0c690f1c04d8 | 73 | |
el15ss | 3:0c690f1c04d8 | 74 | //Intialzing the charachter |
el15ss | 3:0c690f1c04d8 | 75 | c.init(); |
el15ss | 3:0c690f1c04d8 | 76 | |
el15ss | 3:0c690f1c04d8 | 77 | //Intialzing the obstacles |
el15ss | 3:0c690f1c04d8 | 78 | for(i=0;i<No_OBS;i++) |
el15ss | 3:0c690f1c04d8 | 79 | { |
el15ss | 3:0c690f1c04d8 | 80 | obstacle[i].init(); |
el15ss | 3:0c690f1c04d8 | 81 | } |
el15ss | 3:0c690f1c04d8 | 82 | |
el15ss | 3:0c690f1c04d8 | 83 | //Intialzing the gems |
el15ss | 3:0c690f1c04d8 | 84 | for(j=0;j<No_GEMS;j++) |
el15ss | 3:0c690f1c04d8 | 85 | { |
el15ss | 3:0c690f1c04d8 | 86 | gems[j].init(); |
el15ss | 3:0c690f1c04d8 | 87 | } |
el15ss | 3:0c690f1c04d8 | 88 | |
el15ss | 3:0c690f1c04d8 | 89 | |
el15ss | 3:0c690f1c04d8 | 90 | |
el15ss | 3:0c690f1c04d8 | 91 | } |
el15ss | 3:0c690f1c04d8 | 92 | |
el15ss | 3:0c690f1c04d8 | 93 | |
el15ss | 3:0c690f1c04d8 | 94 | |
el15ss | 3:0c690f1c04d8 | 95 | //Funstion to display the Welcome page |
el15ss | 5:1bf7c83f86cc | 96 | void welcome() |
el15ss | 5:1bf7c83f86cc | 97 | { |
el15ss | 3:0c690f1c04d8 | 98 | |
el15ss | 3:0c690f1c04d8 | 99 | |
el15ss | 3:0c690f1c04d8 | 100 | |
el15ss | 3:0c690f1c04d8 | 101 | lcd.printString("Stick Runner! ",0,1); |
el15ss | 3:0c690f1c04d8 | 102 | lcd.printString(" Press Start ",0,4); |
el15ss | 3:0c690f1c04d8 | 103 | lcd.refresh(); |
el15ss | 3:0c690f1c04d8 | 104 | // pad.tone(1500.0,0.5); |
el15ss | 3:0c690f1c04d8 | 105 | // pad.tone(1500.0,0.5); |
el15ss | 3:0c690f1c04d8 | 106 | |
el15ss | 3:0c690f1c04d8 | 107 | |
el15ss | 3:0c690f1c04d8 | 108 | //Flashes LEDS aslong as START is not pressed |
el15ss | 3:0c690f1c04d8 | 109 | while ( pad.check_event(Gamepad::START_PRESSED) == false) |
el15ss | 3:0c690f1c04d8 | 110 | { |
el15ss | 3:0c690f1c04d8 | 111 | pad.leds_on(); |
el15ss | 3:0c690f1c04d8 | 112 | wait(0.1); |
el15ss | 3:0c690f1c04d8 | 113 | pad.leds_off(); |
el15ss | 3:0c690f1c04d8 | 114 | wait(0.1); |
el15ss | 3:0c690f1c04d8 | 115 | |
el15ss | 3:0c690f1c04d8 | 116 | } |
el15ss | 3:0c690f1c04d8 | 117 | |
el15ss | 3:0c690f1c04d8 | 118 | menu(); |
el15ss | 3:0c690f1c04d8 | 119 | } |
el15ss | 3:0c690f1c04d8 | 120 | |
el15ss | 3:0c690f1c04d8 | 121 | |
el15ss | 0:12cfe63faa6a | 122 | |
el15ss | 3:0c690f1c04d8 | 123 | //Function to display the Menu page |
el15ss | 5:1bf7c83f86cc | 124 | void menu() |
el15ss | 5:1bf7c83f86cc | 125 | { |
el15ss | 5:1bf7c83f86cc | 126 | |
el15ss | 0:12cfe63faa6a | 127 | |
el15ss | 5:1bf7c83f86cc | 128 | |
el15ss | 5:1bf7c83f86cc | 129 | lcd.clear(); |
el15ss | 3:0c690f1c04d8 | 130 | lcd.printString(" Menu ",0,0); |
el15ss | 3:0c690f1c04d8 | 131 | lcd.printString("A)New Game ",0,2); |
el15ss | 3:0c690f1c04d8 | 132 | lcd.printString("B)Continue ",0,3); |
el15ss | 3:0c690f1c04d8 | 133 | lcd.printString("X)Instructions ",0,4); |
el15ss | 3:0c690f1c04d8 | 134 | lcd.printString("Y)High Score ",0,5); |
el15ss | 3:0c690f1c04d8 | 135 | |
el15ss | 3:0c690f1c04d8 | 136 | lcd.refresh(); |
el15ss | 3:0c690f1c04d8 | 137 | |
el15ss | 3:0c690f1c04d8 | 138 | |
el15ss | 5:1bf7c83f86cc | 139 | while(1) |
el15ss | 5:1bf7c83f86cc | 140 | { |
el15ss | 5:1bf7c83f86cc | 141 | // wait flashing LEDs until start button is pressed |
el15ss | 5:1bf7c83f86cc | 142 | //Condition to start a new game |
el15ss | 5:1bf7c83f86cc | 143 | if( pad.check_event(Gamepad::A_PRESSED) ) |
el15ss | 5:1bf7c83f86cc | 144 | { |
el15ss | 5:1bf7c83f86cc | 145 | //pad.tone(1000.0,0.5); |
el15ss | 5:1bf7c83f86cc | 146 | |
el15ss | 3:0c690f1c04d8 | 147 | |
el15ss | 5:1bf7c83f86cc | 148 | //Clear, refresh and intialize the game again so we can start a new game |
el15ss | 5:1bf7c83f86cc | 149 | lcd.clear(); |
el15ss | 5:1bf7c83f86cc | 150 | lcd.refresh(); |
el15ss | 5:1bf7c83f86cc | 151 | init(); |
el15ss | 5:1bf7c83f86cc | 152 | stickRunner(); |
el15ss | 3:0c690f1c04d8 | 153 | |
el15ss | 5:1bf7c83f86cc | 154 | |
el15ss | 5:1bf7c83f86cc | 155 | } |
el15ss | 3:0c690f1c04d8 | 156 | |
el15ss | 5:1bf7c83f86cc | 157 | //To continue the same game |
el15ss | 5:1bf7c83f86cc | 158 | else if( pad.check_event(Gamepad::B_PRESSED) ) |
el15ss | 5:1bf7c83f86cc | 159 | { |
el15ss | 5:1bf7c83f86cc | 160 | |
el15ss | 5:1bf7c83f86cc | 161 | // pad.tone(1000.0,0.5); |
el15ss | 5:1bf7c83f86cc | 162 | |
el15ss | 3:0c690f1c04d8 | 163 | |
el15ss | 5:1bf7c83f86cc | 164 | //Simply refreshes the page and continues from where the user left the game |
el15ss | 5:1bf7c83f86cc | 165 | // as the intialize function init() is not called again |
el15ss | 5:1bf7c83f86cc | 166 | lcd.refresh(); |
el15ss | 5:1bf7c83f86cc | 167 | stickRunner(); |
el15ss | 3:0c690f1c04d8 | 168 | |
el15ss | 3:0c690f1c04d8 | 169 | |
el15ss | 5:1bf7c83f86cc | 170 | } |
el15ss | 3:0c690f1c04d8 | 171 | |
el15ss | 5:1bf7c83f86cc | 172 | //To read the game instructions |
el15ss | 5:1bf7c83f86cc | 173 | else if( pad.check_event(Gamepad::X_PRESSED) ) |
el15ss | 5:1bf7c83f86cc | 174 | { |
el15ss | 5:1bf7c83f86cc | 175 | // pad.tone(1000.0,0.5); |
el15ss | 5:1bf7c83f86cc | 176 | Instructions(); |
el15ss | 3:0c690f1c04d8 | 177 | |
el15ss | 5:1bf7c83f86cc | 178 | |
el15ss | 5:1bf7c83f86cc | 179 | } |
el15ss | 3:0c690f1c04d8 | 180 | |
el15ss | 5:1bf7c83f86cc | 181 | //To see the game high score |
el15ss | 5:1bf7c83f86cc | 182 | else if( pad.check_event(Gamepad::Y_PRESSED) ) |
el15ss | 5:1bf7c83f86cc | 183 | { |
el15ss | 5:1bf7c83f86cc | 184 | //pad.tone(1000.0,0.5); |
el15ss | 5:1bf7c83f86cc | 185 | displayHighScore(); |
el15ss | 3:0c690f1c04d8 | 186 | |
el15ss | 5:1bf7c83f86cc | 187 | |
el15ss | 5:1bf7c83f86cc | 188 | |
el15ss | 5:1bf7c83f86cc | 189 | } |
el15ss | 5:1bf7c83f86cc | 190 | |
el15ss | 5:1bf7c83f86cc | 191 | sleep(); |
el15ss | 3:0c690f1c04d8 | 192 | |
el15ss | 3:0c690f1c04d8 | 193 | |
el15ss | 5:1bf7c83f86cc | 194 | } |
el15ss | 3:0c690f1c04d8 | 195 | } |
el15ss | 3:0c690f1c04d8 | 196 | |
el15ss | 3:0c690f1c04d8 | 197 | |
el15ss | 3:0c690f1c04d8 | 198 | |
el15ss | 3:0c690f1c04d8 | 199 | |
el15ss | 3:0c690f1c04d8 | 200 | //This function is responsible for running the game |
el15ss | 3:0c690f1c04d8 | 201 | void stickRunner() |
el15ss | 3:0c690f1c04d8 | 202 | { |
el15ss | 3:0c690f1c04d8 | 203 | int fps = 8; |
el15ss | 3:0c690f1c04d8 | 204 | |
el15ss | 5:1bf7c83f86cc | 205 | render(); |
el15ss | 5:1bf7c83f86cc | 206 | wait(1.0f/fps); |
el15ss | 0:12cfe63faa6a | 207 | |
el15ss | 1:db9ff66f67c8 | 208 | /* Main game loop to read input, render the display and update the game state */ |
el15ss | 3:0c690f1c04d8 | 209 | |
el15ss | 5:1bf7c83f86cc | 210 | while (1) |
el15ss | 5:1bf7c83f86cc | 211 | { |
el15ss | 0:12cfe63faa6a | 212 | |
el15ss | 3:0c690f1c04d8 | 213 | //As long as the character survives update the score |
el15ss | 2:98a41609c827 | 214 | counter++; |
el15ss | 0:12cfe63faa6a | 215 | |
el15ss | 3:0c690f1c04d8 | 216 | //Using the gamepad library to move the character using the joystick |
el15ss | 1:db9ff66f67c8 | 217 | c.updateCharacter(pad.get_direction(),pad.get_mag()); |
el15ss | 3:0c690f1c04d8 | 218 | |
el15ss | 3:0c690f1c04d8 | 219 | //Condition to ckeck if the user wants to pause the game |
el15ss | 0:12cfe63faa6a | 220 | if(pad.check_event(Gamepad::BACK_PRESSED)) |
el15ss | 0:12cfe63faa6a | 221 | { |
el15ss | 0:12cfe63faa6a | 222 | lcd.clear(); |
el15ss | 0:12cfe63faa6a | 223 | lcd.refresh(); |
el15ss | 1:db9ff66f67c8 | 224 | menu(); |
el15ss | 0:12cfe63faa6a | 225 | } |
el15ss | 0:12cfe63faa6a | 226 | |
el15ss | 3:0c690f1c04d8 | 227 | //Loop to make the generation of obstacles a continious loop and also to check if the user has been killed |
el15ss | 1:db9ff66f67c8 | 228 | for(i=0;i<No_OBS;i++) |
el15ss | 0:12cfe63faa6a | 229 | { |
el15ss | 3:0c690f1c04d8 | 230 | //To retrieve the status of the obstacle on the screen |
el15ss | 1:db9ff66f67c8 | 231 | obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); |
el15ss | 0:12cfe63faa6a | 232 | |
el15ss | 1:db9ff66f67c8 | 233 | if(obstacle[i].getObstacleStatus() == false) |
el15ss | 0:12cfe63faa6a | 234 | { |
el15ss | 0:12cfe63faa6a | 235 | obstacle[i].init(); |
el15ss | 0:12cfe63faa6a | 236 | } |
el15ss | 3:0c690f1c04d8 | 237 | |
el15ss | 3:0c690f1c04d8 | 238 | //To check whether the character has been hit by an obstacle by comparing the position of each obstacle |
el15ss | 3:0c690f1c04d8 | 239 | // relative to the character |
el15ss | 5:1bf7c83f86cc | 240 | c.characterStatus(obstacle[i].getObstaclePos()); |
el15ss | 3:0c690f1c04d8 | 241 | |
el15ss | 1:db9ff66f67c8 | 242 | |
el15ss | 0:12cfe63faa6a | 243 | } |
el15ss | 0:12cfe63faa6a | 244 | |
el15ss | 3:0c690f1c04d8 | 245 | //Loop to make the generation of gems a continious loop and also to check if the user has collected them |
el15ss | 1:db9ff66f67c8 | 246 | for(j=0;j<No_GEMS;j++) |
el15ss | 0:12cfe63faa6a | 247 | { |
el15ss | 3:0c690f1c04d8 | 248 | //To check whether the character has collected a gem by comparing the position of each gem |
el15ss | 3:0c690f1c04d8 | 249 | // relative to the character |
el15ss | 1:db9ff66f67c8 | 250 | gems[j].gemStatus(c.getCharacterPos()); |
el15ss | 3:0c690f1c04d8 | 251 | |
el15ss | 3:0c690f1c04d8 | 252 | |
el15ss | 0:12cfe63faa6a | 253 | |
el15ss | 1:db9ff66f67c8 | 254 | if(gems[j].getGemStatus() == false) |
el15ss | 0:12cfe63faa6a | 255 | { |
el15ss | 0:12cfe63faa6a | 256 | gems[j].init(); |
el15ss | 2:98a41609c827 | 257 | |
el15ss | 0:12cfe63faa6a | 258 | } |
el15ss | 1:db9ff66f67c8 | 259 | |
el15ss | 1:db9ff66f67c8 | 260 | |
el15ss | 0:12cfe63faa6a | 261 | } |
el15ss | 2:98a41609c827 | 262 | |
el15ss | 2:98a41609c827 | 263 | //To make the obstacles and gems move along the screen |
el15ss | 3:0c690f1c04d8 | 264 | |
el15ss | 3:0c690f1c04d8 | 265 | |
el15ss | 0:12cfe63faa6a | 266 | i =0; |
el15ss | 0:12cfe63faa6a | 267 | |
el15ss | 1:db9ff66f67c8 | 268 | for(i=0;i<No_OBS;i++) |
el15ss | 1:db9ff66f67c8 | 269 | { |
el15ss | 1:db9ff66f67c8 | 270 | obstacle[i].updateObstacle(); |
el15ss | 1:db9ff66f67c8 | 271 | } |
el15ss | 0:12cfe63faa6a | 272 | |
el15ss | 0:12cfe63faa6a | 273 | |
el15ss | 0:12cfe63faa6a | 274 | j =0; |
el15ss | 0:12cfe63faa6a | 275 | |
el15ss | 1:db9ff66f67c8 | 276 | for(j=0;j<No_GEMS;j++) |
el15ss | 1:db9ff66f67c8 | 277 | { |
el15ss | 1:db9ff66f67c8 | 278 | gems[j].updateGems(); |
el15ss | 2:98a41609c827 | 279 | |
el15ss | 1:db9ff66f67c8 | 280 | } |
el15ss | 0:12cfe63faa6a | 281 | |
el15ss | 0:12cfe63faa6a | 282 | render(); |
el15ss | 0:12cfe63faa6a | 283 | |
el15ss | 0:12cfe63faa6a | 284 | wait(1.0f/fps); |
el15ss | 0:12cfe63faa6a | 285 | } |
el15ss | 3:0c690f1c04d8 | 286 | |
el15ss | 0:12cfe63faa6a | 287 | } |
el15ss | 0:12cfe63faa6a | 288 | |
el15ss | 1:db9ff66f67c8 | 289 | |
el15ss | 3:0c690f1c04d8 | 290 | //Function to draw out the pixels on the screen |
el15ss | 0:12cfe63faa6a | 291 | void render() |
el15ss | 0:12cfe63faa6a | 292 | { |
el15ss | 2:98a41609c827 | 293 | |
el15ss | 0:12cfe63faa6a | 294 | lcd.clear(); |
el15ss | 1:db9ff66f67c8 | 295 | |
el15ss | 3:0c690f1c04d8 | 296 | |
el15ss | 3:0c690f1c04d8 | 297 | //Only draws the character as long as it survives |
el15ss | 1:db9ff66f67c8 | 298 | if(c.getCharacterStatus()) |
el15ss | 1:db9ff66f67c8 | 299 | { |
el15ss | 1:db9ff66f67c8 | 300 | c.draw(lcd); |
el15ss | 1:db9ff66f67c8 | 301 | } |
el15ss | 2:98a41609c827 | 302 | |
el15ss | 5:1bf7c83f86cc | 303 | if(c.getCharacterStatus() == false) |
el15ss | 5:1bf7c83f86cc | 304 | { |
el15ss | 5:1bf7c83f86cc | 305 | over(); |
el15ss | 5:1bf7c83f86cc | 306 | } |
el15ss | 2:98a41609c827 | 307 | |
el15ss | 2:98a41609c827 | 308 | |
el15ss | 3:0c690f1c04d8 | 309 | //Draws the obstacles if the status returned is true |
el15ss | 0:12cfe63faa6a | 310 | |
el15ss | 1:db9ff66f67c8 | 311 | for(i=0;i<No_OBS;i++) |
el15ss | 1:db9ff66f67c8 | 312 | { |
el15ss | 1:db9ff66f67c8 | 313 | if(obstacle[i].getObstacleStatus()) |
el15ss | 1:db9ff66f67c8 | 314 | { |
el15ss | 1:db9ff66f67c8 | 315 | obstacle[i].draw(lcd); |
el15ss | 1:db9ff66f67c8 | 316 | } |
el15ss | 1:db9ff66f67c8 | 317 | } |
el15ss | 0:12cfe63faa6a | 318 | |
el15ss | 3:0c690f1c04d8 | 319 | //Draws the gems if the status returned is true |
el15ss | 5:1bf7c83f86cc | 320 | for(j=0;j<No_GEMS;j++) |
el15ss | 1:db9ff66f67c8 | 321 | { |
el15ss | 1:db9ff66f67c8 | 322 | if(gems[j].getGemStatus()) |
el15ss | 1:db9ff66f67c8 | 323 | { |
el15ss | 1:db9ff66f67c8 | 324 | gems[j].draw(lcd); |
el15ss | 1:db9ff66f67c8 | 325 | |
el15ss | 1:db9ff66f67c8 | 326 | } |
el15ss | 1:db9ff66f67c8 | 327 | |
el15ss | 3:0c690f1c04d8 | 328 | |
el15ss | 1:db9ff66f67c8 | 329 | } |
el15ss | 2:98a41609c827 | 330 | |
el15ss | 1:db9ff66f67c8 | 331 | |
el15ss | 0:12cfe63faa6a | 332 | lcd.refresh(); |
el15ss | 1:db9ff66f67c8 | 333 | |
el15ss | 0:12cfe63faa6a | 334 | } |
el15ss | 0:12cfe63faa6a | 335 | |
el15ss | 1:db9ff66f67c8 | 336 | |
el15ss | 0:12cfe63faa6a | 337 | |
el15ss | 3:0c690f1c04d8 | 338 | //Function to display end of game and also check whether the user got a new highscore and if not write it on the SD card |
el15ss | 5:1bf7c83f86cc | 339 | void over() |
el15ss | 5:1bf7c83f86cc | 340 | { |
el15ss | 3:0c690f1c04d8 | 341 | //pad.tone(1000.0,0.5); |
el15ss | 2:98a41609c827 | 342 | pad.init(); |
el15ss | 5:1bf7c83f86cc | 343 | // lcd.init(); |
el15ss | 5:1bf7c83f86cc | 344 | // lcd.clear(); |
el15ss | 3:0c690f1c04d8 | 345 | |
el15ss | 5:1bf7c83f86cc | 346 | //Mounting on the SD card to read/write in it |
el15ss | 4:2fdafb53eac2 | 347 | //sd.mount(); |
el15ss | 2:98a41609c827 | 348 | |
el15ss | 3:0c690f1c04d8 | 349 | //Converting the counter into a string 'score' to display on the lcd |
el15ss | 2:98a41609c827 | 350 | sprintf (score, " Score : %d",counter); |
el15ss | 2:98a41609c827 | 351 | |
el15ss | 2:98a41609c827 | 352 | lcd.printString(score,0,2); |
el15ss | 2:98a41609c827 | 353 | lcd.printString("GAME OVER!! ",0,0); |
el15ss | 2:98a41609c827 | 354 | //lcd.printString(" ",0,1); |
el15ss | 1:db9ff66f67c8 | 355 | |
el15ss | 3:0c690f1c04d8 | 356 | //Opening file on the SD card |
el15ss | 4:2fdafb53eac2 | 357 | /*file = fopen("/sd/scoreFile.txt", "r"); |
el15ss | 3:0c690f1c04d8 | 358 | |
el15ss | 3:0c690f1c04d8 | 359 | //If file is empty and score to it and display it as the High Score |
el15ss | 2:98a41609c827 | 360 | if(file ==NULL) |
el15ss | 2:98a41609c827 | 361 | { |
el15ss | 2:98a41609c827 | 362 | file = fopen("/sd/scoreFile.txt", "w"); |
el15ss | 2:98a41609c827 | 363 | fprintf(file,"%d",counter); |
el15ss | 2:98a41609c827 | 364 | fclose(file); |
el15ss | 5:1bf7c83f86cc | 365 | lcd.printString("HIGH SCORE",0,3); |
el15ss | 2:98a41609c827 | 366 | |
el15ss | 2:98a41609c827 | 367 | } |
el15ss | 3:0c690f1c04d8 | 368 | |
el15ss | 3:0c690f1c04d8 | 369 | //if not empty compare against the exsisting high score and display whether the user has made a new high score |
el15ss | 5:1bf7c83f86cc | 370 | else |
el15ss | 5:1bf7c83f86cc | 371 | { |
el15ss | 5:1bf7c83f86cc | 372 | fscanf(file,"%d", &highScore); |
el15ss | 5:1bf7c83f86cc | 373 | fclose(file); |
el15ss | 2:98a41609c827 | 374 | |
el15ss | 5:1bf7c83f86cc | 375 | if(counter>highScore) |
el15ss | 5:1bf7c83f86cc | 376 | { |
el15ss | 2:98a41609c827 | 377 | |
el15ss | 5:1bf7c83f86cc | 378 | file = fopen("/sd/scoreFile.txt", "w"); |
el15ss | 5:1bf7c83f86cc | 379 | fprintf(file,"%d",counter); |
el15ss | 5:1bf7c83f86cc | 380 | fclose(file); |
el15ss | 5:1bf7c83f86cc | 381 | lcd.printString("HIGH SCORE",0,3); |
el15ss | 5:1bf7c83f86cc | 382 | } |
el15ss | 2:98a41609c827 | 383 | |
el15ss | 5:1bf7c83f86cc | 384 | }*/ |
el15ss | 2:98a41609c827 | 385 | |
el15ss | 1:db9ff66f67c8 | 386 | |
el15ss | 4:2fdafb53eac2 | 387 | lcd.printString(" PRESS START ",0,5); |
el15ss | 0:12cfe63faa6a | 388 | |
el15ss | 0:12cfe63faa6a | 389 | lcd.refresh(); |
el15ss | 4:2fdafb53eac2 | 390 | //sd.unmount(); |
el15ss | 0:12cfe63faa6a | 391 | |
el15ss | 3:0c690f1c04d8 | 392 | //Takes the user back to the main for a new game |
el15ss | 5:1bf7c83f86cc | 393 | while ( pad.check_event(Gamepad::START_PRESSED) == false) |
el15ss | 5:1bf7c83f86cc | 394 | { |
el15ss | 0:12cfe63faa6a | 395 | pad.leds_on(); |
el15ss | 1:db9ff66f67c8 | 396 | //pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 397 | wait(0.1); |
el15ss | 0:12cfe63faa6a | 398 | pad.leds_off(); |
el15ss | 5:1bf7c83f86cc | 399 | // pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 400 | wait(0.1); |
el15ss | 5:1bf7c83f86cc | 401 | if( pad.check_event(Gamepad::START_PRESSED)) |
el15ss | 5:1bf7c83f86cc | 402 | { |
el15ss | 0:12cfe63faa6a | 403 | main(); |
el15ss | 0:12cfe63faa6a | 404 | wait(1); |
el15ss | 5:1bf7c83f86cc | 405 | } |
el15ss | 0:12cfe63faa6a | 406 | } |
el15ss | 0:12cfe63faa6a | 407 | |
el15ss | 0:12cfe63faa6a | 408 | } |
el15ss | 0:12cfe63faa6a | 409 | |
el15ss | 3:0c690f1c04d8 | 410 | |
el15ss | 3:0c690f1c04d8 | 411 | //Function to display the current High score fo the game and also reset it to 0 |
el15ss | 2:98a41609c827 | 412 | void displayHighScore() |
el15ss | 2:98a41609c827 | 413 | { |
el15ss | 2:98a41609c827 | 414 | sd.mount(); |
el15ss | 2:98a41609c827 | 415 | lcd.clear(); |
el15ss | 2:98a41609c827 | 416 | |
el15ss | 5:1bf7c83f86cc | 417 | //Open file |
el15ss | 5:1bf7c83f86cc | 418 | file = fopen("/sd/scoreFile.txt", "r"); |
el15ss | 5:1bf7c83f86cc | 419 | if(file ==NULL) |
el15ss | 3:0c690f1c04d8 | 420 | { |
el15ss | 3:0c690f1c04d8 | 421 | |
el15ss | 2:98a41609c827 | 422 | highScore = 0; |
el15ss | 2:98a41609c827 | 423 | |
el15ss | 2:98a41609c827 | 424 | } |
el15ss | 5:1bf7c83f86cc | 425 | else |
el15ss | 5:1bf7c83f86cc | 426 | { |
el15ss | 3:0c690f1c04d8 | 427 | //Read the high score from the file |
el15ss | 5:1bf7c83f86cc | 428 | fscanf(file,"%d", &highScore); |
el15ss | 5:1bf7c83f86cc | 429 | fclose(file); |
el15ss | 2:98a41609c827 | 430 | |
el15ss | 5:1bf7c83f86cc | 431 | } |
el15ss | 3:0c690f1c04d8 | 432 | //Convert highscore(int) to score(String) to print on the lcd |
el15ss | 2:98a41609c827 | 433 | sprintf (score, "High Score : %d",highScore); |
el15ss | 2:98a41609c827 | 434 | |
el15ss | 2:98a41609c827 | 435 | lcd.printString(score,0,2); |
el15ss | 2:98a41609c827 | 436 | lcd.printString(" START - reset ",0,4); |
el15ss | 2:98a41609c827 | 437 | lcd.printString(" BACK - menu ",0,5); |
el15ss | 5:1bf7c83f86cc | 438 | lcd.refresh(); |
el15ss | 5:1bf7c83f86cc | 439 | sd.unmount(); |
el15ss | 2:98a41609c827 | 440 | |
el15ss | 5:1bf7c83f86cc | 441 | while(1) |
el15ss | 5:1bf7c83f86cc | 442 | { |
el15ss | 3:0c690f1c04d8 | 443 | //To reset the highscore |
el15ss | 5:1bf7c83f86cc | 444 | if( pad.check_event(Gamepad::START_PRESSED)) |
el15ss | 5:1bf7c83f86cc | 445 | { |
el15ss | 5:1bf7c83f86cc | 446 | sd.mount(); |
el15ss | 5:1bf7c83f86cc | 447 | file = fopen("/sd/scoreFile.txt", "r"); |
el15ss | 5:1bf7c83f86cc | 448 | if(!file ==NULL) |
el15ss | 5:1bf7c83f86cc | 449 | { |
el15ss | 5:1bf7c83f86cc | 450 | //Delete the file if it is empty |
el15ss | 2:98a41609c827 | 451 | fclose(file); |
el15ss | 5:1bf7c83f86cc | 452 | remove("/sd/scoreFile.txt"); |
el15ss | 2:98a41609c827 | 453 | |
el15ss | 2:98a41609c827 | 454 | } |
el15ss | 5:1bf7c83f86cc | 455 | sd.unmount(); |
el15ss | 5:1bf7c83f86cc | 456 | displayHighScore(); |
el15ss | 2:98a41609c827 | 457 | |
el15ss | 2:98a41609c827 | 458 | } |
el15ss | 5:1bf7c83f86cc | 459 | |
el15ss | 3:0c690f1c04d8 | 460 | //Back to menu |
el15ss | 5:1bf7c83f86cc | 461 | if( pad.check_event(Gamepad::BACK_PRESSED)) |
el15ss | 5:1bf7c83f86cc | 462 | { |
el15ss | 5:1bf7c83f86cc | 463 | menu(); |
el15ss | 2:98a41609c827 | 464 | } |
el15ss | 2:98a41609c827 | 465 | |
el15ss | 2:98a41609c827 | 466 | sleep(); |
el15ss | 2:98a41609c827 | 467 | |
el15ss | 2:98a41609c827 | 468 | } |
el15ss | 2:98a41609c827 | 469 | |
el15ss | 2:98a41609c827 | 470 | |
el15ss | 2:98a41609c827 | 471 | } |
el15ss | 2:98a41609c827 | 472 | |
el15ss | 3:0c690f1c04d8 | 473 | |
el15ss | 0:12cfe63faa6a | 474 | |
el15ss | 3:0c690f1c04d8 | 475 | //Function to display the Instructions for the game |
el15ss | 0:12cfe63faa6a | 476 | void Instructions() |
el15ss | 0:12cfe63faa6a | 477 | { |
el15ss | 5:1bf7c83f86cc | 478 | bool i = true; |
el15ss | 5:1bf7c83f86cc | 479 | lcd.clear(); |
el15ss | 2:98a41609c827 | 480 | lcd.printString("INSTURCTIONS: ",0,0); |
el15ss | 0:12cfe63faa6a | 481 | lcd.printString("Collect the ",0,2); |
el15ss | 2:98a41609c827 | 482 | lcd.printString("gems and dodge ",0,3); |
el15ss | 2:98a41609c827 | 483 | lcd.printString("the obstacles ",0,4); |
el15ss | 2:98a41609c827 | 484 | lcd.printString("to get points ",0,5); |
el15ss | 0:12cfe63faa6a | 485 | lcd.refresh(); |
el15ss | 0:12cfe63faa6a | 486 | |
el15ss | 5:1bf7c83f86cc | 487 | while(i == true) |
el15ss | 5:1bf7c83f86cc | 488 | { |
el15ss | 0:12cfe63faa6a | 489 | |
el15ss | 5:1bf7c83f86cc | 490 | if( pad.check_event(Gamepad::BACK_PRESSED) ) |
el15ss | 5:1bf7c83f86cc | 491 | { |
el15ss | 1:db9ff66f67c8 | 492 | //pad.tone(1000.0,0.5); |
el15ss | 5:1bf7c83f86cc | 493 | i = false; |
el15ss | 5:1bf7c83f86cc | 494 | menu(); |
el15ss | 5:1bf7c83f86cc | 495 | } |
el15ss | 0:12cfe63faa6a | 496 | } |
el15ss | 0:12cfe63faa6a | 497 | |
el15ss | 5:1bf7c83f86cc | 498 | } |