ELEC2645 (2018/19) / Mbed 2 deprecated ml16c5l

Dependencies:   mbed

Committer:
ml16c5l
Date:
Tue Apr 09 10:17:24 2019 +0000
Revision:
3:5d860d0d589e
Parent:
1:63db7ec60700
Child:
4:2848b366ee3d
change game to copter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ml16c5l 1:63db7ec60700 1 /*
ml16c5l 1:63db7ec60700 2 ELEC2645 Embedded Systems Project
ml16c5l 1:63db7ec60700 3 School of Electronic & Electrical Engineering
ml16c5l 1:63db7ec60700 4 University of Leeds
ml16c5l 3:5d860d0d589e 5 Name: Caiwenjing Liu
ml16c5l 3:5d860d0d589e 6 Username:ml16c5l
ml16c5l 3:5d860d0d589e 7 Student ID Number: 201165261
ml16c5l 3:5d860d0d589e 8 Date: 09/04/2019
ml16c5l 1:63db7ec60700 9 */
ml16c5l 1:63db7ec60700 10
ml16c5l 3:5d860d0d589e 11 ///////// pre-processor directives ////////
ml16c5l 1:63db7ec60700 12 #include "mbed.h"
ml16c5l 1:63db7ec60700 13 #include "Gamepad.h"
ml16c5l 1:63db7ec60700 14 #include "N5110.h"
ml16c5l 3:5d860d0d589e 15 //#include "CopterEngine.h"
ml16c5l 3:5d860d0d589e 16
ml16c5l 3:5d860d0d589e 17 #ifdef WITH_TESTING
ml16c5l 3:5d860d0d589e 18 # include "tests.h"
ml16c5l 3:5d860d0d589e 19 #endif
ml16c5l 3:5d860d0d589e 20
ml16c5l 1:63db7ec60700 21
ml16c5l 1:63db7ec60700 22
ml16c5l 1:63db7ec60700 23 /////////////// structs /////////////////
ml16c5l 1:63db7ec60700 24 struct UserInput {
ml16c5l 1:63db7ec60700 25 Direction d;
ml16c5l 1:63db7ec60700 26 float mag;
ml16c5l 1:63db7ec60700 27 };
ml16c5l 1:63db7ec60700 28 /////////////// objects ///////////////
ml16c5l 1:63db7ec60700 29 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
ml16c5l 3:5d860d0d589e 30 Gamepad pad;
ml16c5l 3:5d860d0d589e 31 //CopterEngine copter;
ml16c5l 3:5d860d0d589e 32
ml16c5l 3:5d860d0d589e 33 ///////////// prototypes ///////////////
ml16c5l 3:5d860d0d589e 34 void init();
ml16c5l 3:5d860d0d589e 35 void update_game(UserInput input);
ml16c5l 3:5d860d0d589e 36 void render();
ml16c5l 3:5d860d0d589e 37 void welcome();
ml16c5l 3:5d860d0d589e 38
ml16c5l 3:5d860d0d589e 39 ///////////// functions ////////////////
ml16c5l 3:5d860d0d589e 40 int main()
ml16c5l 3:5d860d0d589e 41 {
ml16c5l 3:5d860d0d589e 42
ml16c5l 3:5d860d0d589e 43
ml16c5l 3:5d860d0d589e 44 int fps = 8; // frames per second
ml16c5l 3:5d860d0d589e 45
ml16c5l 3:5d860d0d589e 46 init(); // initialise and then display welcome screen...
ml16c5l 3:5d860d0d589e 47 welcome(); // waiting for the user to start
ml16c5l 3:5d860d0d589e 48
ml16c5l 3:5d860d0d589e 49 render(); // first draw the initial frame
ml16c5l 3:5d860d0d589e 50 wait(1.0f/fps); // and wait for one frame period
ml16c5l 3:5d860d0d589e 51
ml16c5l 3:5d860d0d589e 52
ml16c5l 3:5d860d0d589e 53 // game loop - read input, update the game state and render the display
ml16c5l 3:5d860d0d589e 54 while (1) {
ml16c5l 3:5d860d0d589e 55 copter.read_input(pad);
ml16c5l 3:5d860d0d589e 56 copter.update(pad);
ml16c5l 3:5d860d0d589e 57 render();
ml16c5l 3:5d860d0d589e 58 wait(1.0f/fps);
ml16c5l 3:5d860d0d589e 59 }
ml16c5l 3:5d860d0d589e 60 }
ml16c5l 3:5d860d0d589e 61
ml16c5l 3:5d860d0d589e 62 // initialies all classes and libraries
ml16c5l 3:5d860d0d589e 63 void init()
ml16c5l 3:5d860d0d589e 64 {
ml16c5l 3:5d860d0d589e 65 // need to initialise LCD and Gamepad
ml16c5l 3:5d860d0d589e 66 lcd.init();
ml16c5l 3:5d860d0d589e 67 pad.init();
ml16c5l 3:5d860d0d589e 68
ml16c5l 3:5d860d0d589e 69 // initialise the game with correct ball and paddle sizes
ml16c5l 3:5d860d0d589e 70 copter.init(COPTER_WIDTH,COPTER_HEIGHT,WALL_SIZE,COPTER_SPEED);
ml16c5l 3:5d860d0d589e 71
ml16c5l 3:5d860d0d589e 72 }
ml16c5l 3:5d860d0d589e 73
ml16c5l 3:5d860d0d589e 74 // this function draws each frame on the LCD
ml16c5l 3:5d860d0d589e 75 void render()
ml16c5l 3:5d860d0d589e 76 {
ml16c5l 3:5d860d0d589e 77 // clear screen, re-draw and refresh
ml16c5l 3:5d860d0d589e 78 lcd.clear();
ml16c5l 3:5d860d0d589e 79 copter.draw(lcd);
ml16c5l 3:5d860d0d589e 80 lcd.refresh();
ml16c5l 3:5d860d0d589e 81 }
ml16c5l 3:5d860d0d589e 82
ml16c5l 3:5d860d0d589e 83 // simple splash screen displayed on start-up
ml16c5l 3:5d860d0d589e 84 void welcome() {
ml16c5l 3:5d860d0d589e 85
ml16c5l 3:5d860d0d589e 86 lcd.printString(" Copter! ",0,1);
ml16c5l 3:5d860d0d589e 87 lcd.printString(" Press Start ",0,4);
ml16c5l 3:5d860d0d589e 88 lcd.refresh();
ml16c5l 3:5d860d0d589e 89
ml16c5l 3:5d860d0d589e 90 // wait flashing LEDs until start button is pressed
ml16c5l 3:5d860d0d589e 91 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
ml16c5l 3:5d860d0d589e 92 pad.leds_on();
ml16c5l 3:5d860d0d589e 93 wait(0.1);
ml16c5l 3:5d860d0d589e 94 pad.leds_off();
ml16c5l 3:5d860d0d589e 95 wait(0.1);
ml16c5l 3:5d860d0d589e 96 }
ml16c5l 3:5d860d0d589e 97
ml16c5l 3:5d860d0d589e 98 }