ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Mon May 06 11:21:39 2019 +0000
Revision:
22:0d2ac98a8b48
Parent:
21:6b16ca9834e6
Child:
23:9be87557b89a
Re-organized the functions for the engine and the doodler so that the velocity and position of the doodler are changed as separate functions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17m2h 1:0001cb3eb053 1 #include "mbed.h"
el17m2h 1:0001cb3eb053 2 #include "Gamepad.h"
el17m2h 1:0001cb3eb053 3 #include "N5110.h"
el17m2h 2:360a6c301a4e 4 #include "Engine.h"
el17m2h 1:0001cb3eb053 5
el17m2h 19:5a7b0cdf013b 6 #define FLOORS_WIDTH 12
el17m2h 19:5a7b0cdf013b 7 #define FLOORS_HEIGHT 1
el17m2h 5:8814d6de77d0 8 #define DOODLER_RADIUS 2
el17m2h 5:8814d6de77d0 9
el17m2h 5:8814d6de77d0 10 //structs
el17m2h 5:8814d6de77d0 11 struct UserInput {
el17m2h 5:8814d6de77d0 12 Direction d;
el17m2h 5:8814d6de77d0 13 float mag;
el17m2h 5:8814d6de77d0 14 };
el17m2h 5:8814d6de77d0 15
el17m2h 1:0001cb3eb053 16
el17m2h 1:0001cb3eb053 17 // objects
el17m2h 1:0001cb3eb053 18 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 19 Gamepad pad;
el17m2h 2:360a6c301a4e 20 Engine eng;
el17m2h 19:5a7b0cdf013b 21 Doodler dood;
el17m2h 20:a359092079b0 22 Enemy eny;
el17m2h 1:0001cb3eb053 23
el17m2h 1:0001cb3eb053 24 // prototypes
el17m2h 1:0001cb3eb053 25 void init();
el17m2h 5:8814d6de77d0 26 void update_game(UserInput input);
el17m2h 5:8814d6de77d0 27 void render();
el17m2h 1:0001cb3eb053 28 void welcome();
el17m2h 19:5a7b0cdf013b 29 void game_over();
el17m2h 1:0001cb3eb053 30
el17m2h 1:0001cb3eb053 31 // functions
el17m2h 1:0001cb3eb053 32 int main(){
el17m2h 15:4efa04a6a376 33 while(1){
el17m2h 21:6b16ca9834e6 34 init();
el17m2h 15:4efa04a6a376 35 welcome();
el17m2h 19:5a7b0cdf013b 36 int fps = 8;
el17m2h 19:5a7b0cdf013b 37 render(); // draws
el17m2h 19:5a7b0cdf013b 38 wait(1.0f/fps);
el17m2h 19:5a7b0cdf013b 39 while(1){
el17m2h 19:5a7b0cdf013b 40 eng.read_input(pad);
el17m2h 19:5a7b0cdf013b 41 eng.update(pad);
el17m2h 19:5a7b0cdf013b 42 render();
el17m2h 19:5a7b0cdf013b 43 wait(0.8f/fps);
el17m2h 19:5a7b0cdf013b 44 if (pad.check_event(Gamepad::BACK_PRESSED) == true){
el17m2h 19:5a7b0cdf013b 45 break;
el17m2h 19:5a7b0cdf013b 46 }
el17m2h 19:5a7b0cdf013b 47 }
el17m2h 15:4efa04a6a376 48 render();
el17m2h 15:4efa04a6a376 49 wait(0.1);
el17m2h 1:0001cb3eb053 50 }
el17m2h 1:0001cb3eb053 51 }
el17m2h 1:0001cb3eb053 52
el17m2h 19:5a7b0cdf013b 53
el17m2h 1:0001cb3eb053 54 // initialies all classes and libraries
el17m2h 1:0001cb3eb053 55 void init(){
el17m2h 1:0001cb3eb053 56 // need to initialise LCD and Gamepad
el17m2h 1:0001cb3eb053 57 lcd.init();
el17m2h 1:0001cb3eb053 58 pad.init();
el17m2h 4:8ec314f806ae 59 eng.init(FLOORS_WIDTH, FLOORS_HEIGHT, DOODLER_RADIUS);
el17m2h 1:0001cb3eb053 60 }
el17m2h 1:0001cb3eb053 61
el17m2h 5:8814d6de77d0 62 void render(){
el17m2h 5:8814d6de77d0 63 lcd.clear();
el17m2h 5:8814d6de77d0 64 eng.draw(lcd);
el17m2h 5:8814d6de77d0 65 lcd.refresh();
el17m2h 5:8814d6de77d0 66 }
el17m2h 5:8814d6de77d0 67
el17m2h 1:0001cb3eb053 68 // Starting menu screen display
el17m2h 1:0001cb3eb053 69 void welcome() {
el17m2h 1:0001cb3eb053 70 lcd.printString(" Doodle Jump! ",0,1);
el17m2h 1:0001cb3eb053 71 lcd.printString(" Press Start ",0,4);
el17m2h 1:0001cb3eb053 72 lcd.refresh();
el17m2h 5:8814d6de77d0 73 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
el17m2h 5:8814d6de77d0 74 pad.leds_on();
el17m2h 5:8814d6de77d0 75 wait(0.1);
el17m2h 5:8814d6de77d0 76 pad.leds_off();
el17m2h 5:8814d6de77d0 77 wait(0.1);
el17m2h 5:8814d6de77d0 78 }
el17m2h 19:5a7b0cdf013b 79 }
el17m2h 19:5a7b0cdf013b 80
el17m2h 19:5a7b0cdf013b 81 void game_over() {
el17m2h 19:5a7b0cdf013b 82 lcd.clear();
el17m2h 21:6b16ca9834e6 83 lcd.printString("Game Over!",3,1);
el17m2h 21:6b16ca9834e6 84 lcd.printString("Press back",3,3);
el17m2h 21:6b16ca9834e6 85 lcd.printString("SCORE = ",3,5);
el17m2h 19:5a7b0cdf013b 86 lcd.refresh();
el17m2h 21:6b16ca9834e6 87 while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
el17m2h 19:5a7b0cdf013b 88 pad.leds_on();
el17m2h 19:5a7b0cdf013b 89 wait(0.1);
el17m2h 19:5a7b0cdf013b 90 pad.leds_off();
el17m2h 19:5a7b0cdf013b 91 wait(0.1);
el17m2h 15:4efa04a6a376 92 }
el17m2h 15:4efa04a6a376 93 }