Menu class used as basis for program; running the game and allowing the user to change the style and difficulty of the game via an interface.

Menu.cpp

Committer:
el15mh
Date:
2017-04-07
Revision:
2:917211a4551b
Parent:
1:21b7a5edb9c2
Child:
3:a79daa7c2b55

File content as of revision 2:917211a4551b:

/*
 
 @file Menu.cpp
 
 (c) Max Houghton 02.14.17
 Roller Maze Project, ELEC2645, Univeristy of Leeds
 
 */

#include "Menu.h"

// constructor function used when object is initialised
Menu::Menu()
{
    
}

// destructor function
Menu::~Menu()
{
    
}

void Menu::init(int mazeIndex)
{
    _mazeIndex = mazeIndex;
    
    _engine.init(_mazeIndex, 4, 4, 4);
    
    /*
    if ((_mazeIndex == 0) ||
        (_mazeIndex == 1)) {
        
        _engine.init(_mazeIndex, 4, 4, 4);
    }
    
    else {
        _engine.init(_mazeIndex, 2, 2, 2);
    }
     */
}

/// MAIN MENU METHOD ///

void Menu::main(N5110 &lcd, Gamepad &pad, FXOS8700CQ &device)
{
    int selected = 0;
    
    while(1) {
        
        char d = pad.get_direction();
        
        if ((d == NW) ||
            (d == N)  ||
            (d == NE)){
            
            selected -= 1;
        }
        
        if ((d == SW) ||
            (d == S)  ||
            (d == SE)){
            
            selected += 1;
        }
        
        // lcd.printString("Testing", 0, 2);
        switch (selected) {
                
            case 1:
                
                lcd.clear();
                lcd.printString(">Play game", 0, 0);
                lcd.printString(" Game options", 0, 1);
                lcd.printString(" LCD settings", 0, 2);
                lcd.printString(" Sound", 0, 3);
                
                lcd.refresh();
                
                // either clicking joystick or pressing A selects function
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    playGame(_mazeIndex, lcd, pad, device); // call the game function
                }
                
                wait_ms(250);    // 250ms propogation delay
                
                break;
                
            case 2:
                
                lcd.clear();
                lcd.printString(" Play game", 0, 0);
                lcd.printString(">Game options", 0, 1);
                lcd.printString(" LCD settings", 0, 2);
                lcd.printString(" Sound", 0, 3);
                
                lcd.refresh();
                
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    options(lcd, pad);
                }
                
                wait_ms(250);    // 250ms propogation delay
                
                break;
                
            case 3:
                
                lcd.clear();
                lcd.printString(" Play game", 0, 0);
                lcd.printString(" Game options", 0, 1);
                lcd.printString(">LCD settings", 0, 2);
                lcd.printString(" Sound", 0, 3);
                
                lcd.refresh();
                
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    lcdSettings(lcd, pad);
                }
                
                wait_ms(250);    // 250ms propogation delay
                
                break;
                
            case 4:
                
                lcd.clear();
                lcd.printString(" Play game", 0, 0);
                lcd.printString(" Game options", 0, 1);
                lcd.printString(" LCD settings", 0, 2);
                lcd.printString(">Sound", 0, 3);
                
                lcd.refresh();
                
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    soundSettings(lcd, pad);
                }
                
                wait_ms(250);    // 250ms propogation delay
                
                break;
                
            default:
                
                selected = 1;
                
                break;
        }
        
        // printf("Joystick position = %c \n", d);
        // printf("Selected = %i \n", selected);
        
    }
}

/// INTRO LOOP ///

void intro(N5110 &lcd, Gamepad &pad)
{
    
    // printf("intro started \n");
    while (pad.check_event(Gamepad::START_PRESSED) == false){
        
        // animation sequence
        // lcd.drawCircle(x, y, radius, type);
        
        // DRAW FIRST SEQUENCE OF CIRLCES MOVING ACROSS LCD //
        for (int i = 0; i < 61; i++){
            
            lcd.clear();
            lcd.printString("  Welcome to  ", 0, 1);
            lcd.printString("  Roller Maze ", 0, 2);
            // printf("loop \n");
            lcd.drawLine(56, 40, 68, 40, FILL_BLACK);
            lcd.drawLine(56, 41, 68, 41, FILL_BLACK);
            lcd.refresh();
            
            if (i < 57){
                lcd.drawCircle(i, 36, 4, FILL_BLACK);
                lcd.refresh();
                wait_ms(50);
                
            }
            
            else if (i == 57){
                
                lcd.clear();
                lcd.printString("  Welcome to  ", 0, 1);
                lcd.printString("  Roller Maze ", 0, 2);
                // printf("loop \n");
                lcd.drawLine(56, 40, 68, 40, FILL_BLACK);
                lcd.drawLine(56, 41, 68, 41, FILL_BLACK);
                lcd.drawCircle(i, 37, 4, FILL_BLACK);
                
                wait_ms(25);
                
                for (int c = 55; c < 68; c++){
                    for (int d = 42; d < 47; d++){
                        
                        lcd.clearPixel(c, 42);
                        lcd.clearPixel(c, 43);
                        lcd.clearPixel(c, 44);
                        lcd.clearPixel(c, 45);
                        lcd.clearPixel(c, 46);
                        lcd.clearPixel(c, 47);
                        lcd.refresh();
                        
                    }
                }
                
                lcd.refresh();
            }
            else if (i == 58){
                
                lcd.clear();
                lcd.printString("  Welcome to  ", 0, 1);
                lcd.printString("  Roller Maze ", 0, 2);
                // printf("loop \n");
                lcd.drawLine(56, 40, 68, 40, FILL_BLACK);
                lcd.drawLine(56, 41, 68, 41, FILL_BLACK);
                lcd.drawCircle(i, 38, 4, FILL_BLACK);
                
                wait_ms(25);
                
                for (int c = 55; c < 68; c++){
                    for (int d = 42; d < 47; d++){
                        
                        lcd.clearPixel(c, 42);
                        lcd.clearPixel(c, 43);
                        lcd.clearPixel(c, 44);
                        lcd.clearPixel(c, 45);
                        lcd.clearPixel(c, 46);
                        lcd.clearPixel(c, 47);
                        lcd.refresh();
                        
                    }
                }
                
                lcd.refresh();
                
            }
            else if (i == 59){
                
                lcd.clear();
                lcd.printString("  Welcome to  ", 0, 1);
                lcd.printString("  Roller Maze ", 0, 2);
                // printf("loop \n");
                lcd.drawLine(56, 40, 68, 40, FILL_BLACK);
                lcd.drawLine(56, 41, 68, 41, FILL_BLACK);
                lcd.drawCircle(i, 39, 4, FILL_BLACK);
                
                wait_ms(25);
                
                for (int c = 55; c < 68; c++){
                    for (int d = 42; d < 47; d++){
                        
                        lcd.clearPixel(c, 42);
                        lcd.clearPixel(c, 43);
                        lcd.clearPixel(c, 44);
                        lcd.clearPixel(c, 45);
                        lcd.clearPixel(c, 46);
                        lcd.clearPixel(c, 47);
                        lcd.refresh();
                        
                    }
                }
                
                lcd.refresh();
                
            }
            else {
                
                for (int j = 40; j < 48; j++){
                    
                    lcd.clear();
                    lcd.printString("  Welcome to  ", 0, 1);
                    lcd.printString("  Roller Maze ", 0, 2);
                    // printf("loop \n");
                    lcd.drawLine(56, 40, 68, 40, FILL_BLACK);
                    lcd.drawLine(56, 41, 68, 41, FILL_BLACK);
                    lcd.drawCircle(i, j, 4, FILL_BLACK);
                    
                    wait_ms(125);
                    
                    for (int c = 55; c < 68; c++){
                        for (int d = 42; d < 47; d++){
                            
                            lcd.clearPixel(c, 42);
                            lcd.clearPixel(c, 43);
                            lcd.clearPixel(c, 44);
                            lcd.clearPixel(c, 45);
                            lcd.clearPixel(c, 46);
                            lcd.clearPixel(c, 47);
                        }
                    }
                    
                }
                
                lcd.refresh();
            }
            
            // printf("%i \n", i);
        }
    }
    
    
}

/// GAME LOOP ///
void playGame(N5110 &lcd, Gamepad &pad, FXOS8700CQ &device)
{
    int exit = 0;
    
    while (exit == 0){
        
        _engine.readInput(pad, device);
        _engine.update(pad);
        // rendering screen
        lcd.clear();
        _engine.draw(lcd);
        lcd.refresh();
        
        wait_ms(100);
        
        // get direction of the joystick
        char d = pad.get_direction();
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
    }
}

/// MENU FUNCTIONS ///

void Menu::options(N5110 &lcd, Gamepad &pad)
{
    int exit = 0;
    int selected = 0;
    
    while(exit == 0) {
        
        // get direction of the joystick
        char d = pad.get_direction();
        
        if ((d == NW) ||
            (d == N)  ||
            (d == NE)){
            
            selected -= 1;
        }
        
        if ((d == SW) ||
            (d == S)  ||
            (d == SE)){
            
            selected += 1;
        }
        
        // printf("While loop 1 \n");
        // printf("Selected = %i \n", selected);
        switch (selected) {
                
            case 1:
                
                lcd.clear();
                // displays options page with indicator on first
                lcd.printString("Game Options:", 0, 0);
                lcd.printString(">Difficulty", 0, 2);
                lcd.printString(" Ball Colour", 0, 3);
                lcd.refresh();
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    difficultyOptions(lcd, pad);
                }
                
                wait_ms(250);
                
                break;
                
            case 2:
                
                lcd.clear();
                // displays options page with indicator on second
                lcd.printString("Game Options:", 0, 0);
                lcd.printString(" Difficulty", 0, 2);
                lcd.printString(">Ball Colour", 0, 3);
                lcd.refresh();
                
                // if second option selected
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    lcdColourOptions(lcd, pad);
                }
                
                wait_ms(250);
                
                break;
                
            default:
                
                selected = 1;
                
                break;
        }
        
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
    }
}

void Menu::lcdSettings(N5110 &lcd, Gamepad &pad)
{
    int exit = 0;
    int selected = 0;
    
    while(exit == 0) {
        
        char d = pad.get_direction();
        
        if ((d == NW) ||
            (d == N)  ||
            (d == NE)){
            
            selected -= 1;
        }
        
        if ((d == SW) ||
            (d == S)  ||
            (d == SE)){
            
            selected += 1;
        }
        
        // printf("While loop 1 \n");
        // printf("Selected = %i \n", selected);
        switch (selected) {
                
            case 1:
                
                lcd.clear();
                // displays options page with indicator on first
                lcd.printString("LCD Settings:", 0, 0);
                lcd.printString(">Brightness", 0, 2);
                lcd.printString(" Invert Colour", 0, 3);
                lcd.refresh();
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    
                    lcdBackgroundColour(lcd, pad);
                }
                
                wait_ms(250);
                
                break;
                
            case 2:
                
                lcd.clear();
                // displays options page with indicator on second
                lcd.printString("LCD Settings:", 0, 0);
                lcd.printString(" Brightness", 0, 2);
                lcd.printString(">Invert Colour", 0, 3);
                lcd.refresh();
                
                
                // if second option selected
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    
                    lcdInverseColour(lcd, pad);
                }
                
                wait_ms(250);
                
                break;
                
            default:
                
                selected = 1;
                
                break;
        }
        
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
        
    }
    
    
}

void Menu::soundSettings(N5110 &lcd, Gamepad &pad)
{
    
    int exit = 0;
    // pad.tone(750.0,0.1);
    
    while (exit == 0){
        
        lcd.clear();
        lcd.printString("Sound settings", 0, 2);
        lcd.refresh();
        
        char d = pad.get_direction();
        
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
    }
    
}

void Menu::controlOptions(N5110 &lcd, Gamepad &pad)
{
    int selected = 0;
    int exit = 0;
    
    while (exit == 0){
        char d = pad.get_direction();
        
        if (d == W){
            
            selected -= 1;
        }
        
        if (d == E){
            
            selected += 1;
        }
        
        switch(selected){
                
            case 1:
                
                while (selected == 0){
                    
                    lcd.clear();
                    lcd.printString("Control: ", 0, 0);
                    lcd.printString("   Joystick", 0, 2);
                    
                    lcd.refresh();
                    wait_ms(250);   // 250ms propogation delay
                    
                    if (pad.check_event(Gamepad::A_PRESSED)||
                        pad.check_event(Gamepad::JOY_PRESSED)){
                        
                        CONTROL = true;
                    }
                    
                    break;
                    
                }
                
                
            case 2:
                
                lcd.clear();
                lcd.printString("Difficulty: ", 0, 0);
                lcd.printString(" Easy", 0, 2);
                lcd.printString(">Hard", 0, 3);
                lcd.refresh();
                
                wait_ms(250);   // 250ms propogation delay
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    
                    // maze.mazeIndex = 1;
                    lcd.clear();
                    lcd.printString("Hard mode", 0, 1);
                    lcd.refresh();
                    wait(2);
                    
                    exit = 1;
                }
                
                break;
                
            default:
                
                selected = 1;
                
                break;
                
        }
        
        if (pad.check_event(Gamepad::BACK_PRESSED)){
            
            exit = 1;
        }
        
    }
}


void Menu::difficultyOptions(N5110 &lcd, Gamepad &pad)
{
    int selected = 0;
    int exit = 0;
    
    while(exit == 0){
        
        char d = pad.get_direction();
        
        if ((d == NW) ||
            (d == N)  ||
            (d == NE)){
            
            selected -= 1;
        }
        
        if ((d == SW) ||
            (d == S)  ||
            (d == SE)){
            
            selected += 1;
        }
        
        switch(selected){
                
            case 1:
                
                lcd.clear();
                lcd.printString("Difficulty: ", 0, 0);
                lcd.printString(">Easy", 0, 2);
                lcd.printString(" Hard", 0, 3);
                lcd.refresh();
                
                wait_ms(250);   // 250ms propogation delay
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    _mazeIndex = 0;
                    
                    lcd.clear();
                    lcd.printString("Easy mode", 0, 1);
                    lcd.refresh();
                    wait(2);
                    
                    exit = 1;
                }
                
                break;
                
            case 2:
                
                lcd.clear();
                lcd.printString("Difficulty: ", 0, 0);
                lcd.printString(" Easy", 0, 2);
                lcd.printString(">Hard", 0, 3);
                lcd.refresh();
                
                wait_ms(250);   // 250ms propogation delay
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    _mazeIndex = 1;
                    
                    // maze.mazeIndex = 1;
                    lcd.clear();
                    lcd.printString("Hard mode", 0, 1);
                    lcd.refresh();
                    wait(2);
                    
                    exit = 1;
                }
                
                break;
                
            default:
                
                selected = 1;
                
                break;
                
        }
        
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
        
    }
    
    
}

void Menu::lcdColourOptions(N5110 &lcd, Gamepad &pad)
{
    int selected = 0;
    int exit = 0;
    
    while(exit == 0){
        
        char d = pad.get_direction();
        
        if ((d == NW) ||
            (d == N)  ||
            (d == NE)){
            
            selected -= 1;
        }
        
        if ((d == SW) ||
            (d == S)  ||
            (d == SE)){
            
            selected += 1;
        }
        
        switch(selected){
                
            case 1:
                
                // printf("case 2.1");
                
                lcd.clear();
                lcd.printString("Ball colour: ", 0, 0);
                lcd.printString(">Outline", 0, 2);
                lcd.printString(" Solid", 0, 3);
                lcd.refresh();
                
                wait_ms(250);   // 250ms propogation delay
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    // ball.ballColour = 0;
                    lcd.clear();
                    lcd.printString("Outline", 0, 1);
                    lcd.refresh();
                    wait(2);
                    
                    exit = 1;
                }
                
                break;
                
            case 2:
                
                // printf("case 2.2");
                
                lcd.clear();
                lcd.printString("Ball colour: ", 0, 0);
                lcd.printString(" Outline", 0, 2);
                lcd.printString(">Solid", 0, 3);
                lcd.refresh();
                
                wait_ms(250);   // 250ms propogation delay
                
                
                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                      pad.check_event(Gamepad::JOY_PRESSED))  ||
                     (d == E)) {
                    
                    // ball.ballColour = 1;
                    lcd.clear();
                    lcd.printString("Solid", 0, 1);
                    lcd.refresh();
                    wait(2);
                    
                    exit = 1;
                }
                
                break;
                
            default:
                
                selected = 1;
                
                break;
        }
        
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
    }
    
    
}

void Menu::lcdInverseColour(N5110 &lcd, Gamepad &pad)
{
    int exit = 0;
    
    while (exit == 0) {
        
        lcd.clear();
        lcd.printString("Press A to", 0, 0);
        lcd.printString("invert colours", 0, 1);
        lcd.printString("Press B to", 0, 3);
        lcd.printString("revert to", 0, 4);
        lcd.printString("normal", 0, 5);
        
        if (pad.check_event(Gamepad::A_PRESSED)){
            
            lcd.inverseMode();
            
        }
        
        if (pad.check_event(Gamepad::B_PRESSED)){
            
            lcd.normalMode();
            
        }
        
        lcd.refresh();
        
        char d = pad.get_direction();
        
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
    }
    
    
}

void Menu::lcdBackgroundColour(N5110 &lcd, Gamepad &pad)
{
    // method to change the brightness of the LED backlight
    int exit = 0;
    
    while (exit == 0) {
        
        lcd.clear();
        lcd.printString("BRIGHTNESS", 0, 0);
        lcd.printString("Use DIAL --->", 0, 1);
        lcd.printString("to adjust", 0, 2);
        
        double brightness = pad.read_pot(); // returns value between 0.0 - 1.0
        
        lcd.setBrightness(brightness);
        lcd.refresh();
        
        int width = brightness * 40;
        
        lcd.drawRect(10, 30, 41, 8, FILL_TRANSPARENT);
        lcd.drawRect(11, 31, width, 6, FILL_BLACK);
        lcd.refresh();
        
        wait_ms(10);
        
        char d = pad.get_direction();
        
        if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
            (d == W)){
            
            exit = 1;
        }
        
    }
}

void Menu::animateJoystick(N5110 &lcd)
{
    drawVerticalJoystick(lcd);
    
}


void Menu::drawVerticalJoystick(N5110 &lcd)
{
    /* Drawing a vertical joystick image
     
     33             ####
     34         ############
     35       ################
     36              ##
     37              ##
     38           ########
     39       ################
     40    ######################
     41   ########################
     42  ##########################
     43  ##########################
     44  23456789012345678901234567
     3       4         5
     
     */
    
    for (int i = 32; i < 58; i++){
        lcd.setPixel(i, 43);
        lcd.setPixel(i, 42);
    }
    for (int i = 33; i < 57; i++){
        lcd.setPixel(i, 41);
    }
    for (int i = 34; i < 56; i++){
        lcd.setPixel(i, 40);
    }
    for (int i = 37; i < 54; i++){
        lcd.setPixel(i, 39);
    }
    for (int i = 41; i < 49; i++){
        lcd.setPixel(i, 38);
    }
    
    lcd.setPixel(44, 37);
    lcd.setPixel(45, 37);
    lcd.setPixel(44, 36);
    lcd.setPixel(45, 36);
    
    for (int i = 37; i < 54; i++){
        lcd.setPixel(i, 35);
    }
    for (int i = 39; i < 52; i++){
        lcd.setPixel(i, 34);
    }
    for (int i = 43; i < 47; i++){
        lcd.setPixel(i, 33);
    }
}