submit

Dependencies:   mbed Gamepad N5110

main.cpp

Committer:
694617778
Date:
2019-04-13
Revision:
2:934daa65f73d
Parent:
1:b49c36604125
Child:
3:1358cbb05ad3

File content as of revision 2:934daa65f73d:

///////// 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 welcome();

///////////// functions ////////////////
int main()
{
    int fps = 8;  // frames per second
    
    init(); 
    welcome();  // 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
    }

 
}

// 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.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);
     
}