ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Sun May 05 11:19:38 2019 +0000
Revision:
12:1dfc34bc8382
Parent:
11:1c5c549ba75e
Fixed the Instructions, problems with moving the screen up and down.

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