ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Wed May 08 21:11:35 2019 +0000
Revision:
29:15e9640646b7
Parent:
28:e0161a52a8b9
Child:
30:863565e9859f
Changed the enemy file so that it has functions to make it update its position or make it disappear from the screen. The floors h file includes the enemy, and depending on its position it updates it or not.

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