![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
main.cpp@1:db9ff66f67c8, 2017-04-28 (annotated)
- Committer:
- el15ss
- Date:
- Fri Apr 28 16:30:47 2017 +0000
- Revision:
- 1:db9ff66f67c8
- Parent:
- 0:12cfe63faa6a
- Child:
- 2:98a41609c827
Need to add accelerometer functonality
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 | 0:12cfe63faa6a | 10 | #include <iostream> |
el15ss | 0:12cfe63faa6a | 11 | |
el15ss | 1:db9ff66f67c8 | 12 | #define No_OBS 8 |
el15ss | 1:db9ff66f67c8 | 13 | #define No_GEMS 4 |
el15ss | 0:12cfe63faa6a | 14 | |
el15ss | 1:db9ff66f67c8 | 15 | int i,j,counter; |
el15ss | 0:12cfe63faa6a | 16 | |
el15ss | 1:db9ff66f67c8 | 17 | //Structs |
el15ss | 0:12cfe63faa6a | 18 | struct UserInput { |
el15ss | 0:12cfe63faa6a | 19 | Direction d; |
el15ss | 0:12cfe63faa6a | 20 | float mag; |
el15ss | 0:12cfe63faa6a | 21 | }; |
el15ss | 1:db9ff66f67c8 | 22 | /* Class Objects */ |
el15ss | 0:12cfe63faa6a | 23 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el15ss | 0:12cfe63faa6a | 24 | Gamepad pad; |
el15ss | 0:12cfe63faa6a | 25 | Character c; |
el15ss | 1:db9ff66f67c8 | 26 | Obstacles obstacle[No_OBS]; |
el15ss | 1:db9ff66f67c8 | 27 | Gems gems[No_GEMS],g; |
el15ss | 0:12cfe63faa6a | 28 | |
el15ss | 0:12cfe63faa6a | 29 | |
el15ss | 1:db9ff66f67c8 | 30 | /* Function Prototypes */ |
el15ss | 0:12cfe63faa6a | 31 | void init(); |
el15ss | 0:12cfe63faa6a | 32 | void update_game(UserInput input); |
el15ss | 0:12cfe63faa6a | 33 | void render(); |
el15ss | 0:12cfe63faa6a | 34 | void welcome(); |
el15ss | 0:12cfe63faa6a | 35 | void menu(); |
el15ss | 0:12cfe63faa6a | 36 | void over(); |
el15ss | 0:12cfe63faa6a | 37 | void Instructions(); |
el15ss | 1:db9ff66f67c8 | 38 | |
el15ss | 1:db9ff66f67c8 | 39 | |
el15ss | 1:db9ff66f67c8 | 40 | /* Functions */ |
el15ss | 1:db9ff66f67c8 | 41 | |
el15ss | 0:12cfe63faa6a | 42 | int main() |
el15ss | 0:12cfe63faa6a | 43 | { |
el15ss | 1:db9ff66f67c8 | 44 | int fps = 8; |
el15ss | 0:12cfe63faa6a | 45 | |
el15ss | 1:db9ff66f67c8 | 46 | /* Intialization */ |
el15ss | 0:12cfe63faa6a | 47 | init(); |
el15ss | 1:db9ff66f67c8 | 48 | |
el15ss | 1:db9ff66f67c8 | 49 | /* Drawing the intial frame */ |
el15ss | 0:12cfe63faa6a | 50 | welcome(); |
el15ss | 0:12cfe63faa6a | 51 | wait(1.0f/fps); |
el15ss | 0:12cfe63faa6a | 52 | |
el15ss | 0:12cfe63faa6a | 53 | |
el15ss | 1:db9ff66f67c8 | 54 | |
el15ss | 1:db9ff66f67c8 | 55 | render(); |
el15ss | 0:12cfe63faa6a | 56 | wait(1.0f/fps); |
el15ss | 0:12cfe63faa6a | 57 | |
el15ss | 1:db9ff66f67c8 | 58 | /* Main game loop to read input, render the display and update the game state */ |
el15ss | 0:12cfe63faa6a | 59 | // game loop - read input, update the game state and render the display |
el15ss | 0:12cfe63faa6a | 60 | while (1) { |
el15ss | 0:12cfe63faa6a | 61 | |
el15ss | 0:12cfe63faa6a | 62 | |
el15ss | 0:12cfe63faa6a | 63 | |
el15ss | 1:db9ff66f67c8 | 64 | c.updateCharacter(pad.get_direction(),pad.get_mag()); |
el15ss | 0:12cfe63faa6a | 65 | if(pad.check_event(Gamepad::BACK_PRESSED)) |
el15ss | 0:12cfe63faa6a | 66 | { |
el15ss | 0:12cfe63faa6a | 67 | lcd.clear(); |
el15ss | 0:12cfe63faa6a | 68 | lcd.refresh(); |
el15ss | 1:db9ff66f67c8 | 69 | menu(); |
el15ss | 0:12cfe63faa6a | 70 | } |
el15ss | 0:12cfe63faa6a | 71 | |
el15ss | 1:db9ff66f67c8 | 72 | for(i=0;i<No_OBS;i++) |
el15ss | 0:12cfe63faa6a | 73 | { |
el15ss | 1:db9ff66f67c8 | 74 | obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); |
el15ss | 0:12cfe63faa6a | 75 | |
el15ss | 1:db9ff66f67c8 | 76 | if(obstacle[i].getObstacleStatus() == false) |
el15ss | 0:12cfe63faa6a | 77 | { |
el15ss | 0:12cfe63faa6a | 78 | obstacle[i].init(); |
el15ss | 0:12cfe63faa6a | 79 | } |
el15ss | 1:db9ff66f67c8 | 80 | c.characterStatus(obstacle[i].getObstaclePos()); |
el15ss | 1:db9ff66f67c8 | 81 | obstacle[i].obstacleStatus(c.getCharacterPos()); |
el15ss | 1:db9ff66f67c8 | 82 | |
el15ss | 0:12cfe63faa6a | 83 | } |
el15ss | 0:12cfe63faa6a | 84 | |
el15ss | 0:12cfe63faa6a | 85 | |
el15ss | 1:db9ff66f67c8 | 86 | for(j=0;j<No_GEMS;j++) |
el15ss | 0:12cfe63faa6a | 87 | { |
el15ss | 1:db9ff66f67c8 | 88 | gems[j].gemStatus(c.getCharacterPos()); |
el15ss | 0:12cfe63faa6a | 89 | |
el15ss | 1:db9ff66f67c8 | 90 | if(gems[j].getGemStatus() == false) |
el15ss | 0:12cfe63faa6a | 91 | { |
el15ss | 0:12cfe63faa6a | 92 | gems[j].init(); |
el15ss | 0:12cfe63faa6a | 93 | } |
el15ss | 1:db9ff66f67c8 | 94 | |
el15ss | 1:db9ff66f67c8 | 95 | |
el15ss | 0:12cfe63faa6a | 96 | } |
el15ss | 0:12cfe63faa6a | 97 | i =0; |
el15ss | 0:12cfe63faa6a | 98 | |
el15ss | 1:db9ff66f67c8 | 99 | for(i=0;i<No_OBS;i++) |
el15ss | 1:db9ff66f67c8 | 100 | { |
el15ss | 1:db9ff66f67c8 | 101 | obstacle[i].updateObstacle(); |
el15ss | 1:db9ff66f67c8 | 102 | } |
el15ss | 0:12cfe63faa6a | 103 | |
el15ss | 0:12cfe63faa6a | 104 | |
el15ss | 0:12cfe63faa6a | 105 | j =0; |
el15ss | 0:12cfe63faa6a | 106 | |
el15ss | 1:db9ff66f67c8 | 107 | for(j=0;j<No_GEMS;j++) |
el15ss | 1:db9ff66f67c8 | 108 | { |
el15ss | 1:db9ff66f67c8 | 109 | gems[j].updateGems(); |
el15ss | 1:db9ff66f67c8 | 110 | } |
el15ss | 0:12cfe63faa6a | 111 | |
el15ss | 0:12cfe63faa6a | 112 | render(); |
el15ss | 0:12cfe63faa6a | 113 | |
el15ss | 0:12cfe63faa6a | 114 | wait(1.0f/fps); |
el15ss | 0:12cfe63faa6a | 115 | } |
el15ss | 0:12cfe63faa6a | 116 | } |
el15ss | 0:12cfe63faa6a | 117 | |
el15ss | 0:12cfe63faa6a | 118 | void init() |
el15ss | 0:12cfe63faa6a | 119 | { |
el15ss | 0:12cfe63faa6a | 120 | // need to initialise LCD and Gamepad |
el15ss | 0:12cfe63faa6a | 121 | lcd.init(); |
el15ss | 0:12cfe63faa6a | 122 | pad.init(); |
el15ss | 0:12cfe63faa6a | 123 | c.init(); |
el15ss | 0:12cfe63faa6a | 124 | |
el15ss | 1:db9ff66f67c8 | 125 | for(i=0;i<No_OBS;i++) |
el15ss | 1:db9ff66f67c8 | 126 | { |
el15ss | 1:db9ff66f67c8 | 127 | obstacle[i].init(); |
el15ss | 1:db9ff66f67c8 | 128 | } |
el15ss | 1:db9ff66f67c8 | 129 | |
el15ss | 1:db9ff66f67c8 | 130 | for(j=0;j<No_GEMS;j++) |
el15ss | 1:db9ff66f67c8 | 131 | { |
el15ss | 1:db9ff66f67c8 | 132 | gems[j].init(); |
el15ss | 1:db9ff66f67c8 | 133 | } |
el15ss | 0:12cfe63faa6a | 134 | |
el15ss | 0:12cfe63faa6a | 135 | |
el15ss | 0:12cfe63faa6a | 136 | |
el15ss | 0:12cfe63faa6a | 137 | } |
el15ss | 0:12cfe63faa6a | 138 | |
el15ss | 0:12cfe63faa6a | 139 | void render() |
el15ss | 0:12cfe63faa6a | 140 | { |
el15ss | 0:12cfe63faa6a | 141 | // clear screen, re-draw and refresh |
el15ss | 0:12cfe63faa6a | 142 | lcd.clear(); |
el15ss | 1:db9ff66f67c8 | 143 | |
el15ss | 1:db9ff66f67c8 | 144 | if(c.getCharacterStatus()) |
el15ss | 1:db9ff66f67c8 | 145 | { |
el15ss | 1:db9ff66f67c8 | 146 | c.draw(lcd); |
el15ss | 1:db9ff66f67c8 | 147 | } |
el15ss | 0:12cfe63faa6a | 148 | //obstacle[i].draw(lcd); |
el15ss | 0:12cfe63faa6a | 149 | |
el15ss | 1:db9ff66f67c8 | 150 | for(i=0;i<No_OBS;i++) |
el15ss | 1:db9ff66f67c8 | 151 | { |
el15ss | 1:db9ff66f67c8 | 152 | if(obstacle[i].getObstacleStatus()) |
el15ss | 1:db9ff66f67c8 | 153 | { |
el15ss | 1:db9ff66f67c8 | 154 | obstacle[i].draw(lcd); |
el15ss | 1:db9ff66f67c8 | 155 | } |
el15ss | 1:db9ff66f67c8 | 156 | } |
el15ss | 0:12cfe63faa6a | 157 | |
el15ss | 1:db9ff66f67c8 | 158 | |
el15ss | 1:db9ff66f67c8 | 159 | for(j=0;j<No_GEMS;j++) |
el15ss | 1:db9ff66f67c8 | 160 | { |
el15ss | 1:db9ff66f67c8 | 161 | if(gems[j].getGemStatus()) |
el15ss | 1:db9ff66f67c8 | 162 | { |
el15ss | 1:db9ff66f67c8 | 163 | gems[j].draw(lcd); |
el15ss | 1:db9ff66f67c8 | 164 | |
el15ss | 1:db9ff66f67c8 | 165 | } |
el15ss | 1:db9ff66f67c8 | 166 | |
el15ss | 1:db9ff66f67c8 | 167 | else{ |
el15ss | 1:db9ff66f67c8 | 168 | counter++; |
el15ss | 1:db9ff66f67c8 | 169 | } |
el15ss | 0:12cfe63faa6a | 170 | |
el15ss | 1:db9ff66f67c8 | 171 | } |
el15ss | 1:db9ff66f67c8 | 172 | |
el15ss | 1:db9ff66f67c8 | 173 | if(c.getCharacterStatus() == false) |
el15ss | 1:db9ff66f67c8 | 174 | { |
el15ss | 1:db9ff66f67c8 | 175 | over(); |
el15ss | 1:db9ff66f67c8 | 176 | } |
el15ss | 1:db9ff66f67c8 | 177 | |
el15ss | 1:db9ff66f67c8 | 178 | |
el15ss | 0:12cfe63faa6a | 179 | lcd.refresh(); |
el15ss | 1:db9ff66f67c8 | 180 | |
el15ss | 0:12cfe63faa6a | 181 | } |
el15ss | 0:12cfe63faa6a | 182 | |
el15ss | 1:db9ff66f67c8 | 183 | |
el15ss | 0:12cfe63faa6a | 184 | void welcome() { |
el15ss | 0:12cfe63faa6a | 185 | |
el15ss | 1:db9ff66f67c8 | 186 | |
el15ss | 1:db9ff66f67c8 | 187 | |
el15ss | 0:12cfe63faa6a | 188 | lcd.printString("Stick Runner! ",0,1); |
el15ss | 0:12cfe63faa6a | 189 | lcd.printString(" Press Start ",0,4); |
el15ss | 0:12cfe63faa6a | 190 | lcd.refresh(); |
el15ss | 1:db9ff66f67c8 | 191 | // pad.tone(1500.0,0.5); |
el15ss | 1:db9ff66f67c8 | 192 | //pad.tone(1500.0,0.5); |
el15ss | 0:12cfe63faa6a | 193 | |
el15ss | 0:12cfe63faa6a | 194 | |
el15ss | 0:12cfe63faa6a | 195 | // wait flashing LEDs until start button is pressed |
el15ss | 1:db9ff66f67c8 | 196 | while ( pad.check_event(Gamepad::START_PRESSED) == false) |
el15ss | 1:db9ff66f67c8 | 197 | { |
el15ss | 0:12cfe63faa6a | 198 | pad.leds_on(); |
el15ss | 0:12cfe63faa6a | 199 | wait(0.1); |
el15ss | 0:12cfe63faa6a | 200 | pad.leds_off(); |
el15ss | 0:12cfe63faa6a | 201 | wait(0.1); |
el15ss | 0:12cfe63faa6a | 202 | |
el15ss | 0:12cfe63faa6a | 203 | } |
el15ss | 0:12cfe63faa6a | 204 | menu(); |
el15ss | 0:12cfe63faa6a | 205 | } |
el15ss | 0:12cfe63faa6a | 206 | |
el15ss | 0:12cfe63faa6a | 207 | void over() { |
el15ss | 0:12cfe63faa6a | 208 | lcd.clear(); |
el15ss | 1:db9ff66f67c8 | 209 | |
el15ss | 1:db9ff66f67c8 | 210 | char buffer[50]; |
el15ss | 1:db9ff66f67c8 | 211 | |
el15ss | 1:db9ff66f67c8 | 212 | sprintf (buffer, "%d",counter); |
el15ss | 1:db9ff66f67c8 | 213 | |
el15ss | 1:db9ff66f67c8 | 214 | lcd.printString(buffer,0,3); |
el15ss | 0:12cfe63faa6a | 215 | lcd.printString(" GAME ",0,1); |
el15ss | 0:12cfe63faa6a | 216 | lcd.printString(" OVER!!!!! ",0,2); |
el15ss | 1:db9ff66f67c8 | 217 | |
el15ss | 0:12cfe63faa6a | 218 | lcd.printString(" PRESS START ",0,4); |
el15ss | 0:12cfe63faa6a | 219 | |
el15ss | 0:12cfe63faa6a | 220 | lcd.refresh(); |
el15ss | 0:12cfe63faa6a | 221 | |
el15ss | 0:12cfe63faa6a | 222 | // wait flashing LEDs until start button is pressed |
el15ss | 0:12cfe63faa6a | 223 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el15ss | 0:12cfe63faa6a | 224 | pad.leds_on(); |
el15ss | 1:db9ff66f67c8 | 225 | //pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 226 | wait(0.1); |
el15ss | 0:12cfe63faa6a | 227 | pad.leds_off(); |
el15ss | 1:db9ff66f67c8 | 228 | // pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 229 | wait(0.1); |
el15ss | 0:12cfe63faa6a | 230 | if( pad.check_event(Gamepad::START_PRESSED)) { |
el15ss | 0:12cfe63faa6a | 231 | main(); |
el15ss | 0:12cfe63faa6a | 232 | wait(1); |
el15ss | 0:12cfe63faa6a | 233 | } |
el15ss | 0:12cfe63faa6a | 234 | } |
el15ss | 0:12cfe63faa6a | 235 | |
el15ss | 0:12cfe63faa6a | 236 | } |
el15ss | 0:12cfe63faa6a | 237 | |
el15ss | 0:12cfe63faa6a | 238 | void menu() { |
el15ss | 0:12cfe63faa6a | 239 | int fps =8; |
el15ss | 0:12cfe63faa6a | 240 | bool i = true; |
el15ss | 0:12cfe63faa6a | 241 | while(i == true) |
el15ss | 0:12cfe63faa6a | 242 | { |
el15ss | 0:12cfe63faa6a | 243 | lcd.printString(" Menu ",0,1); |
el15ss | 0:12cfe63faa6a | 244 | lcd.printString("A)New Game ",0,2); |
el15ss | 0:12cfe63faa6a | 245 | lcd.printString("B)Continue ",0,3); |
el15ss | 0:12cfe63faa6a | 246 | lcd.printString("X)Instructions ",0,4); |
el15ss | 0:12cfe63faa6a | 247 | |
el15ss | 0:12cfe63faa6a | 248 | lcd.refresh(); |
el15ss | 0:12cfe63faa6a | 249 | |
el15ss | 0:12cfe63faa6a | 250 | // wait flashing LEDs until start button is pressed |
el15ss | 0:12cfe63faa6a | 251 | if( pad.check_event(Gamepad::A_PRESSED) ) { |
el15ss | 1:db9ff66f67c8 | 252 | //pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 253 | i = false; |
el15ss | 0:12cfe63faa6a | 254 | lcd.clear(); |
el15ss | 0:12cfe63faa6a | 255 | lcd.refresh(); |
el15ss | 1:db9ff66f67c8 | 256 | init(); |
el15ss | 0:12cfe63faa6a | 257 | |
el15ss | 0:12cfe63faa6a | 258 | //break; |
el15ss | 0:12cfe63faa6a | 259 | } |
el15ss | 0:12cfe63faa6a | 260 | |
el15ss | 0:12cfe63faa6a | 261 | else if( pad.check_event(Gamepad::B_PRESSED) ) { |
el15ss | 0:12cfe63faa6a | 262 | //lcd.clear(); |
el15ss | 1:db9ff66f67c8 | 263 | // pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 264 | i = false; |
el15ss | 0:12cfe63faa6a | 265 | lcd.refresh(); |
el15ss | 0:12cfe63faa6a | 266 | |
el15ss | 0:12cfe63faa6a | 267 | |
el15ss | 0:12cfe63faa6a | 268 | } |
el15ss | 0:12cfe63faa6a | 269 | |
el15ss | 0:12cfe63faa6a | 270 | else if( pad.check_event(Gamepad::X_PRESSED) ) { |
el15ss | 1:db9ff66f67c8 | 271 | // pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 272 | Instructions(); |
el15ss | 0:12cfe63faa6a | 273 | wait(100.0f/fps); |
el15ss | 0:12cfe63faa6a | 274 | i = false; |
el15ss | 0:12cfe63faa6a | 275 | } |
el15ss | 0:12cfe63faa6a | 276 | |
el15ss | 0:12cfe63faa6a | 277 | |
el15ss | 0:12cfe63faa6a | 278 | } |
el15ss | 0:12cfe63faa6a | 279 | } |
el15ss | 0:12cfe63faa6a | 280 | |
el15ss | 0:12cfe63faa6a | 281 | void Instructions() |
el15ss | 0:12cfe63faa6a | 282 | { |
el15ss | 0:12cfe63faa6a | 283 | bool i = true; |
el15ss | 0:12cfe63faa6a | 284 | lcd.clear(); |
el15ss | 0:12cfe63faa6a | 285 | lcd.printString("INSTURCTIONS: ",0,1); |
el15ss | 0:12cfe63faa6a | 286 | lcd.printString("Collect the ",0,2); |
el15ss | 0:12cfe63faa6a | 287 | lcd.printString("gems to get ",0,3); |
el15ss | 0:12cfe63faa6a | 288 | lcd.printString("points ",0,4); |
el15ss | 0:12cfe63faa6a | 289 | lcd.refresh(); |
el15ss | 0:12cfe63faa6a | 290 | |
el15ss | 0:12cfe63faa6a | 291 | while(i == true){ |
el15ss | 0:12cfe63faa6a | 292 | |
el15ss | 0:12cfe63faa6a | 293 | if( pad.check_event(Gamepad::BACK_PRESSED) ) { |
el15ss | 1:db9ff66f67c8 | 294 | //pad.tone(1000.0,0.5); |
el15ss | 0:12cfe63faa6a | 295 | i = false; |
el15ss | 0:12cfe63faa6a | 296 | menu(); |
el15ss | 0:12cfe63faa6a | 297 | } |
el15ss | 0:12cfe63faa6a | 298 | } |
el15ss | 0:12cfe63faa6a | 299 | |
el15ss | 0:12cfe63faa6a | 300 | } |