ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Thu May 09 14:07:09 2019 +0000
Revision:
34:a9b14a4ccd46
Parent:
33:de130e274391
Child:
36:0c852c5ade4b
Finish adding comments to the engine file and started adding comments to the floors explaining each function.

Who changed what in which revision?

UserRevisionLine numberNew 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 31:5c4acae51026 44 #ifdef WITH_TESTING
el17m2h 30:863565e9859f 45 int number_of_failures = run_all_tests();
el17m2h 30:863565e9859f 46 if(number_of_failures > 0) return number_of_failures;
el17m2h 31:5c4acae51026 47 #endif
el17m2h 29:15e9640646b7 48 while(1) {
el17m2h 21:6b16ca9834e6 49 init();
el17m2h 15:4efa04a6a376 50 welcome();
el17m2h 19:5a7b0cdf013b 51 int fps = 8;
el17m2h 31:5c4acae51026 52 render(); // draws
el17m2h 29:15e9640646b7 53 while(1) {
el17m2h 29:15e9640646b7 54 render();
el17m2h 19:5a7b0cdf013b 55 eng.read_input(pad);
el17m2h 33:de130e274391 56 check_score();
el17m2h 19:5a7b0cdf013b 57 wait(0.8f/fps);
el17m2h 27:af14fcd5a520 58 bool end_game = eng.get_game_over();
el17m2h 29:15e9640646b7 59 if (end_game == true) {
el17m2h 27:af14fcd5a520 60 game_over();
el17m2h 27:af14fcd5a520 61 break;
el17m2h 27:af14fcd5a520 62 }
el17m2h 29:15e9640646b7 63 if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
el17m2h 19:5a7b0cdf013b 64 break;
el17m2h 19:5a7b0cdf013b 65 }
el17m2h 19:5a7b0cdf013b 66 }
el17m2h 1:0001cb3eb053 67 }
el17m2h 1:0001cb3eb053 68 }
el17m2h 1:0001cb3eb053 69
el17m2h 34:a9b14a4ccd46 70
el17m2h 34:a9b14a4ccd46 71 ////////////////////////// FUNCTIONS CODE /////////////////////////////////////
el17m2h 34:a9b14a4ccd46 72 void init() // initialies all classes and libraries, LCD and Gamepad
el17m2h 34:a9b14a4ccd46 73 {
el17m2h 1:0001cb3eb053 74 lcd.init();
el17m2h 23:9be87557b89a 75 lcd.setBrightness(1);
el17m2h 24:67dc71a8f009 76 lcd.setContrast(0.5),
el17m2h 34:a9b14a4ccd46 77 pad.init();
el17m2h 26:d16a5b1e0ace 78 eng.init();
el17m2h 1:0001cb3eb053 79 }
el17m2h 1:0001cb3eb053 80
el17m2h 34:a9b14a4ccd46 81 // prints the current engine screen
el17m2h 34:a9b14a4ccd46 82 void render()
el17m2h 29:15e9640646b7 83 {
el17m2h 34:a9b14a4ccd46 84 lcd.clear(); // clears the previous screen display
el17m2h 34:a9b14a4ccd46 85 eng.draw(lcd); // sets the current updated screen display
el17m2h 34:a9b14a4ccd46 86 lcd.refresh(); // prints the current screen diplay
el17m2h 5:8814d6de77d0 87 }
el17m2h 5:8814d6de77d0 88
el17m2h 1:0001cb3eb053 89 // Starting menu screen display
el17m2h 29:15e9640646b7 90 void welcome()
el17m2h 29:15e9640646b7 91 {
el17m2h 29:15e9640646b7 92 lcd.printString(" Doodle Jump! ",0,1);
el17m2h 1:0001cb3eb053 93 lcd.printString(" Press Start ",0,4);
el17m2h 34:a9b14a4ccd46 94 lcd.refresh();
el17m2h 34:a9b14a4ccd46 95 while ( pad.check_event(Gamepad::START_PRESSED) == false) { // infinite loop until START pressed
el17m2h 5:8814d6de77d0 96 pad.leds_on();
el17m2h 5:8814d6de77d0 97 wait(0.1);
el17m2h 5:8814d6de77d0 98 pad.leds_off();
el17m2h 5:8814d6de77d0 99 wait(0.1);
el17m2h 5:8814d6de77d0 100 }
el17m2h 19:5a7b0cdf013b 101 }
el17m2h 19:5a7b0cdf013b 102
el17m2h 34:a9b14a4ccd46 103 // Checks if the previous score has increased by one right after one to correct an infinite addition
el17m2h 33:de130e274391 104 void check_score()
el17m2h 33:de130e274391 105 {
el17m2h 33:de130e274391 106 bool score_added = eng.get_score_check();
el17m2h 33:de130e274391 107 eng.update(pad);
el17m2h 33:de130e274391 108 bool updated_score_added = eng.get_score_check();
el17m2h 34:a9b14a4ccd46 109 if (
el17m2h 34:a9b14a4ccd46 110 (score_added == true) && // conditions indicate the score has been added sequentially after two updates
el17m2h 34:a9b14a4ccd46 111 (updated_score_added == true) // this means the same floor has remained in the position that sets an addition, since it is
el17m2h 34:a9b14a4ccd46 112 ) // 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 113 {
el17m2h 34:a9b14a4ccd46 114 eng.subtract_score(); // subtracts the incorrect addition
el17m2h 34:a9b14a4ccd46 115 }
el17m2h 34:a9b14a4ccd46 116 }
el17m2h 33:de130e274391 117
el17m2h 34:a9b14a4ccd46 118 // Called on the main function to display the game over screen when the game has ended
el17m2h 29:15e9640646b7 119 void game_over()
el17m2h 29:15e9640646b7 120 {
el17m2h 19:5a7b0cdf013b 121 lcd.clear();
el17m2h 29:15e9640646b7 122 lcd.printString("Game Over!",3,1);
el17m2h 29:15e9640646b7 123 lcd.printString("Press back",3,3);
el17m2h 28:e0161a52a8b9 124 int score = eng.get_score();
el17m2h 34:a9b14a4ccd46 125 char text[14];
el17m2h 34:a9b14a4ccd46 126 sprintf(text,"SCORE:%d", score);
el17m2h 34:a9b14a4ccd46 127 lcd.printString(text, 3, 5);
el17m2h 19:5a7b0cdf013b 128 lcd.refresh();
el17m2h 21:6b16ca9834e6 129 while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
el17m2h 29:15e9640646b7 130 pad.leds_on();
el17m2h 29:15e9640646b7 131 wait(0.1);
el17m2h 29:15e9640646b7 132 pad.leds_off();
el17m2h 29:15e9640646b7 133 wait(0.1);
el17m2h 15:4efa04a6a376 134 }
el17m2h 15:4efa04a6a376 135 }