ELEC2645 (2018/19) / Mbed 2 deprecated el17arm

Dependencies:   mbed

main.cpp

Committer:
el17arm
Date:
2019-04-24
Revision:
45:bad704c546d4
Parent:
44:e29458976114
Child:
48:bd1f31fbfee3

File content as of revision 45:bad704c546d4:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Andrew Milner
Username: el17arm
Student ID Number: 201162219
Date: 18/03/19
*/

///////// pre-processor directives ////////

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

/////////////// objects ///////////////

Gameengine game;
Gamepad pad;
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
AnalogIn pot0(PTB2);
BusOut oxygen(PTA1, PTA2, PTC2);
BusOut lives(PTC3, PTC4, PTD3);

///////////// prototypes ///////////////

/** 
  * @brief sets the contrast of lcd screen
  * @details contrast is adjust with analog input PTB2
  */
void contrast();
/** 
  * @brief initialises all classes and libraries and default game settings
  * @details initialises LCD and gamepad library and sets screen brightness
  */
void init();
/** 
  * @brief Displays opening screen
  * @details Screen displays title of game and instructs player to press start to begin
  */
void start_screen();
/** 
  * @brief Controls LEDs on front panel
  * @details Every time a player dies a RHS LED is turned off, as time reduces
  * LEDs turn off on LHS
  */
void leds();
/** 
  * @brief initialises and renders all level objects
  * @details Contains functions for all levels and will display next level objects
  * once player has completed each level
  */
void render();
/** 
  * @brief Displays screen if game over or game complete
  * @details Screen will display player's final score and display game over or
  * game complete
  */
void restart();

///////////// functions ////////////////

/** Level Class
@author Andrew Milner University of Leeds
@brief Sets position of all level objects
@date April 2019

@code 

int main()
{
    init();     // initialises screen and default game settings
    start_screen();     // waits for user to press start to begin
    
    // game loop, reads input and updates game accordingly
    while (1) {

        contrast();     // adjusts contrast of screen
        render();       // draws all level objects on to screen
        game.update(lcd, pad); // updates movement, collected keys, lives, time left
        restart();      // restart screen if game over or end screen if game complete displays final score
        wait(0.1);      // wait function to control fps
    }
}
@endcode 
*/

//initialises all classes and libraries and default game settings
void init()
{
    lcd.init();
    lcd.normalMode();      // normal colour mode
    lcd.refresh();
    lcd.setBrightness(1.0);
    pad.init();
    game.game_init();
    contrast(); 
}

// detects which level is being played and draws the level objects with collision settings
void render()
{
    game.draw_l1(lcd, pad); // level 1 
    game.draw_l2(lcd, pad); // level 2
    game.draw_l3(lcd, pad); // level 3
    leds();                 // function to update leds to reflect time and lives left
}

// controls contrast of the screen
void contrast()
{
    lcd.refresh();
    float con = pot0.read();
    lcd.setContrast(con);
    lcd.clear();
}

// displays start screen and waits for user to press start
void start_screen()
{   
    lcd.drawSprite(2,20,8,3,(int *)miner_right);
    lcd.drawSprite(79,20,8,3,(int *)miner_left);
    lcd.drawRect(0,0,84,48, FILL_TRANSPARENT);
    lcd.printString("MANIC MILNER",7,1);
    lcd.printString("Press start!",8,4);
    lcd.refresh();
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
    }
}

// displays appropriate screen with final score for game over and game complete
void restart()
{
    if (game.game_over() == true) {
        lcd.clear();
        game.get_score(lcd);
        lcd.drawRect(0,0,84,48, FILL_TRANSPARENT);
        lcd.printString("Game Over! ",14,1);
        lcd.printString("Score ",18,2);
        lcd.printString("Play Again?",10,4);
    }
    if (game.game_complete(lcd) == true) {
        lcd.clear();
        game.get_score(lcd);
        lcd.drawRect(0,0,84,48, FILL_TRANSPARENT);
        lcd.printString("YOU WIN!!! ",12,1);
        lcd.printString("Score ",18,2);
        lcd.printString("Well done!!!",8,4);
    }
}

// leds indicate how many lives and time left
void leds()
{   
    // fsm to display lives left
    int l_leds[4]  = {0b111,0b110,0b100,0b000};
    //fsm to display how much time (oxygen) left
    int r_leds[4]  = {0b111,0b110,0b100,0b000};
    
    // function keeps track of time elapsed and changes leds accordingly
    oxygen = l_leds[game.oxygen_leds()];
    // function keeps track of lives left and changes leds accordingly
    lives = r_leds[game.lives_leds()];
}