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@33:de130e274391, 2019-05-09 (annotated)
- Committer:
- el17m2h
- Date:
- Thu May 09 11:39:59 2019 +0000
- Revision:
- 33:de130e274391
- Parent:
- 31:5c4acae51026
- Child:
- 34:a9b14a4ccd46
Changed trivial functions to one-line structure.
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 | 29:15e9640646b7 | 11 | #include "mbed.h" |
el17m2h | 29:15e9640646b7 | 12 | #include "Gamepad.h" |
el17m2h | 29:15e9640646b7 | 13 | #include "N5110.h" |
el17m2h | 29:15e9640646b7 | 14 | #include "Engine.h" |
el17m2h | 1:0001cb3eb053 | 15 | |
el17m2h | 30:863565e9859f | 16 | |
el17m2h | 30:863565e9859f | 17 | #ifdef WITH_TESTING |
el17m2h | 30:863565e9859f | 18 | # include "tests.h" |
el17m2h | 30:863565e9859f | 19 | #endif |
el17m2h | 30:863565e9859f | 20 | |
el17m2h | 31:5c4acae51026 | 21 | // structs |
el17m2h | 5:8814d6de77d0 | 22 | struct UserInput { |
el17m2h | 5:8814d6de77d0 | 23 | Direction d; |
el17m2h | 5:8814d6de77d0 | 24 | float mag; |
el17m2h | 5:8814d6de77d0 | 25 | }; |
el17m2h | 5:8814d6de77d0 | 26 | |
el17m2h | 1:0001cb3eb053 | 27 | // objects |
el17m2h | 31:5c4acae51026 | 28 | 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 | 29 | Gamepad pad; |
el17m2h | 2:360a6c301a4e | 30 | Engine eng; |
el17m2h | 19:5a7b0cdf013b | 31 | Doodler dood; |
el17m2h | 1:0001cb3eb053 | 32 | |
el17m2h | 31:5c4acae51026 | 33 | // functions |
el17m2h | 1:0001cb3eb053 | 34 | void init(); |
el17m2h | 5:8814d6de77d0 | 35 | void update_game(UserInput input); |
el17m2h | 5:8814d6de77d0 | 36 | void render(); |
el17m2h | 1:0001cb3eb053 | 37 | void welcome(); |
el17m2h | 33:de130e274391 | 38 | void check_score(); |
el17m2h | 19:5a7b0cdf013b | 39 | void game_over(); |
el17m2h | 1:0001cb3eb053 | 40 | |
el17m2h | 29:15e9640646b7 | 41 | int main() |
el17m2h | 29:15e9640646b7 | 42 | { |
el17m2h | 31:5c4acae51026 | 43 | #ifdef WITH_TESTING |
el17m2h | 30:863565e9859f | 44 | int number_of_failures = run_all_tests(); |
el17m2h | 30:863565e9859f | 45 | if(number_of_failures > 0) return number_of_failures; |
el17m2h | 31:5c4acae51026 | 46 | #endif |
el17m2h | 29:15e9640646b7 | 47 | while(1) { |
el17m2h | 21:6b16ca9834e6 | 48 | init(); |
el17m2h | 15:4efa04a6a376 | 49 | welcome(); |
el17m2h | 19:5a7b0cdf013b | 50 | int fps = 8; |
el17m2h | 31:5c4acae51026 | 51 | render(); // draws |
el17m2h | 29:15e9640646b7 | 52 | while(1) { |
el17m2h | 29:15e9640646b7 | 53 | render(); |
el17m2h | 19:5a7b0cdf013b | 54 | eng.read_input(pad); |
el17m2h | 33:de130e274391 | 55 | check_score(); |
el17m2h | 19:5a7b0cdf013b | 56 | wait(0.8f/fps); |
el17m2h | 27:af14fcd5a520 | 57 | bool end_game = eng.get_game_over(); |
el17m2h | 29:15e9640646b7 | 58 | if (end_game == true) { |
el17m2h | 27:af14fcd5a520 | 59 | game_over(); |
el17m2h | 27:af14fcd5a520 | 60 | break; |
el17m2h | 27:af14fcd5a520 | 61 | } |
el17m2h | 29:15e9640646b7 | 62 | if (pad.check_event(Gamepad::BACK_PRESSED) == true) { |
el17m2h | 19:5a7b0cdf013b | 63 | break; |
el17m2h | 19:5a7b0cdf013b | 64 | } |
el17m2h | 19:5a7b0cdf013b | 65 | } |
el17m2h | 1:0001cb3eb053 | 66 | } |
el17m2h | 1:0001cb3eb053 | 67 | } |
el17m2h | 1:0001cb3eb053 | 68 | |
el17m2h | 1:0001cb3eb053 | 69 | // initialies all classes and libraries |
el17m2h | 29:15e9640646b7 | 70 | void init() |
el17m2h | 31:5c4acae51026 | 71 | { // need to initialise LCD and Gamepad |
el17m2h | 1:0001cb3eb053 | 72 | lcd.init(); |
el17m2h | 23:9be87557b89a | 73 | lcd.setBrightness(1); |
el17m2h | 24:67dc71a8f009 | 74 | lcd.setContrast(0.5), |
el17m2h | 29:15e9640646b7 | 75 | pad.init(); |
el17m2h | 26:d16a5b1e0ace | 76 | eng.init(); |
el17m2h | 1:0001cb3eb053 | 77 | } |
el17m2h | 1:0001cb3eb053 | 78 | |
el17m2h | 29:15e9640646b7 | 79 | void render() |
el17m2h | 29:15e9640646b7 | 80 | { |
el17m2h | 5:8814d6de77d0 | 81 | lcd.clear(); |
el17m2h | 5:8814d6de77d0 | 82 | eng.draw(lcd); |
el17m2h | 5:8814d6de77d0 | 83 | lcd.refresh(); |
el17m2h | 5:8814d6de77d0 | 84 | } |
el17m2h | 5:8814d6de77d0 | 85 | |
el17m2h | 1:0001cb3eb053 | 86 | // Starting menu screen display |
el17m2h | 29:15e9640646b7 | 87 | void welcome() |
el17m2h | 29:15e9640646b7 | 88 | { |
el17m2h | 29:15e9640646b7 | 89 | lcd.printString(" Doodle Jump! ",0,1); |
el17m2h | 1:0001cb3eb053 | 90 | lcd.printString(" Press Start ",0,4); |
el17m2h | 1:0001cb3eb053 | 91 | lcd.refresh(); |
el17m2h | 5:8814d6de77d0 | 92 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el17m2h | 5:8814d6de77d0 | 93 | pad.leds_on(); |
el17m2h | 5:8814d6de77d0 | 94 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 95 | pad.leds_off(); |
el17m2h | 5:8814d6de77d0 | 96 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 97 | } |
el17m2h | 19:5a7b0cdf013b | 98 | } |
el17m2h | 19:5a7b0cdf013b | 99 | |
el17m2h | 33:de130e274391 | 100 | void check_score() |
el17m2h | 33:de130e274391 | 101 | { |
el17m2h | 33:de130e274391 | 102 | bool score_added = eng.get_score_check(); |
el17m2h | 33:de130e274391 | 103 | eng.update(pad); |
el17m2h | 33:de130e274391 | 104 | bool updated_score_added = eng.get_score_check(); |
el17m2h | 33:de130e274391 | 105 | if ( (score_added == true) && (updated_score_added == true) ) { |
el17m2h | 33:de130e274391 | 106 | eng.subtract_score(); |
el17m2h | 33:de130e274391 | 107 | } // checking if the previous score has increased by one right after one update means that the a floor |
el17m2h | 33:de130e274391 | 108 | } // remains fixed at y = 10 (which adds a score). This will then prevent the score from increasing infinitely. |
el17m2h | 33:de130e274391 | 109 | |
el17m2h | 29:15e9640646b7 | 110 | void game_over() |
el17m2h | 29:15e9640646b7 | 111 | { |
el17m2h | 19:5a7b0cdf013b | 112 | lcd.clear(); |
el17m2h | 29:15e9640646b7 | 113 | lcd.printString("Game Over!",3,1); |
el17m2h | 29:15e9640646b7 | 114 | lcd.printString("Press back",3,3); |
el17m2h | 28:e0161a52a8b9 | 115 | int score = eng.get_score(); |
el17m2h | 28:e0161a52a8b9 | 116 | char buffer[14]; |
el17m2h | 28:e0161a52a8b9 | 117 | sprintf(buffer,"SCORE:%d", score); |
el17m2h | 28:e0161a52a8b9 | 118 | lcd.printString(buffer, 3, 5); |
el17m2h | 19:5a7b0cdf013b | 119 | lcd.refresh(); |
el17m2h | 21:6b16ca9834e6 | 120 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { |
el17m2h | 29:15e9640646b7 | 121 | pad.leds_on(); |
el17m2h | 29:15e9640646b7 | 122 | wait(0.1); |
el17m2h | 29:15e9640646b7 | 123 | pad.leds_off(); |
el17m2h | 29:15e9640646b7 | 124 | wait(0.1); |
el17m2h | 15:4efa04a6a376 | 125 | } |
el17m2h | 15:4efa04a6a376 | 126 | } |