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