ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Thu Apr 25 15:08:52 2019 +0000
Revision:
3:aa82968b7a8e
Parent:
2:888634fff8ff
Child:
8:c3153fd4d8ce
Completed Dodge game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll16o2l 3:aa82968b7a8e 1 /**
ll16o2l 3:aa82968b7a8e 2 * ELEC2645 Embedded Systems Project
ll16o2l 3:aa82968b7a8e 3 * School of Electronic & Electrical Engineering
ll16o2l 3:aa82968b7a8e 4 * University of Leeds
ll16o2l 3:aa82968b7a8e 5 * @file main.cpp
ll16o2l 3:aa82968b7a8e 6 * @brief
ll16o2l 3:aa82968b7a8e 7 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 8 * Username: ll16o2l
ll16o2l 3:aa82968b7a8e 9 * Student ID Number:201140613
ll16o2l 3:aa82968b7a8e 10 * @Date 05/03/2019
ll16o2l 1:2d3139578aca 11 */
ll16o2l 1:2d3139578aca 12
ll16o2l 1:2d3139578aca 13 ///////// pre-processor directives ////////
ll16o2l 1:2d3139578aca 14 #include "mbed.h"
ll16o2l 1:2d3139578aca 15 #include "Gamepad.h"
ll16o2l 1:2d3139578aca 16 #include "N5110.h"
ll16o2l 2:888634fff8ff 17 #include "DodgeEngine.h"
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 3:aa82968b7a8e 22 #define OBJECTS_SPEED 4 /////////////////Want to make it changing
ll16o2l 3:aa82968b7a8e 23 #define LIVES 5
ll16o2l 3:aa82968b7a8e 24 #define KIT_SIZE 7
ll16o2l 1:2d3139578aca 25
ll16o2l 1:2d3139578aca 26 /////////////// structs /////////////////
ll16o2l 1:2d3139578aca 27 struct UserInput {
ll16o2l 2:888634fff8ff 28 Direction d;
ll16o2l 2:888634fff8ff 29 float mag;
ll16o2l 1:2d3139578aca 30 };
ll16o2l 1:2d3139578aca 31 /////////////// objects ///////////////
ll16o2l 1:2d3139578aca 32 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
ll16o2l 1:2d3139578aca 33 Gamepad pad;
ll16o2l 2:888634fff8ff 34 DodgeEngine dodge;
ll16o2l 1:2d3139578aca 35
ll16o2l 1:2d3139578aca 36 ///////////// prototypes ///////////////
ll16o2l 1:2d3139578aca 37 void init();
ll16o2l 1:2d3139578aca 38 void update_game(UserInput input);
ll16o2l 1:2d3139578aca 39 void render();
ll16o2l 1:2d3139578aca 40 void welcome();
ll16o2l 1:2d3139578aca 41
ll16o2l 1:2d3139578aca 42 ///////////// functions ////////////////
ll16o2l 1:2d3139578aca 43 int main()
ll16o2l 1:2d3139578aca 44 {
ll16o2l 3:aa82968b7a8e 45 // This is used for how many loops per second (i.e frames per second)
ll16o2l 3:aa82968b7a8e 46 int fps = 15;
ll16o2l 1:2d3139578aca 47
ll16o2l 3:aa82968b7a8e 48 init(); // initialise and then display welcome screen
ll16o2l 1:2d3139578aca 49 welcome(); // waiting for the user to start
ll16o2l 1:2d3139578aca 50
ll16o2l 1:2d3139578aca 51 render(); // first draw the initial frame
ll16o2l 1:2d3139578aca 52 wait(1.0f/fps); // and wait for one frame period
ll16o2l 3:aa82968b7a8e 53
ll16o2l 3:aa82968b7a8e 54 // This will set the initial value to be used in the loop
ll16o2l 3:aa82968b7a8e 55 int lose = 0;
ll16o2l 3:aa82968b7a8e 56 // This sets the time to 0 until the game starts
ll16o2l 3:aa82968b7a8e 57 float time = 0;
ll16o2l 3:aa82968b7a8e 58
ll16o2l 1:2d3139578aca 59 // game loop - read input, update the game state and render the display
ll16o2l 3:aa82968b7a8e 60 while (lose == 0) { // Until the value of lose changes to 1 keep looping
ll16o2l 3:aa82968b7a8e 61 dodge.read_input(pad); // Execute the read_input method from dodge source file
ll16o2l 3:aa82968b7a8e 62 dodge.update(pad); // Execute the update method from dodge source file
ll16o2l 3:aa82968b7a8e 63 render(); // Execute the render method
ll16o2l 3:aa82968b7a8e 64 wait(1.0f/fps); // This controls how fast the while loop iterates
ll16o2l 3:aa82968b7a8e 65 time = time + 0.06666f; // Adds the time for every iteration to count the time
ll16o2l 3:aa82968b7a8e 66 // printf("Time = %.2f \n", time); // Used to test the time is working
ll16o2l 3:aa82968b7a8e 67 dodge.time(time); // Input the time from the main file to dodge source file
ll16o2l 3:aa82968b7a8e 68 lose = dodge.get_lose(); // Execute the get_lose method then save return value to time variable
ll16o2l 1:2d3139578aca 69 }
ll16o2l 3:aa82968b7a8e 70 main(); // Execute the main method after lose = 1 and the while loop stops
ll16o2l 3:aa82968b7a8e 71 wait(1.0f);
ll16o2l 3:aa82968b7a8e 72 }/**
ll16o2l 3:aa82968b7a8e 73 * This method will be used to initialies all classes and libraries.
ll16o2l 3:aa82968b7a8e 74 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 75 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 76 */
ll16o2l 1:2d3139578aca 77 void init()
ll16o2l 1:2d3139578aca 78 {
ll16o2l 3:aa82968b7a8e 79 // initialise LCD and Gamepad
ll16o2l 1:2d3139578aca 80 lcd.init();
ll16o2l 1:2d3139578aca 81 pad.init();
ll16o2l 1:2d3139578aca 82
ll16o2l 3:aa82968b7a8e 83 // initialise the game with pre-determerined directives
ll16o2l 3:aa82968b7a8e 84 dodge.init(PLAYER_WIDTH,PLAYER_HEIGHT,OBJECTS_SIZE,OBJECTS_SPEED,LIVES,KIT_SIZE);
ll16o2l 1:2d3139578aca 85
ll16o2l 1:2d3139578aca 86 }
ll16o2l 1:2d3139578aca 87
ll16o2l 3:aa82968b7a8e 88 /**
ll16o2l 3:aa82968b7a8e 89 * This method will be used to draw the frames onto the LCD.
ll16o2l 3:aa82968b7a8e 90 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 91 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 92 */
ll16o2l 1:2d3139578aca 93 void render()
ll16o2l 1:2d3139578aca 94 {
ll16o2l 1:2d3139578aca 95 // clear screen, re-draw and refresh
ll16o2l 1:2d3139578aca 96 lcd.clear();
ll16o2l 2:888634fff8ff 97 dodge.draw(lcd);
ll16o2l 1:2d3139578aca 98 lcd.refresh();
ll16o2l 1:2d3139578aca 99 }
ll16o2l 3:aa82968b7a8e 100 /**
ll16o2l 3:aa82968b7a8e 101 * This method will be used to initialies the
ll16o2l 3:aa82968b7a8e 102 * welcome screen and wait for user input.
ll16o2l 3:aa82968b7a8e 103 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 104 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 105 */
ll16o2l 1:2d3139578aca 106 void welcome() {
ll16o2l 1:2d3139578aca 107
ll16o2l 3:aa82968b7a8e 108 // Write onto the LCD and refresh
ll16o2l 2:888634fff8ff 109 lcd.printString(" Dodge! ",0,1);
ll16o2l 1:2d3139578aca 110 lcd.printString(" Press Start ",0,4);
ll16o2l 1:2d3139578aca 111 lcd.refresh();
ll16o2l 1:2d3139578aca 112
ll16o2l 1:2d3139578aca 113 // wait flashing LEDs until start button is pressed
ll16o2l 2:888634fff8ff 114 while (pad.check_event(Gamepad::START_PRESSED) == false) {
ll16o2l 1:2d3139578aca 115 pad.leds_on();
ll16o2l 1:2d3139578aca 116 wait(0.1);
ll16o2l 1:2d3139578aca 117 pad.leds_off();
ll16o2l 1:2d3139578aca 118 wait(0.1);
ll16o2l 1:2d3139578aca 119 }
ll16o2l 3:aa82968b7a8e 120 }