Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

main.cpp

Committer:
AhmedPlaymaker
Date:
2019-04-23
Revision:
41:4edac50f010d
Parent:
40:1debed7ec01c
Child:
46:dc7dccae9f9e

File content as of revision 41:4edac50f010d:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Ahmed Nomaan Adamjee
Username: AhmedPlaymaker
Student ID Number: 201161436
Date:
*/

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "FXOS8700CQ.h"
#include "StartScreen.h"
#include "SnakevsBlock.h"
#include "SDFileSystem.h"

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

/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
FXOS8700CQ device(I2C_SDA,I2C_SCL);
StartScreen _start;
SnakevsBlock game;
AnalogIn noisy(PTB0); //This creates a random noise which I can use to seed the random numbers.
// Connections to SD card holder on K64F (SPI interface)
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
//Serial serial(USBTX, USBRX);  // for PC debug

///////////// prototypes //////////////
void init();
void update_game(UserInput input);
void refresh_game();

//Constants//
int fps = 40;  // frames per second
int g_mode = 1; //selects between joystick and motion control.

///////////// MAIN ////////////////
int main()
{
    init();
    _start.screen_saver(lcd, pad);
    _start.instruct(lcd, pad);
    _start.info(lcd, pad);
    while(1)  { //This loop is created for Play/Continue configuration
        _start.main_menu(lcd, pad, sd);
        fps = _start.fps;
        g_mode = _start.g_mode;
         // start the game
        refresh_game();
        
        wait(1.0f/fps);  
    
        // snakeVSblock - detect input respect to the menu options, and update data and refresh screen
        while (1) {
            
            game.read_input(pad, device, g_mode);
            int back = game.update(pad);
            refresh_game();
            
            wait(1.0f/fps);
            if(back)  {
                break;
            }
                    
        }
    }
}

void init()
{
    // need to initialise LCD and Gamepad 
    lcd.init();
    device.init();
    pad.init();
    pad.tone(1000.0,0.1);
    game.init();
    srand(100000*noisy.read());
}

void refresh_game()
{
    lcd.clear();  
    game.draw(lcd, pad, sd);
    game.get_pos();
    lcd.refresh();
}