Racing game

Dependencies:   mbed

main.cpp

Committer:
batJoro
Date:
2019-05-10
Revision:
12:bc9a43f56261
Parent:
11:0e6a221ad8a9

File content as of revision 12:bc9a43f56261:



/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering University of Leeds

Name: Dobri Tsvetkov
Username: el17dtt
Student ID Number: 201154059
Date: 12.03.2019
*/


#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "menu.h"
#include "engine.h"



/////////////// structs /////////////////


/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad gamepad;
Engine engine;

///////////// prototypes ///////////////
void init();
void welcome();
void finish();
void render();

///////////// functions ////////////////
int main() {
    
    int fps = 9;  // frames per second
    
    init();
    
    welcome();
    // game loop - read input, update the game state and render the display
    while(1) {
        engine.read_input(gamepad, 1.0f/fps);
        engine.update(gamepad, lcd, 1.0f/fps);
        if ( engine.get_lap() == 1) break;
        render();
        wait(1.0f/fps);
    }
    
    finish();
}


// initialies all classes and libraries
void init()
{

    // need to initialise LCD and Gamepad and Engine
    lcd.init();
    gamepad.init();
    
    // init the engine and start the game at 0 speed
    engine.init(48 , 84, 0);
    
    gamepad.leds_on();
    lcd.setContrast(0.4);
}
// function to call the intro method of the menu class
void welcome() {
    
    Menu menu;   
    menu.intro(lcd, gamepad);
    menu.startMainMenu(lcd, gamepad);

}
// display lap time and finish
void finish() {
    clearScreen(lcd);
    if(engine.get_lap_time() < 37) {
        printSoundString(lcd, gamepad, "Great job", 0, 0, 876, 0.1);
        printSoundString(lcd, gamepad, "lap time < 37s", 0, 1, 876, 0.1);
        printSoundString(lcd, gamepad, "The planet is ", 0, 2, 876, 0.1);
        printSoundString(lcd, gamepad, "safe", 0, 3, 876, 0.1);
    }
    if(engine.get_lap_time() > 37) {
        printSoundString(lcd, gamepad, "Game over", 0, 1, 876, 0.1);
        printSoundString(lcd, gamepad, "lap time > 37s", 0, 1, 876, 0.1);
        printSoundString(lcd, gamepad, "The delivery", 0, 2, 876, 0.1);
        printSoundString(lcd, gamepad, "failed and the ", 0, 3, 876, 0.1);
        printSoundString(lcd, gamepad, "planet will", 0, 4, 876, 0.1);
        printSoundString(lcd, gamepad, "be destroyed", 0, 5, 876, 0.1);
    }
    wait(60); 
}
// this function draws each frame on the LCD
void render() {
    
        lcd.clear();
        engine.draw(lcd);
        lcd.refresh();
}