submit

Dependencies:   mbed Gamepad N5110

main.cpp

Committer:
694617778
Date:
2019-04-13
Revision:
1:b49c36604125
Parent:
0:b67614a0c4cf
Child:
2:934daa65f73d

File content as of revision 1:b49c36604125:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "snake.h"
#include "Engine.h"

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

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

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

///////////// functions ////////////////
int main()
{
    int fps = 8;  // frames per second
    
    init(); 
    welcome();  // show welcome display, waiting for the user to start
    

    while (1) {

    run();
    wait(5.0f/fps);  // and wait for one frame period
}
    
     // game loop - read input, update the game state and render the display

}

// simple splash screen displayed on start-up
void welcome() {
    
    lcd.printString(" ###SNAKE### ",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);
    }
 
}

// this function draws each frame on the LCD
void run()
{
    // clear screen, re-draw and refresh
    lcd.clear();
    int length = snake.get_length();
    snake.draw(lcd,length);
    int direction = engine.get_direction(pad);
    snake.update(direction,length);
    lcd.refresh();
}

// initialies all classes and libraries
void init()
{
    // need to initialise LCD and Gamepad 
    lcd.init();
    pad.init();    
    snake.init(1,3);
     
}