ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

main.cpp

Committer:
ll16o2l
Date:
2019-04-06
Revision:
2:888634fff8ff
Parent:
1:2d3139578aca
Child:
3:aa82968b7a8e

File content as of revision 2:888634fff8ff:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Oliver Luong
Username: ll16o2l
Student ID Number:201140613
Date:05/03/2019
*/

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "DodgeEngine.h"
#include "Stopwatch.h" // published library


#define PLAYER_WIDTH 14
#define PLAYER_HEIGHT 14
#define OBJECTS_SIZE 2 
#define OBJECTS_SPEED 3 /////////////////Want to make it changing

//#ifdef WITH_TESTING
//# include "tests.h"
//#endif

/////////////// structs /////////////////
struct UserInput {
    Direction d; 
    float mag;
};
/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
DodgeEngine dodge;
Stopwatch _stopwatch;

///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
void welcome();

///////////// functions ////////////////
int main()
{
//#ifdef WITH_TESTING
//    int number_of_failures = run_all_tests();
//
//    if(number_of_failures > 0) return number_of_failures;
//#endif

    int fps = 15; // frames per second

    init();     // initialise and then display welcome screen...
    welcome();  // waiting for the user to start
    
    render();  // first draw the initial frame 
    wait(1.0f/fps);  // and wait for one frame period

    int lose = dodge.get_lose(); 
    // game loop - read input, update the game state and render the display
    while (lose == 0) {
        dodge.read_input(pad);
        dodge.update(pad);
        render();
        wait(1.0f/fps);
    }
    welcome();
}

// initialies all classes and libraries
void init()
{
    // need to initialise LCD and Gamepad 
    lcd.init();
    pad.init();
     
    // initialise the game with correct ball and paddle sizes
    dodge.init(PLAYER_WIDTH,PLAYER_HEIGHT,OBJECTS_SIZE,OBJECTS_SPEED);  ////////////CHANGE

}

// this function draws each frame on the LCD
void render()
{
    // clear screen, re-draw and refresh
    lcd.clear();  
    dodge.draw(lcd);
    lcd.refresh();
}

// simple splash screen displayed on start-up
void welcome() {
    
    lcd.printString("     Dodge!    ",0,1);  
    lcd.printString("  Press Start ",0,4);
    lcd.refresh();
     
    // wait flashing LEDs until start button is pressed 
    while (pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
    _stopwatch.start();
 
}

//void lose(){
//     int _lose = DodgeEngine.get_lose;
//}