ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Tue Apr 30 09:43:56 2019 +0000
Revision:
11:1c5c549ba75e
Parent:
10:0c200ed5973a
Child:
12:1dfc34bc8382
Fixed the bugs with private and public variables as well as naming conventions of the buttons.

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