submit

Dependencies:   mbed Gamepad N5110

main.cpp

Committer:
694617778
Date:
2019-04-20
Revision:
10:68076e3dcc33
Parent:
9:18b059e5abb9
Child:
11:543c62bed764

File content as of revision 10:68076e3dcc33:

///////// 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
    while(1){    
      init(); 
      engine.welcome(pad,lcd);  // show welcome display, waiting for the user to start
      engine.menu(pad,lcd);  // show the select display, waiting for the user to select
    // game loop - read input, update the game state and render the display
      while (snake.over == 0) {
        run(); // run the game
        while (pad.check_event(Gamepad::START_PRESSED) == true){
            engine.pause(pad,lcd);
            }
        wait(engine.p/fps);  // and wait for one frame period
      }
    over();  // show gameover display, waiting for the user to restart
    }
}

// this function draws each frame on the LCD
void run()
{
    // clear screen, re-draw and refresh
    lcd.clear();
    int length = snake.get_length();
    int direction = engine.get_direction(pad);
    snake.check_eat(pad);
    snake.draw(lcd,length);
    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);
    engine.init();

}

void over()
{
    int s = snake.get_score(); // get teh scores to show at the gameover display
    engine.gameover(pad,lcd,s);

}