Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

main.cpp

Committer:
ellisbhastroud
Date:
2019-04-23
Revision:
10:9f54a6366e94
Parent:
9:bc34f2243e43
Child:
12:7f7fadb5c106

File content as of revision 10:9f54a6366e94:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Ellis Blackford Stroud
Username: el17ebs
Student ID Number: 201155309
Date: 09/05/19
*/

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Menu.h"
#include "GolfEngine.h"

// objects 
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Menu menu;
GolfEngine golf;

//functions
void init();
void inverseFlash();
//
int frame_rate;

int main()
{
    init();
    lcd.clear();
    menu.print_welcome(lcd);
    lcd.clear();
    bool start_game = false; 
    
    while(start_game == false) {   //menu loop navigates menu until game started
        
        menu.menu_screen(pad, lcd); 
        start_game = menu.menu_change(lcd, pad); //changes menu screen to selected screen the returns true when start game 
    }  
    lcd.clear();
    golf.printLevel(lcd); 
    lcd.refresh();
    wait(1);
    frame_rate = menu.get_frame_rate(); //changes frame rate to the value chosen in settings
    golf.init(frame_rate); //resets variables for game and sets frame rate

    //game loop to run game
    
    while(1){ 
        
        if(golf.get_hole_flag() == false) { //if ball not in hole
            
            lcd.clear();
            golf.read_input(pad);
            golf.update_ball(pad);
            golf.drawGame(lcd, pad);     
            lcd.refresh();
            wait(1.0f/frame_rate); //time between loops/frames (at lower frame rates ball may be harder to get in hole)
            
        } else if(golf.get_hole_flag() == true) { //if ball goes in hole end level and start new level
            
            inverseFlash(); //flashes screen pixels on and off
            inverseFlash();
            lcd.clear();
            golf.new_level(); //moves ball to new level position and increments level
            golf.reset_hole_flag(); //so that the game loop can continue
            golf.printLevel(lcd); //notifying user of start of new level 
            lcd.refresh();
            wait(1);
        }
    }
}

void init()
{
    lcd.init();
    pad.init();
    menu.init();
    lcd.setContrast(0.5f);
}

void inverseFlash()
{
    lcd.inverseMode();
    lcd.refresh();
    wait(0.25);
    lcd.normalMode();
    lcd.refresh();
    wait(0.25);
    lcd.inverseMode();
    lcd.refresh();
    wait(0.25);
    lcd.normalMode();
    lcd.refresh();
    wait(0.25);
}