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@28:e0161a52a8b9, 2019-05-08 (annotated)
- Committer:
- el17m2h
- Date:
- Wed May 08 18:02:11 2019 +0000
- Revision:
- 28:e0161a52a8b9
- Parent:
- 27:af14fcd5a520
- Child:
- 29:15e9640646b7
Added a get function to the engine in order to be able to call the final score on the main.cpp to display on the game over screen function.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17m2h | 24:67dc71a8f009 | 1 | /* |
el17m2h | 24:67dc71a8f009 | 2 | ELEC2645 Embedded Systems Project |
el17m2h | 24:67dc71a8f009 | 3 | School of Electronic & Electrical Engineering |
el17m2h | 24:67dc71a8f009 | 4 | University of Leeds |
el17m2h | 24:67dc71a8f009 | 5 | Name: Melissa Hartmann |
el17m2h | 24:67dc71a8f009 | 6 | Username: el17m2h |
el17m2h | 24:67dc71a8f009 | 7 | Student ID Number: 201176603 |
el17m2h | 24:67dc71a8f009 | 8 | Date: 09/05/2019 |
el17m2h | 24:67dc71a8f009 | 9 | */ |
el17m2h | 24:67dc71a8f009 | 10 | |
el17m2h | 1:0001cb3eb053 | 11 | #include "mbed.h" |
el17m2h | 1:0001cb3eb053 | 12 | #include "Gamepad.h" |
el17m2h | 1:0001cb3eb053 | 13 | #include "N5110.h" |
el17m2h | 2:360a6c301a4e | 14 | #include "Engine.h" |
el17m2h | 1:0001cb3eb053 | 15 | |
el17m2h | 5:8814d6de77d0 | 16 | //structs |
el17m2h | 5:8814d6de77d0 | 17 | struct UserInput { |
el17m2h | 5:8814d6de77d0 | 18 | Direction d; |
el17m2h | 5:8814d6de77d0 | 19 | float mag; |
el17m2h | 5:8814d6de77d0 | 20 | }; |
el17m2h | 5:8814d6de77d0 | 21 | |
el17m2h | 1:0001cb3eb053 | 22 | // objects |
el17m2h | 1:0001cb3eb053 | 23 | 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 | 24 | Gamepad pad; |
el17m2h | 2:360a6c301a4e | 25 | Engine eng; |
el17m2h | 19:5a7b0cdf013b | 26 | Doodler dood; |
el17m2h | 20:a359092079b0 | 27 | Enemy eny; |
el17m2h | 1:0001cb3eb053 | 28 | |
el17m2h | 1:0001cb3eb053 | 29 | // prototypes |
el17m2h | 1:0001cb3eb053 | 30 | void init(); |
el17m2h | 5:8814d6de77d0 | 31 | void update_game(UserInput input); |
el17m2h | 5:8814d6de77d0 | 32 | void render(); |
el17m2h | 1:0001cb3eb053 | 33 | void welcome(); |
el17m2h | 19:5a7b0cdf013b | 34 | void game_over(); |
el17m2h | 1:0001cb3eb053 | 35 | |
el17m2h | 1:0001cb3eb053 | 36 | // functions |
el17m2h | 1:0001cb3eb053 | 37 | int main(){ |
el17m2h | 15:4efa04a6a376 | 38 | while(1){ |
el17m2h | 21:6b16ca9834e6 | 39 | init(); |
el17m2h | 15:4efa04a6a376 | 40 | welcome(); |
el17m2h | 19:5a7b0cdf013b | 41 | int fps = 8; |
el17m2h | 19:5a7b0cdf013b | 42 | render(); // draws |
el17m2h | 19:5a7b0cdf013b | 43 | wait(1.0f/fps); |
el17m2h | 19:5a7b0cdf013b | 44 | while(1){ |
el17m2h | 19:5a7b0cdf013b | 45 | eng.read_input(pad); |
el17m2h | 19:5a7b0cdf013b | 46 | eng.update(pad); |
el17m2h | 19:5a7b0cdf013b | 47 | render(); |
el17m2h | 19:5a7b0cdf013b | 48 | wait(0.8f/fps); |
el17m2h | 27:af14fcd5a520 | 49 | bool end_game = eng.get_game_over(); |
el17m2h | 27:af14fcd5a520 | 50 | if (end_game == true){ |
el17m2h | 27:af14fcd5a520 | 51 | game_over(); |
el17m2h | 27:af14fcd5a520 | 52 | break; |
el17m2h | 27:af14fcd5a520 | 53 | } |
el17m2h | 19:5a7b0cdf013b | 54 | if (pad.check_event(Gamepad::BACK_PRESSED) == true){ |
el17m2h | 19:5a7b0cdf013b | 55 | break; |
el17m2h | 19:5a7b0cdf013b | 56 | } |
el17m2h | 19:5a7b0cdf013b | 57 | } |
el17m2h | 15:4efa04a6a376 | 58 | render(); |
el17m2h | 15:4efa04a6a376 | 59 | wait(0.1); |
el17m2h | 1:0001cb3eb053 | 60 | } |
el17m2h | 1:0001cb3eb053 | 61 | } |
el17m2h | 1:0001cb3eb053 | 62 | |
el17m2h | 1:0001cb3eb053 | 63 | // initialies all classes and libraries |
el17m2h | 1:0001cb3eb053 | 64 | void init(){ |
el17m2h | 1:0001cb3eb053 | 65 | // need to initialise LCD and Gamepad |
el17m2h | 1:0001cb3eb053 | 66 | lcd.init(); |
el17m2h | 23:9be87557b89a | 67 | lcd.setBrightness(1); |
el17m2h | 24:67dc71a8f009 | 68 | lcd.setContrast(0.5), |
el17m2h | 1:0001cb3eb053 | 69 | pad.init(); |
el17m2h | 26:d16a5b1e0ace | 70 | eng.init(); |
el17m2h | 1:0001cb3eb053 | 71 | } |
el17m2h | 1:0001cb3eb053 | 72 | |
el17m2h | 5:8814d6de77d0 | 73 | void render(){ |
el17m2h | 5:8814d6de77d0 | 74 | lcd.clear(); |
el17m2h | 5:8814d6de77d0 | 75 | eng.draw(lcd); |
el17m2h | 5:8814d6de77d0 | 76 | lcd.refresh(); |
el17m2h | 5:8814d6de77d0 | 77 | } |
el17m2h | 5:8814d6de77d0 | 78 | |
el17m2h | 1:0001cb3eb053 | 79 | // Starting menu screen display |
el17m2h | 1:0001cb3eb053 | 80 | void welcome() { |
el17m2h | 1:0001cb3eb053 | 81 | lcd.printString(" Doodle Jump! ",0,1); |
el17m2h | 1:0001cb3eb053 | 82 | lcd.printString(" Press Start ",0,4); |
el17m2h | 1:0001cb3eb053 | 83 | lcd.refresh(); |
el17m2h | 5:8814d6de77d0 | 84 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el17m2h | 5:8814d6de77d0 | 85 | pad.leds_on(); |
el17m2h | 5:8814d6de77d0 | 86 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 87 | pad.leds_off(); |
el17m2h | 5:8814d6de77d0 | 88 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 89 | } |
el17m2h | 19:5a7b0cdf013b | 90 | } |
el17m2h | 19:5a7b0cdf013b | 91 | |
el17m2h | 19:5a7b0cdf013b | 92 | void game_over() { |
el17m2h | 19:5a7b0cdf013b | 93 | lcd.clear(); |
el17m2h | 21:6b16ca9834e6 | 94 | lcd.printString("Game Over!",3,1); |
el17m2h | 21:6b16ca9834e6 | 95 | lcd.printString("Press back",3,3); |
el17m2h | 28:e0161a52a8b9 | 96 | int score = eng.get_score(); |
el17m2h | 28:e0161a52a8b9 | 97 | char buffer[14]; |
el17m2h | 28:e0161a52a8b9 | 98 | sprintf(buffer,"SCORE:%d", score); |
el17m2h | 28:e0161a52a8b9 | 99 | lcd.printString(buffer, 3, 5); |
el17m2h | 19:5a7b0cdf013b | 100 | lcd.refresh(); |
el17m2h | 21:6b16ca9834e6 | 101 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { |
el17m2h | 19:5a7b0cdf013b | 102 | pad.leds_on(); |
el17m2h | 19:5a7b0cdf013b | 103 | wait(0.1); |
el17m2h | 19:5a7b0cdf013b | 104 | pad.leds_off(); |
el17m2h | 19:5a7b0cdf013b | 105 | wait(0.1); |
el17m2h | 15:4efa04a6a376 | 106 | } |
el17m2h | 15:4efa04a6a376 | 107 | } |