Owen Cavender 201159294

Dependencies:   mbed Gamepad2

main.cpp

Committer:
el17oc
Date:
2020-05-28
Revision:
10:ee781d18e0f6
Parent:
9:a69a6a06dddf
Child:
11:e7c56013acd9
Child:
12:60c856354406

File content as of revision 10:ee781d18e0f6:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical appleering
University of Leeds
2019/20

Name: Owen Cavender
Username: el17oc
Student ID Number: 201159294
Date: 20/04/2020

The game loop will be entered after the game is initialised. The first step is to process the
user input (if any). The next step is to update the game apple depending on the input i.e.
moving a character or checking for collisions. The final step is to update the display and render
this on the LCD. There is usual////
*/

// includes
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "snake.h"
#include "GameEngine.h"




//objects
Gamepad pad;
Snake snake;
N5110 lcd;
Timer timer;
GameEngine engine;
//functions

void welcome();
void render();
void init();
void update_game_state();
void gameover_true();

//Serial pc(USBTX, USBRX);

int main()
{

    int fps = 6;  // frames per second

    init();                   // initialise and then display welcome screen...
    welcome();
    wait(1.0f/fps);  // and wait for one frame period
    render();
    timer.start();                                     //ROB atm the code works to here
    //need to draw an initial snake
    
    while (1) {
        
        snake.get_direction(pad);
        snake.render_clear_tail(lcd);
        engine.get_LEDs(pad, snake);           // =needs to be cleared before _x3, _y3 is updated
        snake.move_snake();
        
        snake.check_collisions();
        snake.apple_collected(lcd, pad, timer);
        
        snake.get_time(timer);
        engine.get_LEDs(pad, snake);


        gameover_true();
        snake.render(lcd);
        snake.print_display_time(lcd);
       

        wait(1.0f/fps);
    }

}

//added old score   - changed postion of timer and code

void init()
{

    snake.init();           //need to initialise snake class
    pad.init();
    lcd.init();
}



void welcome()
{

    
    lcd.printString("     SNAKE    ",0,1);
    lcd.printString("  Press Start ",0,4);

    while ( pad.start_pressed() == false) {
        lcd.setContrast( pad.read_pot1());
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
        lcd.refresh();
}


void render()
{
    lcd.clear();
    lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);
    lcd.printString("     GO!      ", 0,25);
    wait(1.0);
    lcd.refresh();
}




void gameover_true()      //taking action if crash has occured
{
    bool gameover = snake.get_gameover();
    int score = snake.get_score();

    while (gameover == true) {
        lcd.clear();
        lcd.printString( "  Game Over L ", 0, 2 );
        engine.print_scores(lcd, snake);

        if (score <= 7) {
            lcd.printString("    Loser     ", 0,10);
        }
        if (7< score <= 25) {
            lcd.printString("    Good Job!     ", 0,10);
        } else {
            lcd.printString("    SNAKE PRO     ", 0,10);
            lcd.printString(" ~.~.~.~.<8>~  ", 0, 5);
        }
        wait(3);
    
    lcd.refresh();
}
}

//has an apple been collected? if so increase length by 1 increase score by 1 and makes coin collecting noise and spawn new apple
//has snake touched itself or wall - if so end game - present score - make xxx sound fail --- apple needs to be different to the snakes body and different to wall
//has button button been pressed - if so change direction accordingly
//max length - when length = width x height - 1 - game complete
//each pixel of length must follow the front pixel
// 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake



//need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files