Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@23:9be87557b89a, 2019-05-08 (annotated)
- Committer:
- el17m2h
- Date:
- Wed May 08 08:46:11 2019 +0000
- Revision:
- 23:9be87557b89a
- Parent:
- 22:0d2ac98a8b48
- Child:
- 24:67dc71a8f009
Added a sprite for the doodler and the enemy.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17m2h | 1:0001cb3eb053 | 1 | #include "mbed.h" |
el17m2h | 1:0001cb3eb053 | 2 | #include "Gamepad.h" |
el17m2h | 1:0001cb3eb053 | 3 | #include "N5110.h" |
el17m2h | 2:360a6c301a4e | 4 | #include "Engine.h" |
el17m2h | 1:0001cb3eb053 | 5 | |
el17m2h | 23:9be87557b89a | 6 | #define FLOORS_WIDTH 14 |
el17m2h | 19:5a7b0cdf013b | 7 | #define FLOORS_HEIGHT 1 |
el17m2h | 5:8814d6de77d0 | 8 | |
el17m2h | 5:8814d6de77d0 | 9 | //structs |
el17m2h | 5:8814d6de77d0 | 10 | struct UserInput { |
el17m2h | 5:8814d6de77d0 | 11 | Direction d; |
el17m2h | 5:8814d6de77d0 | 12 | float mag; |
el17m2h | 5:8814d6de77d0 | 13 | }; |
el17m2h | 5:8814d6de77d0 | 14 | |
el17m2h | 1:0001cb3eb053 | 15 | |
el17m2h | 1:0001cb3eb053 | 16 | // objects |
el17m2h | 1:0001cb3eb053 | 17 | N5110 lcd(PTC5,PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // START, LCD SCE, LCD RST, LCD DC, LCD MOSI, LCD CLK, LCD Backlight |
el17m2h | 1:0001cb3eb053 | 18 | Gamepad pad; |
el17m2h | 2:360a6c301a4e | 19 | Engine eng; |
el17m2h | 19:5a7b0cdf013b | 20 | Doodler dood; |
el17m2h | 20:a359092079b0 | 21 | Enemy eny; |
el17m2h | 1:0001cb3eb053 | 22 | |
el17m2h | 1:0001cb3eb053 | 23 | // prototypes |
el17m2h | 1:0001cb3eb053 | 24 | void init(); |
el17m2h | 5:8814d6de77d0 | 25 | void update_game(UserInput input); |
el17m2h | 5:8814d6de77d0 | 26 | void render(); |
el17m2h | 1:0001cb3eb053 | 27 | void welcome(); |
el17m2h | 19:5a7b0cdf013b | 28 | void game_over(); |
el17m2h | 1:0001cb3eb053 | 29 | |
el17m2h | 1:0001cb3eb053 | 30 | // functions |
el17m2h | 1:0001cb3eb053 | 31 | int main(){ |
el17m2h | 15:4efa04a6a376 | 32 | while(1){ |
el17m2h | 21:6b16ca9834e6 | 33 | init(); |
el17m2h | 15:4efa04a6a376 | 34 | welcome(); |
el17m2h | 19:5a7b0cdf013b | 35 | int fps = 8; |
el17m2h | 19:5a7b0cdf013b | 36 | render(); // draws |
el17m2h | 19:5a7b0cdf013b | 37 | wait(1.0f/fps); |
el17m2h | 19:5a7b0cdf013b | 38 | while(1){ |
el17m2h | 19:5a7b0cdf013b | 39 | eng.read_input(pad); |
el17m2h | 19:5a7b0cdf013b | 40 | eng.update(pad); |
el17m2h | 19:5a7b0cdf013b | 41 | render(); |
el17m2h | 19:5a7b0cdf013b | 42 | wait(0.8f/fps); |
el17m2h | 19:5a7b0cdf013b | 43 | if (pad.check_event(Gamepad::BACK_PRESSED) == true){ |
el17m2h | 19:5a7b0cdf013b | 44 | break; |
el17m2h | 19:5a7b0cdf013b | 45 | } |
el17m2h | 19:5a7b0cdf013b | 46 | } |
el17m2h | 15:4efa04a6a376 | 47 | render(); |
el17m2h | 15:4efa04a6a376 | 48 | wait(0.1); |
el17m2h | 1:0001cb3eb053 | 49 | } |
el17m2h | 1:0001cb3eb053 | 50 | } |
el17m2h | 1:0001cb3eb053 | 51 | |
el17m2h | 19:5a7b0cdf013b | 52 | |
el17m2h | 1:0001cb3eb053 | 53 | // initialies all classes and libraries |
el17m2h | 1:0001cb3eb053 | 54 | void init(){ |
el17m2h | 1:0001cb3eb053 | 55 | // need to initialise LCD and Gamepad |
el17m2h | 1:0001cb3eb053 | 56 | lcd.init(); |
el17m2h | 23:9be87557b89a | 57 | lcd.setBrightness(1); |
el17m2h | 1:0001cb3eb053 | 58 | pad.init(); |
el17m2h | 23:9be87557b89a | 59 | eng.init(FLOORS_WIDTH, FLOORS_HEIGHT); |
el17m2h | 1:0001cb3eb053 | 60 | } |
el17m2h | 1:0001cb3eb053 | 61 | |
el17m2h | 5:8814d6de77d0 | 62 | void render(){ |
el17m2h | 5:8814d6de77d0 | 63 | lcd.clear(); |
el17m2h | 5:8814d6de77d0 | 64 | eng.draw(lcd); |
el17m2h | 5:8814d6de77d0 | 65 | lcd.refresh(); |
el17m2h | 5:8814d6de77d0 | 66 | } |
el17m2h | 5:8814d6de77d0 | 67 | |
el17m2h | 1:0001cb3eb053 | 68 | // Starting menu screen display |
el17m2h | 1:0001cb3eb053 | 69 | void welcome() { |
el17m2h | 1:0001cb3eb053 | 70 | lcd.printString(" Doodle Jump! ",0,1); |
el17m2h | 1:0001cb3eb053 | 71 | lcd.printString(" Press Start ",0,4); |
el17m2h | 1:0001cb3eb053 | 72 | lcd.refresh(); |
el17m2h | 5:8814d6de77d0 | 73 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el17m2h | 5:8814d6de77d0 | 74 | pad.leds_on(); |
el17m2h | 5:8814d6de77d0 | 75 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 76 | pad.leds_off(); |
el17m2h | 5:8814d6de77d0 | 77 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 78 | } |
el17m2h | 19:5a7b0cdf013b | 79 | } |
el17m2h | 19:5a7b0cdf013b | 80 | |
el17m2h | 19:5a7b0cdf013b | 81 | void game_over() { |
el17m2h | 19:5a7b0cdf013b | 82 | lcd.clear(); |
el17m2h | 21:6b16ca9834e6 | 83 | lcd.printString("Game Over!",3,1); |
el17m2h | 21:6b16ca9834e6 | 84 | lcd.printString("Press back",3,3); |
el17m2h | 21:6b16ca9834e6 | 85 | lcd.printString("SCORE = ",3,5); |
el17m2h | 19:5a7b0cdf013b | 86 | lcd.refresh(); |
el17m2h | 21:6b16ca9834e6 | 87 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { |
el17m2h | 19:5a7b0cdf013b | 88 | pad.leds_on(); |
el17m2h | 19:5a7b0cdf013b | 89 | wait(0.1); |
el17m2h | 19:5a7b0cdf013b | 90 | pad.leds_off(); |
el17m2h | 19:5a7b0cdf013b | 91 | wait(0.1); |
el17m2h | 15:4efa04a6a376 | 92 | } |
el17m2h | 15:4efa04a6a376 | 93 | } |