![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
main.cpp@6:bf601a65cb27, 2017-05-04 (annotated)
- Committer:
- el15ss
- Date:
- Thu May 04 13:04:14 2017 +0000
- Revision:
- 6:bf601a65cb27
- Parent:
- 5:1bf7c83f86cc
- Child:
- 7:887651afda26
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 | 3:0c690f1c04d8 | 24 | //To helo 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 | 3:0c690f1c04d8 | 113 | // pad.tone(1500.0,0.5); |
el15ss | 3:0c690f1c04d8 | 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 | 5:1bf7c83f86cc | 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 | 5:1bf7c83f86cc | 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 | 5:1bf7c83f86cc | 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 | 5:1bf7c83f86cc | 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 | 6:bf601a65cb27 | 248 | //values = device.get_values(); |
el15ss | 6:bf601a65cb27 | 249 | // c.accMove(values); |
el15ss | 6:bf601a65cb27 | 250 | |
el15ss | 3:0c690f1c04d8 | 251 | //Condition to ckeck if the user wants to pause the game |
el15ss | 0:12cfe63faa6a | 252 | if(pad.check_event(Gamepad::BACK_PRESSED)) |
el15ss | 0:12cfe63faa6a | 253 | { |
el15ss | 0:12cfe63faa6a | 254 | lcd.clear(); |
el15ss | 6:bf601a65cb27 | 255 | lcd.refresh(); |
el15ss | 6:bf601a65cb27 | 256 | |
el15ss | 6:bf601a65cb27 | 257 | |
el15ss | 6:bf601a65cb27 | 258 | pad.led(2,1); |
el15ss | 6:bf601a65cb27 | 259 | pad.led(5,1); |
el15ss | 6:bf601a65cb27 | 260 | |
el15ss | 6:bf601a65cb27 | 261 | |
el15ss | 6:bf601a65cb27 | 262 | |
el15ss | 6:bf601a65cb27 | 263 | //fprintf("Game paused and sent to menu from the game when BACK is pressed"); |
el15ss | 1:db9ff66f67c8 | 264 | menu(); |
el15ss | 0:12cfe63faa6a | 265 | } |
el15ss | 0:12cfe63faa6a | 266 | |
el15ss | 6:bf601a65cb27 | 267 | //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 | 268 | for(i=0;i<No_OBS;i++) |
el15ss | 6:bf601a65cb27 | 269 | { //fprintf("in to loop to check when to initialise the obstacles and "); |
el15ss | 6:bf601a65cb27 | 270 | |
el15ss | 3:0c690f1c04d8 | 271 | //To retrieve the status of the obstacle on the screen |
el15ss | 6:bf601a65cb27 | 272 | //fprintf("Obstacle Status called from stickrunner()"); |
el15ss | 1:db9ff66f67c8 | 273 | obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); |
el15ss | 6:bf601a65cb27 | 274 | //fprintf("Value returned from obstacle status %d", obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); |
el15ss | 0:12cfe63faa6a | 275 | |
el15ss | 1:db9ff66f67c8 | 276 | if(obstacle[i].getObstacleStatus() == false) |
el15ss | 0:12cfe63faa6a | 277 | { |
el15ss | 6:bf601a65cb27 | 278 | //fprintf("Init clalled from stickrunner() in obstaccle"); |
el15ss | 0:12cfe63faa6a | 279 | obstacle[i].init(); |
el15ss | 0:12cfe63faa6a | 280 | } |
el15ss | 3:0c690f1c04d8 | 281 | |
el15ss | 3:0c690f1c04d8 | 282 | //To check whether the character has been hit by an obstacle by comparing the position of each obstacle |
el15ss | 3:0c690f1c04d8 | 283 | // relative to the character |
el15ss | 6:bf601a65cb27 | 284 | //fprintf("Character status called from stickrunner() in obstacle loop"); |
el15ss | 5:1bf7c83f86cc | 285 | c.characterStatus(obstacle[i].getObstaclePos()); |
el15ss | 6:bf601a65cb27 | 286 | //fprintf("the value returned by character status %d", c.characterStatus(obstacle[i].getObstaclePos())); |
el15ss | 3:0c690f1c04d8 | 287 | |
el15ss | 1:db9ff66f67c8 | 288 | |
el15ss | 0:12cfe63faa6a | 289 | } |
el15ss | 0:12cfe63faa6a | 290 | |
el15ss | 3:0c690f1c04d8 | 291 | //Loop to make the generation of gems a continious loop and also to check if the user has collected them |
el15ss | 1:db9ff66f67c8 | 292 | for(j=0;j<No_GEMS;j++) |
el15ss | 0:12cfe63faa6a | 293 | { |
el15ss | 6:bf601a65cb27 | 294 | //fprintf("in to loop to check when to initialise the gems and "); |
el15ss | 6:bf601a65cb27 | 295 | |
el15ss | 3:0c690f1c04d8 | 296 | //To check whether the character has collected a gem by comparing the position of each gem |
el15ss | 3:0c690f1c04d8 | 297 | // relative to the character |
el15ss | 6:bf601a65cb27 | 298 | //fprintf("Gems Status called from stickrunner()"); |
el15ss | 1:db9ff66f67c8 | 299 | gems[j].gemStatus(c.getCharacterPos()); |
el15ss | 6:bf601a65cb27 | 300 | //fprintf("Value returned from gem status %d", gem[i].gemStatus(obstacle[i].getGemPos()); |
el15ss | 0:12cfe63faa6a | 301 | |
el15ss | 1:db9ff66f67c8 | 302 | if(gems[j].getGemStatus() == false) |
el15ss | 0:12cfe63faa6a | 303 | { |
el15ss | 6:bf601a65cb27 | 304 | |
el15ss | 6:bf601a65cb27 | 305 | //fprintf("Init clalled from stickrunner() in gem"); |
el15ss | 0:12cfe63faa6a | 306 | gems[j].init(); |
el15ss | 2:98a41609c827 | 307 | |
el15ss | 6:bf601a65cb27 | 308 | |
el15ss | 0:12cfe63faa6a | 309 | } |
el15ss | 1:db9ff66f67c8 | 310 | |
el15ss | 1:db9ff66f67c8 | 311 | |
el15ss | 0:12cfe63faa6a | 312 | } |
el15ss | 2:98a41609c827 | 313 | |
el15ss | 2:98a41609c827 | 314 | //To make the obstacles and gems move along the screen |
el15ss | 3:0c690f1c04d8 | 315 | |
el15ss | 3:0c690f1c04d8 | 316 | |
el15ss | 0:12cfe63faa6a | 317 | i =0; |
el15ss | 0:12cfe63faa6a | 318 | |
el15ss | 1:db9ff66f67c8 | 319 | for(i=0;i<No_OBS;i++) |
el15ss | 1:db9ff66f67c8 | 320 | { |
el15ss | 6:bf601a65cb27 | 321 | |
el15ss | 6:bf601a65cb27 | 322 | //fprintf("update obstacle clalled from stickrunner()"); |
el15ss | 1:db9ff66f67c8 | 323 | obstacle[i].updateObstacle(); |
el15ss | 1:db9ff66f67c8 | 324 | } |
el15ss | 0:12cfe63faa6a | 325 | |
el15ss | 0:12cfe63faa6a | 326 | |
el15ss | 0:12cfe63faa6a | 327 | j =0; |
el15ss | 0:12cfe63faa6a | 328 | |
el15ss | 1:db9ff66f67c8 | 329 | for(j=0;j<No_GEMS;j++) |
el15ss | 1:db9ff66f67c8 | 330 | { |
el15ss | 6:bf601a65cb27 | 331 | //fprintf("update gem clalled from stickrunner()"); |
el15ss | 1:db9ff66f67c8 | 332 | gems[j].updateGems(); |
el15ss | 2:98a41609c827 | 333 | |
el15ss | 1:db9ff66f67c8 | 334 | } |
el15ss | 6:bf601a65cb27 | 335 | |
el15ss | 6:bf601a65cb27 | 336 | //fprintf("render called from stickrunner()"); |
el15ss | 0:12cfe63faa6a | 337 | render(); |
el15ss | 0:12cfe63faa6a | 338 | |
el15ss | 0:12cfe63faa6a | 339 | wait(1.0f/fps); |
el15ss | 0:12cfe63faa6a | 340 | } |
el15ss | 3:0c690f1c04d8 | 341 | |
el15ss | 0:12cfe63faa6a | 342 | } |
el15ss | 0:12cfe63faa6a | 343 | |
el15ss | 1:db9ff66f67c8 | 344 | |
el15ss | 3:0c690f1c04d8 | 345 | //Function to draw out the pixels on the screen |
el15ss | 0:12cfe63faa6a | 346 | void render() |
el15ss | 0:12cfe63faa6a | 347 | { |
el15ss | 6:bf601a65cb27 | 348 | //fprintf("In render"); |
el15ss | 0:12cfe63faa6a | 349 | lcd.clear(); |
el15ss | 1:db9ff66f67c8 | 350 | |
el15ss | 3:0c690f1c04d8 | 351 | |
el15ss | 3:0c690f1c04d8 | 352 | //Only draws the character as long as it survives |
el15ss | 1:db9ff66f67c8 | 353 | if(c.getCharacterStatus()) |
el15ss | 1:db9ff66f67c8 | 354 | { |
el15ss | 6:bf601a65cb27 | 355 | //fprintf("Character drawn"); |
el15ss | 1:db9ff66f67c8 | 356 | c.draw(lcd); |
el15ss | 1:db9ff66f67c8 | 357 | } |
el15ss | 2:98a41609c827 | 358 | |
el15ss | 5:1bf7c83f86cc | 359 | if(c.getCharacterStatus() == false) |
el15ss | 5:1bf7c83f86cc | 360 | { |
el15ss | 6:bf601a65cb27 | 361 | //fprintf("over called from render()"); |
el15ss | 5:1bf7c83f86cc | 362 | over(); |
el15ss | 5:1bf7c83f86cc | 363 | } |
el15ss | 2:98a41609c827 | 364 | |
el15ss | 2:98a41609c827 | 365 | |
el15ss | 3:0c690f1c04d8 | 366 | //Draws the obstacles if the status returned is true |
el15ss | 0:12cfe63faa6a | 367 | |
el15ss | 1:db9ff66f67c8 | 368 | for(i=0;i<No_OBS;i++) |
el15ss | 1:db9ff66f67c8 | 369 | { |
el15ss | 1:db9ff66f67c8 | 370 | if(obstacle[i].getObstacleStatus()) |
el15ss | 1:db9ff66f67c8 | 371 | { |
el15ss | 6:bf601a65cb27 | 372 | //fprintf("obstacle drawn"); |
el15ss | 1:db9ff66f67c8 | 373 | obstacle[i].draw(lcd); |
el15ss | 1:db9ff66f67c8 | 374 | } |
el15ss | 1:db9ff66f67c8 | 375 | } |
el15ss | 0:12cfe63faa6a | 376 | |
el15ss | 3:0c690f1c04d8 | 377 | //Draws the gems if the status returned is true |
el15ss | 5:1bf7c83f86cc | 378 | for(j=0;j<No_GEMS;j++) |
el15ss | 1:db9ff66f67c8 | 379 | { |
el15ss | 1:db9ff66f67c8 | 380 | if(gems[j].getGemStatus()) |
el15ss | 1:db9ff66f67c8 | 381 | { |
el15ss | 6:bf601a65cb27 | 382 | //fprintf("gem drawn"); |
el15ss | 1:db9ff66f67c8 | 383 | gems[j].draw(lcd); |
el15ss | 1:db9ff66f67c8 | 384 | |
el15ss | 1:db9ff66f67c8 | 385 | } |
el15ss | 1:db9ff66f67c8 | 386 | |
el15ss | 3:0c690f1c04d8 | 387 | |
el15ss | 1:db9ff66f67c8 | 388 | } |
el15ss | 2:98a41609c827 | 389 | |
el15ss | 1:db9ff66f67c8 | 390 | |
el15ss | 0:12cfe63faa6a | 391 | lcd.refresh(); |
el15ss | 1:db9ff66f67c8 | 392 | |
el15ss | 0:12cfe63faa6a | 393 | } |
el15ss | 0:12cfe63faa6a | 394 | |
el15ss | 1:db9ff66f67c8 | 395 | |
el15ss | 0:12cfe63faa6a | 396 | |
el15ss | 3:0c690f1c04d8 | 397 | //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 | 398 | void over() |
el15ss | 5:1bf7c83f86cc | 399 | { |
el15ss | 6:bf601a65cb27 | 400 | //fprintf("In over()"); |
el15ss | 6:bf601a65cb27 | 401 | |
el15ss | 6:bf601a65cb27 | 402 | |
el15ss | 6:bf601a65cb27 | 403 | |
el15ss | 3:0c690f1c04d8 | 404 | //pad.tone(1000.0,0.5); |
el15ss | 2:98a41609c827 | 405 | pad.init(); |
el15ss | 6:bf601a65cb27 | 406 | |
el15ss | 3:0c690f1c04d8 | 407 | |
el15ss | 6:bf601a65cb27 | 408 | |
el15ss | 3:0c690f1c04d8 | 409 | //Converting the counter into a string 'score' to display on the lcd |
el15ss | 2:98a41609c827 | 410 | sprintf (score, " Score : %d",counter); |
el15ss | 6:bf601a65cb27 | 411 | //fprintf("Counter converted to string %s",score); |
el15ss | 2:98a41609c827 | 412 | |
el15ss | 2:98a41609c827 | 413 | lcd.printString(score,0,2); |
el15ss | 2:98a41609c827 | 414 | lcd.printString("GAME OVER!! ",0,0); |
el15ss | 3:0c690f1c04d8 | 415 | |
el15ss | 6:bf601a65cb27 | 416 | |
el15ss | 4:2fdafb53eac2 | 417 | lcd.printString(" PRESS START ",0,5); |
el15ss | 0:12cfe63faa6a | 418 | |
el15ss | 0:12cfe63faa6a | 419 | lcd.refresh(); |
el15ss | 6:bf601a65cb27 | 420 | |
el15ss | 0:12cfe63faa6a | 421 | |
el15ss | 3:0c690f1c04d8 | 422 | //Takes the user back to the main for a new game |
el15ss | 5:1bf7c83f86cc | 423 | while ( pad.check_event(Gamepad::START_PRESSED) == false) |
el15ss | 5:1bf7c83f86cc | 424 | { |
el15ss | 0:12cfe63faa6a | 425 | pad.leds_on(); |
el15ss | 1:db9ff66f67c8 | 426 | //pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 427 | wait(0.1); |
el15ss | 0:12cfe63faa6a | 428 | pad.leds_off(); |
el15ss | 5:1bf7c83f86cc | 429 | // pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 430 | wait(0.1); |
el15ss | 5:1bf7c83f86cc | 431 | if( pad.check_event(Gamepad::START_PRESSED)) |
el15ss | 5:1bf7c83f86cc | 432 | { |
el15ss | 6:bf601a65cb27 | 433 | //fprintf("main called from over() to start a new game"); |
el15ss | 0:12cfe63faa6a | 434 | main(); |
el15ss | 0:12cfe63faa6a | 435 | wait(1); |
el15ss | 5:1bf7c83f86cc | 436 | } |
el15ss | 0:12cfe63faa6a | 437 | } |
el15ss | 0:12cfe63faa6a | 438 | |
el15ss | 0:12cfe63faa6a | 439 | } |
el15ss | 0:12cfe63faa6a | 440 | |
el15ss | 3:0c690f1c04d8 | 441 | |
el15ss | 3:0c690f1c04d8 | 442 | //Function to display the current High score fo the game and also reset it to 0 |
el15ss | 2:98a41609c827 | 443 | void displayHighScore() |
el15ss | 2:98a41609c827 | 444 | { |
el15ss | 6:bf601a65cb27 | 445 | //fprintf("In highscore()"); |
el15ss | 6:bf601a65cb27 | 446 | |
el15ss | 6:bf601a65cb27 | 447 | |
el15ss | 2:98a41609c827 | 448 | lcd.clear(); |
el15ss | 2:98a41609c827 | 449 | |
el15ss | 5:1bf7c83f86cc | 450 | //Open file |
el15ss | 3:0c690f1c04d8 | 451 | |
el15ss | 6:bf601a65cb27 | 452 | if(highScore <= counter) |
el15ss | 6:bf601a65cb27 | 453 | { |
el15ss | 6:bf601a65cb27 | 454 | highScore = counter; |
el15ss | 6:bf601a65cb27 | 455 | //fprintf("value of highscore from if %d", highScore); |
el15ss | 6:bf601a65cb27 | 456 | lcd.printString(" BACK - menu ",0,5); |
el15ss | 6:bf601a65cb27 | 457 | } |
el15ss | 2:98a41609c827 | 458 | |
el15ss | 6:bf601a65cb27 | 459 | else |
el15ss | 6:bf601a65cb27 | 460 | { |
el15ss | 6:bf601a65cb27 | 461 | highScore = highScore; |
el15ss | 6:bf601a65cb27 | 462 | |
el15ss | 6:bf601a65cb27 | 463 | } |
el15ss | 3:0c690f1c04d8 | 464 | //Convert highscore(int) to score(String) to print on the lcd |
el15ss | 6:bf601a65cb27 | 465 | sprintf (score, " %d",highScore); |
el15ss | 6:bf601a65cb27 | 466 | //fprintf("value of score when updated with highscore %s", score); |
el15ss | 6:bf601a65cb27 | 467 | lcd.printString("High Score :",0,2); |
el15ss | 6:bf601a65cb27 | 468 | lcd.printString(score,0,3); |
el15ss | 6:bf601a65cb27 | 469 | |
el15ss | 2:98a41609c827 | 470 | lcd.printString(" BACK - menu ",0,5); |
el15ss | 5:1bf7c83f86cc | 471 | lcd.refresh(); |
el15ss | 5:1bf7c83f86cc | 472 | sd.unmount(); |
el15ss | 2:98a41609c827 | 473 | |
el15ss | 5:1bf7c83f86cc | 474 | while(1) |
el15ss | 5:1bf7c83f86cc | 475 | { |
el15ss | 3:0c690f1c04d8 | 476 | //Back to menu |
el15ss | 5:1bf7c83f86cc | 477 | if( pad.check_event(Gamepad::BACK_PRESSED)) |
el15ss | 5:1bf7c83f86cc | 478 | { |
el15ss | 6:bf601a65cb27 | 479 | //fprintf("menu called from highscore when back is pressed"); |
el15ss | 5:1bf7c83f86cc | 480 | menu(); |
el15ss | 2:98a41609c827 | 481 | } |
el15ss | 2:98a41609c827 | 482 | |
el15ss | 2:98a41609c827 | 483 | sleep(); |
el15ss | 2:98a41609c827 | 484 | |
el15ss | 2:98a41609c827 | 485 | } |
el15ss | 2:98a41609c827 | 486 | |
el15ss | 2:98a41609c827 | 487 | |
el15ss | 2:98a41609c827 | 488 | } |
el15ss | 2:98a41609c827 | 489 | |
el15ss | 3:0c690f1c04d8 | 490 | |
el15ss | 0:12cfe63faa6a | 491 | |
el15ss | 3:0c690f1c04d8 | 492 | //Function to display the Instructions for the game |
el15ss | 0:12cfe63faa6a | 493 | void Instructions() |
el15ss | 0:12cfe63faa6a | 494 | { |
el15ss | 6:bf601a65cb27 | 495 | //fprintf("in instructions()"); |
el15ss | 5:1bf7c83f86cc | 496 | bool i = true; |
el15ss | 5:1bf7c83f86cc | 497 | lcd.clear(); |
el15ss | 2:98a41609c827 | 498 | lcd.printString("INSTURCTIONS: ",0,0); |
el15ss | 0:12cfe63faa6a | 499 | lcd.printString("Collect the ",0,2); |
el15ss | 2:98a41609c827 | 500 | lcd.printString("gems and dodge ",0,3); |
el15ss | 2:98a41609c827 | 501 | lcd.printString("the obstacles ",0,4); |
el15ss | 2:98a41609c827 | 502 | lcd.printString("to get points ",0,5); |
el15ss | 0:12cfe63faa6a | 503 | lcd.refresh(); |
el15ss | 0:12cfe63faa6a | 504 | |
el15ss | 5:1bf7c83f86cc | 505 | while(i == true) |
el15ss | 5:1bf7c83f86cc | 506 | { |
el15ss | 0:12cfe63faa6a | 507 | |
el15ss | 5:1bf7c83f86cc | 508 | if( pad.check_event(Gamepad::BACK_PRESSED) ) |
el15ss | 5:1bf7c83f86cc | 509 | { |
el15ss | 1:db9ff66f67c8 | 510 | //pad.tone(1000.0,0.5); |
el15ss | 5:1bf7c83f86cc | 511 | i = false; |
el15ss | 6:bf601a65cb27 | 512 | |
el15ss | 6:bf601a65cb27 | 513 | //fprintf("menu called from instructions when back pressed"); |
el15ss | 5:1bf7c83f86cc | 514 | menu(); |
el15ss | 5:1bf7c83f86cc | 515 | } |
el15ss | 0:12cfe63faa6a | 516 | } |
el15ss | 0:12cfe63faa6a | 517 | |
el15ss | 5:1bf7c83f86cc | 518 | } |