ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Tue Apr 30 09:32:46 2019 +0000
Revision:
10:0c200ed5973a
Parent:
8:c3153fd4d8ce
Child:
11:1c5c549ba75e
Adding in the user input to the main.cpp file and adding the instructions to the game mechanics.

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