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@36:0c852c5ade4b, 2019-05-09 (annotated)
- Committer:
- el17m2h
- Date:
- Thu May 09 14:22:29 2019 +0000
- Revision:
- 36:0c852c5ade4b
- Parent:
- 34:a9b14a4ccd46
Updated the comments on the documentation for the engine, floors and doodler.
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 | 5:8814d6de77d0 | 21 | struct UserInput { |
el17m2h | 5:8814d6de77d0 | 22 | Direction d; |
el17m2h | 5:8814d6de77d0 | 23 | float mag; |
el17m2h | 5:8814d6de77d0 | 24 | }; |
el17m2h | 5:8814d6de77d0 | 25 | |
el17m2h | 34:a9b14a4ccd46 | 26 | //////////////////////// OBJECTS /////////////////////////////////// |
el17m2h | 31:5c4acae51026 | 27 | 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 | 28 | Gamepad pad; |
el17m2h | 2:360a6c301a4e | 29 | Engine eng; |
el17m2h | 19:5a7b0cdf013b | 30 | Doodler dood; |
el17m2h | 1:0001cb3eb053 | 31 | |
el17m2h | 34:a9b14a4ccd46 | 32 | //////////////////////// FUNCTIONS DECLARED ////////////////////////////// |
el17m2h | 1:0001cb3eb053 | 33 | void init(); |
el17m2h | 5:8814d6de77d0 | 34 | void update_game(UserInput input); |
el17m2h | 5:8814d6de77d0 | 35 | void render(); |
el17m2h | 1:0001cb3eb053 | 36 | void welcome(); |
el17m2h | 33:de130e274391 | 37 | void check_score(); |
el17m2h | 19:5a7b0cdf013b | 38 | void game_over(); |
el17m2h | 1:0001cb3eb053 | 39 | |
el17m2h | 34:a9b14a4ccd46 | 40 | |
el17m2h | 34:a9b14a4ccd46 | 41 | ////////////////////////// MAIN CODE ///////////////////////////////////// |
el17m2h | 29:15e9640646b7 | 42 | int main() |
el17m2h | 29:15e9640646b7 | 43 | { |
el17m2h | 29:15e9640646b7 | 44 | while(1) { |
el17m2h | 21:6b16ca9834e6 | 45 | init(); |
el17m2h | 15:4efa04a6a376 | 46 | welcome(); |
el17m2h | 19:5a7b0cdf013b | 47 | int fps = 8; |
el17m2h | 31:5c4acae51026 | 48 | render(); // draws |
el17m2h | 29:15e9640646b7 | 49 | while(1) { |
el17m2h | 29:15e9640646b7 | 50 | render(); |
el17m2h | 19:5a7b0cdf013b | 51 | eng.read_input(pad); |
el17m2h | 33:de130e274391 | 52 | check_score(); |
el17m2h | 19:5a7b0cdf013b | 53 | wait(0.8f/fps); |
el17m2h | 27:af14fcd5a520 | 54 | bool end_game = eng.get_game_over(); |
el17m2h | 29:15e9640646b7 | 55 | if (end_game == true) { |
el17m2h | 27:af14fcd5a520 | 56 | game_over(); |
el17m2h | 27:af14fcd5a520 | 57 | break; |
el17m2h | 27:af14fcd5a520 | 58 | } |
el17m2h | 29:15e9640646b7 | 59 | if (pad.check_event(Gamepad::BACK_PRESSED) == true) { |
el17m2h | 19:5a7b0cdf013b | 60 | break; |
el17m2h | 19:5a7b0cdf013b | 61 | } |
el17m2h | 19:5a7b0cdf013b | 62 | } |
el17m2h | 1:0001cb3eb053 | 63 | } |
el17m2h | 1:0001cb3eb053 | 64 | } |
el17m2h | 1:0001cb3eb053 | 65 | |
el17m2h | 34:a9b14a4ccd46 | 66 | |
el17m2h | 34:a9b14a4ccd46 | 67 | ////////////////////////// FUNCTIONS CODE ///////////////////////////////////// |
el17m2h | 34:a9b14a4ccd46 | 68 | void init() // initialies all classes and libraries, LCD and Gamepad |
el17m2h | 34:a9b14a4ccd46 | 69 | { |
el17m2h | 1:0001cb3eb053 | 70 | lcd.init(); |
el17m2h | 23:9be87557b89a | 71 | lcd.setBrightness(1); |
el17m2h | 24:67dc71a8f009 | 72 | lcd.setContrast(0.5), |
el17m2h | 34:a9b14a4ccd46 | 73 | pad.init(); |
el17m2h | 26:d16a5b1e0ace | 74 | eng.init(); |
el17m2h | 1:0001cb3eb053 | 75 | } |
el17m2h | 1:0001cb3eb053 | 76 | |
el17m2h | 34:a9b14a4ccd46 | 77 | // prints the current engine screen |
el17m2h | 34:a9b14a4ccd46 | 78 | void render() |
el17m2h | 29:15e9640646b7 | 79 | { |
el17m2h | 34:a9b14a4ccd46 | 80 | lcd.clear(); // clears the previous screen display |
el17m2h | 34:a9b14a4ccd46 | 81 | eng.draw(lcd); // sets the current updated screen display |
el17m2h | 34:a9b14a4ccd46 | 82 | lcd.refresh(); // prints the current screen diplay |
el17m2h | 5:8814d6de77d0 | 83 | } |
el17m2h | 5:8814d6de77d0 | 84 | |
el17m2h | 1:0001cb3eb053 | 85 | // Starting menu screen display |
el17m2h | 29:15e9640646b7 | 86 | void welcome() |
el17m2h | 29:15e9640646b7 | 87 | { |
el17m2h | 29:15e9640646b7 | 88 | lcd.printString(" Doodle Jump! ",0,1); |
el17m2h | 1:0001cb3eb053 | 89 | lcd.printString(" Press Start ",0,4); |
el17m2h | 34:a9b14a4ccd46 | 90 | lcd.refresh(); |
el17m2h | 34:a9b14a4ccd46 | 91 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { // infinite loop until START pressed |
el17m2h | 5:8814d6de77d0 | 92 | pad.leds_on(); |
el17m2h | 5:8814d6de77d0 | 93 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 94 | pad.leds_off(); |
el17m2h | 5:8814d6de77d0 | 95 | wait(0.1); |
el17m2h | 5:8814d6de77d0 | 96 | } |
el17m2h | 19:5a7b0cdf013b | 97 | } |
el17m2h | 19:5a7b0cdf013b | 98 | |
el17m2h | 34:a9b14a4ccd46 | 99 | // Checks if the previous score has increased by one right after one to correct an infinite addition |
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 | 34:a9b14a4ccd46 | 105 | if ( |
el17m2h | 34:a9b14a4ccd46 | 106 | (score_added == true) && // conditions indicate the score has been added sequentially after two updates |
el17m2h | 34:a9b14a4ccd46 | 107 | (updated_score_added == true) // this means the same floor has remained in the position that sets an addition, since it is |
el17m2h | 34:a9b14a4ccd46 | 108 | ) // an impossibility for it to be another floor (floors have specific y-coordinate separation so there is never one next to another) |
el17m2h | 34:a9b14a4ccd46 | 109 | { |
el17m2h | 34:a9b14a4ccd46 | 110 | eng.subtract_score(); // subtracts the incorrect addition |
el17m2h | 34:a9b14a4ccd46 | 111 | } |
el17m2h | 34:a9b14a4ccd46 | 112 | } |
el17m2h | 33:de130e274391 | 113 | |
el17m2h | 34:a9b14a4ccd46 | 114 | // Called on the main function to display the game over screen when the game has ended |
el17m2h | 29:15e9640646b7 | 115 | void game_over() |
el17m2h | 29:15e9640646b7 | 116 | { |
el17m2h | 19:5a7b0cdf013b | 117 | lcd.clear(); |
el17m2h | 29:15e9640646b7 | 118 | lcd.printString("Game Over!",3,1); |
el17m2h | 29:15e9640646b7 | 119 | lcd.printString("Press back",3,3); |
el17m2h | 28:e0161a52a8b9 | 120 | int score = eng.get_score(); |
el17m2h | 34:a9b14a4ccd46 | 121 | char text[14]; |
el17m2h | 34:a9b14a4ccd46 | 122 | sprintf(text,"SCORE:%d", score); |
el17m2h | 34:a9b14a4ccd46 | 123 | lcd.printString(text, 3, 5); |
el17m2h | 19:5a7b0cdf013b | 124 | lcd.refresh(); |
el17m2h | 21:6b16ca9834e6 | 125 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { |
el17m2h | 29:15e9640646b7 | 126 | pad.leds_on(); |
el17m2h | 29:15e9640646b7 | 127 | wait(0.1); |
el17m2h | 29:15e9640646b7 | 128 | pad.leds_off(); |
el17m2h | 29:15e9640646b7 | 129 | wait(0.1); |
el17m2h | 15:4efa04a6a376 | 130 | } |
el17m2h | 15:4efa04a6a376 | 131 | } |