Falldown completed game

Dependencies:   mbed

main.cpp

Committer:
el17cr
Date:
2019-05-09
Revision:
8:8d9c5a7e57d3
Parent:
7:cf469c3505a2

File content as of revision 8:8d9c5a7e57d3:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Connor Rainey
Username: el17cr
Student ID Number: 201125971
Date: 09/05/2019
*/

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

// These values cannot be changed
#define GROUND_HEIGHT 2
#define BALL_SIZE 3


// define initial ground width, ground speed and level
int ground_width = 40;
double ground_speed = 0.3;
int level = 1;

// struct holds data for reading joystick
struct UserInput {
    Direction d;
    float mag;
};

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // K64F - pwr from 3V3
Gamepad pad; //gamepad library
Falldown falldown; // falldown class

void init();
void update_game(UserInput input);
void render();
void welcome();
void game_over();
void game_complete();


int main()
{
    init();
    welcome();
    render();
    wait(0.1); //delay required before clearing the display and constructing the next frame

    // start game loop
    while(1) {
        falldown.read_input(pad);
        falldown.update(pad);
        render();
        wait(0.1);
        // check goal funtion returns a 1 if ball reaches the bottom of the screen
        if (falldown.check_goal() == 1) {
            level = level++; // increment level if goal reached
            ground_width = ground_width + 1; // increases ground width
            ground_speed = ground_speed + 0.1; // increase ground speed
            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
            wait(0.1);

            // if on final level, and goal reached
        } else if ((level == 5) && (falldown.check_complete() == 1)) {
            game_complete(); // call game complete function
            wait(0.5);
            level = 1; // if game complete function is broken and player chooses to play again, re-set values for level, ground width and ground speed
            ground_width = 40;
            ground_speed = 0.5;
            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
            wait(0.1);

            //
        } else if (falldown.check_end() == 1)  {
            game_over(); // call game over function
            level = 1; // if game over function is broken and player chooses to play again, re-set values for level, ground width and ground speed
            ground_width = 40;
            ground_speed = 0.5;
            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
            wait(0.1);
        }
    }
}

void init() // initialise lcd, gamepad and game
{
    lcd.init();
    pad.init();

    falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed);
}

void render() // render game
{
    lcd.clear(); //clear lcd
    falldown.draw(lcd); //draws game
    char buffer[1]; //displays level
    sprintf(buffer,"%2d",level);
    lcd.printString(buffer,0,1);
    lcd.refresh();
}

void welcome() //Displays welcome screen
{
    lcd.printString("*Falldown*",12,0);
    lcd.printString("Get to",24,2);
    lcd.printString("the bottom!",11,3);
    lcd.printString("Press Start",11,5);

    lcd.refresh();

    // leds flash until start pressed
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
}

void game_over()
{
    lcd.init(); //re initialise lcd
    lcd.printString("Game Over! :-(",0,0);
    lcd.printString("centre",25,2);
    lcd.printString("joystick",20,3);
    lcd.printString("Play again? A",0,5);
    lcd.refresh();
    while (pad.check_event(Gamepad::A_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
        if (pad.check_event(Gamepad::A_PRESSED) == true) {
            break;  // break while loop when button is pressed and continue main game loop
        }
    }
    wait(0.5);
    pad.init(); //bug fix, initialise pad ensures button does remain true from last time it was pressed
}

// Function adjusted for completion of the game
void game_complete()
{
    lcd.init();
    lcd.printString("Game Complete!",0,0);
    lcd.printString(":-)",33,2);
    lcd.printString("Play again? A",0,4);
    lcd.refresh();
    while (pad.check_event(Gamepad::A_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
        if (pad.check_event(Gamepad::A_PRESSED) == true) {
            break;
        }
    }
    wait(0.5);
    pad.init();
}