ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Sat Apr 06 12:08:39 2019 +0000
Revision:
2:888634fff8ff
Parent:
1:2d3139578aca
Child:
3:aa82968b7a8e
Added stopwatch however still needs adjusting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll16o2l 0:03363a508918 1 /*
ll16o2l 0:03363a508918 2 ELEC2645 Embedded Systems Project
ll16o2l 0:03363a508918 3 School of Electronic & Electrical Engineering
ll16o2l 0:03363a508918 4 University of Leeds
ll16o2l 0:03363a508918 5 Name: Oliver Luong
ll16o2l 0:03363a508918 6 Username: ll16o2l
ll16o2l 0:03363a508918 7 Student ID Number:201140613
ll16o2l 0:03363a508918 8 Date:05/03/2019
ll16o2l 1:2d3139578aca 9 */
ll16o2l 1:2d3139578aca 10
ll16o2l 1:2d3139578aca 11 ///////// pre-processor directives ////////
ll16o2l 1:2d3139578aca 12 #include "mbed.h"
ll16o2l 1:2d3139578aca 13 #include "Gamepad.h"
ll16o2l 1:2d3139578aca 14 #include "N5110.h"
ll16o2l 2:888634fff8ff 15 #include "DodgeEngine.h"
ll16o2l 2:888634fff8ff 16 #include "Stopwatch.h" // published library
ll16o2l 2:888634fff8ff 17
ll16o2l 1:2d3139578aca 18
ll16o2l 2:888634fff8ff 19 #define PLAYER_WIDTH 14
ll16o2l 2:888634fff8ff 20 #define PLAYER_HEIGHT 14
ll16o2l 2:888634fff8ff 21 #define OBJECTS_SIZE 2
ll16o2l 2:888634fff8ff 22 #define OBJECTS_SPEED 3 /////////////////Want to make it changing
ll16o2l 2:888634fff8ff 23
ll16o2l 2:888634fff8ff 24 //#ifdef WITH_TESTING
ll16o2l 2:888634fff8ff 25 //# include "tests.h"
ll16o2l 2:888634fff8ff 26 //#endif
ll16o2l 1:2d3139578aca 27
ll16o2l 1:2d3139578aca 28 /////////////// structs /////////////////
ll16o2l 1:2d3139578aca 29 struct UserInput {
ll16o2l 2:888634fff8ff 30 Direction d;
ll16o2l 2:888634fff8ff 31 float mag;
ll16o2l 1:2d3139578aca 32 };
ll16o2l 1:2d3139578aca 33 /////////////// objects ///////////////
ll16o2l 1:2d3139578aca 34 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
ll16o2l 1:2d3139578aca 35 Gamepad pad;
ll16o2l 2:888634fff8ff 36 DodgeEngine dodge;
ll16o2l 2:888634fff8ff 37 Stopwatch _stopwatch;
ll16o2l 1:2d3139578aca 38
ll16o2l 1:2d3139578aca 39 ///////////// prototypes ///////////////
ll16o2l 1:2d3139578aca 40 void init();
ll16o2l 1:2d3139578aca 41 void update_game(UserInput input);
ll16o2l 1:2d3139578aca 42 void render();
ll16o2l 1:2d3139578aca 43 void welcome();
ll16o2l 1:2d3139578aca 44
ll16o2l 1:2d3139578aca 45 ///////////// functions ////////////////
ll16o2l 1:2d3139578aca 46 int main()
ll16o2l 1:2d3139578aca 47 {
ll16o2l 2:888634fff8ff 48 //#ifdef WITH_TESTING
ll16o2l 2:888634fff8ff 49 // int number_of_failures = run_all_tests();
ll16o2l 2:888634fff8ff 50 //
ll16o2l 2:888634fff8ff 51 // if(number_of_failures > 0) return number_of_failures;
ll16o2l 2:888634fff8ff 52 //#endif
ll16o2l 1:2d3139578aca 53
ll16o2l 2:888634fff8ff 54 int fps = 15; // frames per second
ll16o2l 1:2d3139578aca 55
ll16o2l 1:2d3139578aca 56 init(); // initialise and then display welcome screen...
ll16o2l 1:2d3139578aca 57 welcome(); // waiting for the user to start
ll16o2l 1:2d3139578aca 58
ll16o2l 1:2d3139578aca 59 render(); // first draw the initial frame
ll16o2l 1:2d3139578aca 60 wait(1.0f/fps); // and wait for one frame period
ll16o2l 1:2d3139578aca 61
ll16o2l 2:888634fff8ff 62 int lose = dodge.get_lose();
ll16o2l 1:2d3139578aca 63 // game loop - read input, update the game state and render the display
ll16o2l 2:888634fff8ff 64 while (lose == 0) {
ll16o2l 2:888634fff8ff 65 dodge.read_input(pad);
ll16o2l 2:888634fff8ff 66 dodge.update(pad);
ll16o2l 1:2d3139578aca 67 render();
ll16o2l 1:2d3139578aca 68 wait(1.0f/fps);
ll16o2l 1:2d3139578aca 69 }
ll16o2l 2:888634fff8ff 70 welcome();
ll16o2l 1:2d3139578aca 71 }
ll16o2l 1:2d3139578aca 72
ll16o2l 1:2d3139578aca 73 // initialies all classes and libraries
ll16o2l 1:2d3139578aca 74 void init()
ll16o2l 1:2d3139578aca 75 {
ll16o2l 1:2d3139578aca 76 // need to initialise LCD and Gamepad
ll16o2l 1:2d3139578aca 77 lcd.init();
ll16o2l 1:2d3139578aca 78 pad.init();
ll16o2l 1:2d3139578aca 79
ll16o2l 1:2d3139578aca 80 // initialise the game with correct ball and paddle sizes
ll16o2l 2:888634fff8ff 81 dodge.init(PLAYER_WIDTH,PLAYER_HEIGHT,OBJECTS_SIZE,OBJECTS_SPEED); ////////////CHANGE
ll16o2l 1:2d3139578aca 82
ll16o2l 1:2d3139578aca 83 }
ll16o2l 1:2d3139578aca 84
ll16o2l 1:2d3139578aca 85 // this function draws each frame on the LCD
ll16o2l 1:2d3139578aca 86 void render()
ll16o2l 1:2d3139578aca 87 {
ll16o2l 1:2d3139578aca 88 // clear screen, re-draw and refresh
ll16o2l 1:2d3139578aca 89 lcd.clear();
ll16o2l 2:888634fff8ff 90 dodge.draw(lcd);
ll16o2l 1:2d3139578aca 91 lcd.refresh();
ll16o2l 1:2d3139578aca 92 }
ll16o2l 1:2d3139578aca 93
ll16o2l 1:2d3139578aca 94 // simple splash screen displayed on start-up
ll16o2l 1:2d3139578aca 95 void welcome() {
ll16o2l 1:2d3139578aca 96
ll16o2l 2:888634fff8ff 97 lcd.printString(" Dodge! ",0,1);
ll16o2l 1:2d3139578aca 98 lcd.printString(" Press Start ",0,4);
ll16o2l 1:2d3139578aca 99 lcd.refresh();
ll16o2l 1:2d3139578aca 100
ll16o2l 1:2d3139578aca 101 // wait flashing LEDs until start button is pressed
ll16o2l 2:888634fff8ff 102 while (pad.check_event(Gamepad::START_PRESSED) == false) {
ll16o2l 1:2d3139578aca 103 pad.leds_on();
ll16o2l 1:2d3139578aca 104 wait(0.1);
ll16o2l 1:2d3139578aca 105 pad.leds_off();
ll16o2l 1:2d3139578aca 106 wait(0.1);
ll16o2l 1:2d3139578aca 107 }
ll16o2l 2:888634fff8ff 108 _stopwatch.start();
ll16o2l 1:2d3139578aca 109
ll16o2l 2:888634fff8ff 110 }
ll16o2l 2:888634fff8ff 111
ll16o2l 2:888634fff8ff 112 //void lose(){
ll16o2l 2:888634fff8ff 113 // int _lose = DodgeEngine.get_lose;
ll16o2l 2:888634fff8ff 114 //}