ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Thu May 09 08:59:39 2019 +0000
Revision:
30:863565e9859f
Parent:
29:15e9640646b7
Child:
31:5c4acae51026
Changed the size of the enemy and updated the new values in the floors cpp. I also added the test code for the doodler's movement and called it on the main cpp.

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 //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 1:0001cb3eb053 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 29:15e9640646b7 33 // prototypes
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 19:5a7b0cdf013b 38 void game_over();
el17m2h 1:0001cb3eb053 39
el17m2h 1:0001cb3eb053 40 // functions
el17m2h 29:15e9640646b7 41 int main()
el17m2h 29:15e9640646b7 42 {
el17m2h 30:863565e9859f 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 30:863565e9859f 46 #endif
el17m2h 30:863565e9859f 47
el17m2h 29:15e9640646b7 48 while(1) {
el17m2h 21:6b16ca9834e6 49 init();
el17m2h 15:4efa04a6a376 50 welcome();
el17m2h 19:5a7b0cdf013b 51 int fps = 8;
el17m2h 19:5a7b0cdf013b 52 render(); // draws
el17m2h 19:5a7b0cdf013b 53 wait(1.0f/fps);
el17m2h 29:15e9640646b7 54 while(1) {
el17m2h 29:15e9640646b7 55 render();
el17m2h 19:5a7b0cdf013b 56 eng.read_input(pad);
el17m2h 29:15e9640646b7 57 bool score_added = eng.get_score_check();
el17m2h 19:5a7b0cdf013b 58 eng.update(pad);
el17m2h 29:15e9640646b7 59 bool updated_score_added = eng.get_score_check();
el17m2h 29:15e9640646b7 60 if ( (score_added == true) && (updated_score_added == true) ){
el17m2h 29:15e9640646b7 61 eng.subtract_score();
el17m2h 29:15e9640646b7 62 } // checking if the previous score has increased by one right after one update means that the a floor
el17m2h 29:15e9640646b7 63 // remains fixed at y = 10 (which adds a score). This will then prevent the score from increasing infinitely.
el17m2h 19:5a7b0cdf013b 64 wait(0.8f/fps);
el17m2h 27:af14fcd5a520 65 bool end_game = eng.get_game_over();
el17m2h 29:15e9640646b7 66 if (end_game == true) {
el17m2h 27:af14fcd5a520 67 game_over();
el17m2h 27:af14fcd5a520 68 break;
el17m2h 27:af14fcd5a520 69 }
el17m2h 29:15e9640646b7 70 if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
el17m2h 19:5a7b0cdf013b 71 break;
el17m2h 19:5a7b0cdf013b 72 }
el17m2h 19:5a7b0cdf013b 73 }
el17m2h 15:4efa04a6a376 74 render();
el17m2h 15:4efa04a6a376 75 wait(0.1);
el17m2h 1:0001cb3eb053 76 }
el17m2h 1:0001cb3eb053 77 }
el17m2h 1:0001cb3eb053 78
el17m2h 1:0001cb3eb053 79 // initialies all classes and libraries
el17m2h 29:15e9640646b7 80 void init()
el17m2h 29:15e9640646b7 81 {
el17m2h 29:15e9640646b7 82 // need to initialise LCD and Gamepad
el17m2h 1:0001cb3eb053 83 lcd.init();
el17m2h 23:9be87557b89a 84 lcd.setBrightness(1);
el17m2h 24:67dc71a8f009 85 lcd.setContrast(0.5),
el17m2h 29:15e9640646b7 86 pad.init();
el17m2h 26:d16a5b1e0ace 87 eng.init();
el17m2h 1:0001cb3eb053 88 }
el17m2h 1:0001cb3eb053 89
el17m2h 29:15e9640646b7 90 void render()
el17m2h 29:15e9640646b7 91 {
el17m2h 5:8814d6de77d0 92 lcd.clear();
el17m2h 5:8814d6de77d0 93 eng.draw(lcd);
el17m2h 5:8814d6de77d0 94 lcd.refresh();
el17m2h 5:8814d6de77d0 95 }
el17m2h 5:8814d6de77d0 96
el17m2h 1:0001cb3eb053 97 // Starting menu screen display
el17m2h 29:15e9640646b7 98 void welcome()
el17m2h 29:15e9640646b7 99 {
el17m2h 29:15e9640646b7 100 lcd.printString(" Doodle Jump! ",0,1);
el17m2h 1:0001cb3eb053 101 lcd.printString(" Press Start ",0,4);
el17m2h 1:0001cb3eb053 102 lcd.refresh();
el17m2h 5:8814d6de77d0 103 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
el17m2h 5:8814d6de77d0 104 pad.leds_on();
el17m2h 5:8814d6de77d0 105 wait(0.1);
el17m2h 5:8814d6de77d0 106 pad.leds_off();
el17m2h 5:8814d6de77d0 107 wait(0.1);
el17m2h 5:8814d6de77d0 108 }
el17m2h 19:5a7b0cdf013b 109 }
el17m2h 19:5a7b0cdf013b 110
el17m2h 29:15e9640646b7 111 void game_over()
el17m2h 29:15e9640646b7 112 {
el17m2h 19:5a7b0cdf013b 113 lcd.clear();
el17m2h 29:15e9640646b7 114 lcd.printString("Game Over!",3,1);
el17m2h 29:15e9640646b7 115 lcd.printString("Press back",3,3);
el17m2h 28:e0161a52a8b9 116 int score = eng.get_score();
el17m2h 28:e0161a52a8b9 117 char buffer[14];
el17m2h 28:e0161a52a8b9 118 sprintf(buffer,"SCORE:%d", score);
el17m2h 28:e0161a52a8b9 119 lcd.printString(buffer, 3, 5);
el17m2h 19:5a7b0cdf013b 120 lcd.refresh();
el17m2h 21:6b16ca9834e6 121 while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
el17m2h 29:15e9640646b7 122 pad.leds_on();
el17m2h 29:15e9640646b7 123 wait(0.1);
el17m2h 29:15e9640646b7 124 pad.leds_off();
el17m2h 29:15e9640646b7 125 wait(0.1);
el17m2h 15:4efa04a6a376 126 }
el17m2h 15:4efa04a6a376 127 }