submit

Dependencies:   mbed Gamepad N5110

main.cpp

Committer:
694617778
Date:
2019-04-14
Revision:
5:82094591b4b4
Parent:
4:323f42022d87
Child:
6:feb6351e6f8e

File content as of revision 5:82094591b4b4:

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

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

///////////// prototypes ///////////////
void init();
void run();
void over();
///////////// functions ////////////////
int main()
{
    int fps = 8;  // frames per second
    
    init(); 
    engine.welcome(pad,lcd);  // show welcome display, waiting for the user to start
    
    // game loop - read input, update the game state and render the display
    while (snake.over == 0) {
    run();
    wait(1.0f/fps);  // and wait for one frame period
    }
    over();
 
}

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

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

void over()
{
    int s = snake.get_score();
    engine.gameover(pad,lcd,s);

}