FINAL VERSION

Dependencies:   mbed

main.cpp

Committer:
jamesheavey
Date:
2019-04-27
Revision:
52:652b5f51319d
Parent:
51:9d5c10a88b22
Child:
53:8e3da0b58fe9

File content as of revision 52:652b5f51319d:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "BreakoutEngine.h"
#include "Bitmap.h"
#include<iostream>
#include "Sprites.h"
//#include "MotionControl.h"

#ifdef WITH_TESTING
# include "tests.h"
#endif

#define PADDLE_WIDTH 15
#define PADDLE_HEIGHT 2
#define BALL_SIZE 2
#define BALL_SPEED 3

/////////////// structs /////////////////
struct UserInput {
    Direction d;
    float mag;
};
/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
BreakoutEngine breakout;

///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
bool main_menu();
void settings();
void how_to_play();
void end_screen();
void title_screen();
void main_game(bool tilt);
int i = 0;
int prev_score = 0;

bool tilt = false;

///////////// functions ////////////////
int main()
{
#ifdef WITH_TESTING
    int number_of_failures = run_all_tests();

    if(number_of_failures > 0) return number_of_failures;
#endif
    title_screen();
    
}

// initialies all classes and libraries
void init(int prev_score)
{
    // need to initialise LCD and Gamepad 
    lcd.init();
    pad.init();
     
    // initialise the game with correct ball and paddle sizes
    breakout.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,pad.read_pot()*3+2,prev_score);

}

// this function draws each frame on the LCD
void render()
{
    // clear screen, re-draw and refresh
    lcd.clear();  
    breakout.draw(lcd);
    lcd.refresh();
}

// simple splash screen displayed on start-up
bool main_menu() {
    // wait flashing LEDs until start button is pressed 
    //bool joy = true;
    
    lcd.clear();
    lcd.printString("   START ",0,2);    // start with default as joystick
    lcd.printString("   SETTINGS ",0,3);   // choose between joystick and tilt
    lcd.printString("   HOW TO PLAY ",0,4);  // brief text on how to do stuff
    lcd.refresh();  
    
    int pointer = 0;
    
    while (pad.check_event(Gamepad::A_PRESSED) == false) {  //maybe add a jingle
        if (pad.check_event(Gamepad::L_PRESSED) && pointer > 0) {  // change to joystick
            pointer -= 1;
        } else if (pad.check_event(Gamepad::R_PRESSED) == S && pointer < 2) {
            pointer += 1;
        }
        
        switch(pointer){
            case 0:
                lcd.printString("->",0,2);
                
                pad.tone(750.0,0.3);
                break;
            case 1:
                lcd.printString("->",0,3);
                
                pad.tone(750.0,0.3);
                break;
            case 2:
                lcd.printString("->",0,4);
                
                pad.tone(750.0,0.3);
                break;
        }
        
    }
    if (pointer == 0) {
        main_game(tilt);
    }
    else if (pointer == 1){
        settings();
    }
    else if (pointer == 2){
        how_to_play();
    }
    
    
    return tilt;
}

void settings() {

    lcd.clear();
    lcd.printString("   JOYSTICK ",0,2);    // start with default as joystick
    lcd.printString("   TILT ",0,3);   // choose between joystick and tilt
    lcd.refresh();  
    
    while (pad.check_event(Gamepad::A_PRESSED) || pad.check_event(Gamepad::B_PRESSED) == false) {
        int pointer = 0;
        if (pad.check_event(Gamepad::L_PRESSED)  == N && pointer > 0) {
            pointer -= 1;
        } else if (pad.check_event(Gamepad::R_PRESSED)  == S && pointer < 1) {
            pointer += 1;
        }
        
        switch(pointer){
            case 0:
                lcd.printString("->",0,2);
                
                pad.tone(750.0,0.3);
                
                break;
            case 1:
                lcd.printString("->",0,3);
                
                pad.tone(750.0,0.3);
                break; 
        }
        if (pad.check_event(Gamepad::A_PRESSED) && pointer == 0) {
            tilt = false;
        }
        else if (pad.check_event(Gamepad::A_PRESSED) && pointer == 1) {
            tilt = true;
        }
        else if (pad.check_event(Gamepad::B_PRESSED)) {
            main_menu();
        }
    }
}

void how_to_play() {
    while (pad.check_event(Gamepad::B_PRESSED) == false) {
        lcd.clear();
        lcd.printString("  explain ",2,2);
        lcd.printString(" PRESS B ",1,4);
        lcd.refresh();
    }
    main_menu();
}

void end_screen() {
    lcd.clear();
    lcd.printString("   RESTART? ",2,2);
    lcd.printString(" PRESS START ",1,4);
    lcd.refresh();    
    pad.tone(750.0,0.3);
    wait(0.4);
    
    pad.tone(300.0,0.3);
    wait(0.4);
    
    while (pad.check_event(Gamepad::START_PRESSED) == false) { 
        lcd.clear();
        lcd.printString("   RESTART? ",2,2); //print score here
        lcd.printString(" PRESS START ",1,4);
        lcd.refresh();
    }
    title_screen();
}

void victory_screen() {
    lcd.clear();
    lcd.printString("   VICTORY! ",2,1);
    lcd.printString(" CONT = START ",0,4);  // print score here
    lcd.printString(" MENU = BACK ",0,5);
    lcd.refresh();
    pad.tone(2500.0,0.1);
    wait(0.4);
        
    pad.tone(2500.0,0.1);
    wait(0.2);
        
    pad.tone(4000.0,0.6);
    wait(0.6);
    while (pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED) == false) { 
        lcd.clear();
        
        lcd.printString("   VICTORY! ",2,1);
        lcd.printString(" CONT = START ",0,4);
        lcd.printString(" MENU = BACK ",0,5);

        lcd.refresh();
        
        if (pad.check_event(Gamepad::START_PRESSED)) {
            prev_score = breakout.get_prev_score();
            main_game(tilt); // placeholder, change so game restarts
        }
        else if (pad.check_event(Gamepad::BACK_PRESSED)) {
            title_screen();
        }
    }
}

void title_screen() {
    tilt = false;
    prev_score = 0;
    init(prev_score);     // initialise and then display welcome screen...
    
    Bitmap breakwhite(breakwhite_data, 48, 84);
    Bitmap breakblack(breakblack_data, 48, 84);
    
    while (pad.check_event(Gamepad::L_PRESSED) || pad.check_event(Gamepad::R_PRESSED) || pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED)== false) {  //maybe add a jingle
        
        breakwhite.render(lcd, 0, 0);
        lcd.refresh();
        pad.leds_on();
        wait(0.5);
            
        lcd.clear();
            
        breakblack.render(lcd, 0, 0);
        lcd.refresh();
        pad.leds_off();
        wait(0.5);
    }
     
    if (main_menu() == true) {
        tilt = true;
    }  // waiting for the user to start
    
    main_game(tilt);
    
}

void main_game(bool tilt) {
    int fps = 8;  // frames per second
    i ++; //to init the stuff
    if (i > 1) {
        init(prev_score);
    }
    bool pause = false;
    lcd.setBrightness(1);
    
    Bitmap three(three_data, 48, 84);
    Bitmap two(two_data, 48, 84);
    Bitmap one(one_data, 48, 84);
    
    lcd.clear();
    three.render(lcd, 0, 0);
    lcd.refresh();
    pad.tone(500.0,0.5);
    wait(1);
    
    lcd.clear();
    two.render(lcd, 0, 0);
    lcd.refresh();
    pad.tone(500.0,0.5);
    wait(1);
    
    lcd.clear();
    one.render(lcd, 0, 0);
    lcd.refresh();
    pad.tone(1000.0,1);
    wait(1);
    
    render();  // first draw the initial frame 
    wait(1.0f/fps);  // and wait for one frame period


    // game loop - read input, update the game state and render the display
    while (1) {
        breakout.read_input(pad,tilt);
        breakout.update(pad);
        render();
        if (breakout.check_goal(pad) == true) {
            lcd.setBrightness(0);
            wait(0.1);
            lcd.setBrightness(1);
            wait(0.1);
            lcd.setBrightness(0);
            wait(0.1);
            lcd.setBrightness(1);
            wait(0.1);
            lcd.setBrightness(0);
            wait(0.1);
            lcd.setBrightness(1);
            wait(0.1);
            lcd.setBrightness(0);
            wait(0.1);
            lcd.setBrightness(1);
        }
        if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
            pause = !pause;
        }
        while (pause == true) {
            lcd.clear();
            lcd.printString("    PAUSED ",0,2);
            lcd.refresh();
            if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
                pause = !pause;
            }
            wait(1.0f/fps);
        }
        if (breakout.get_lives() == 0) {
            //if (end_screen() == true) {
            //    main_menu();
            //}
            //else {
            //    main_menu();
            //}
            pad.leds_off(); //turns last led off (doesnt run through and update bored so last led doent turn off via lives leds
            end_screen();
        }
        if (breakout.get_num_left() == 0) {
            victory_screen();
        }
        wait(1.0f/fps);
    }
}