ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

main.cpp

Committer:
el17m2h
Date:
2019-05-09
Revision:
33:de130e274391
Parent:
31:5c4acae51026
Child:
34:a9b14a4ccd46

File content as of revision 33:de130e274391:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Melissa Hartmann
Username: el17m2h
Student ID Number: 201176603
Date: 09/05/2019
*/

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Engine.h"


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

// structs
struct UserInput {
    Direction d;
    float mag;
};

// objects
N5110 lcd(PTC5,PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // START, LCD SCE, LCD RST, LCD DC, LCD MOSI, LCD CLK, LCD Backlight
Gamepad pad;
Engine eng;
Doodler dood;

// functions
void init();
void update_game(UserInput input);
void render();
void welcome();
void check_score();
void game_over();

int main()
{
#ifdef WITH_TESTING
    int number_of_failures = run_all_tests();
    if(number_of_failures > 0) return number_of_failures;
#endif
    while(1) {
        init();
        welcome();
        int fps = 8;
        render();  // draws
        while(1) {
            render();
            eng.read_input(pad);
            check_score();
            wait(0.8f/fps);
            bool end_game = eng.get_game_over();
            if (end_game == true) {
                game_over();
                break;
            }
            if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
                break;
            }
        }
    }
}

// initialies all classes and libraries
void init()
{  // need to initialise LCD and Gamepad
    lcd.init();
    lcd.setBrightness(1);
    lcd.setContrast(0.5),
                    pad.init();
    eng.init();
}

void render()
{
    lcd.clear();
    eng.draw(lcd);
    lcd.refresh();
}

// Starting menu screen display
void welcome()
{
    lcd.printString(" Doodle Jump! ",0,1);
    lcd.printString("  Press Start ",0,4);
    lcd.refresh();
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
}

void check_score()
{
    bool score_added = eng.get_score_check();
    eng.update(pad);
    bool updated_score_added = eng.get_score_check();
    if ( (score_added == true) && (updated_score_added == true) ) {
        eng.subtract_score();
    }  // checking if the previous score has increased by one right after one update means that the a floor
}  // remains fixed at y = 10 (which adds a score). This will then prevent the score from increasing infinitely.

void game_over()
{
    lcd.clear();
    lcd.printString("Game Over!",3,1);
    lcd.printString("Press back",3,3);
    int score = eng.get_score();
    char buffer[14];
    sprintf(buffer,"SCORE:%d", score);
    lcd.printString(buffer, 3, 5);
    lcd.refresh();
    while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
}