ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18ac

Dependencies:   mbed

Menu/Menu.cpp

Committer:
ale_carb0ni
Date:
2020-05-26
Revision:
2:7fa08670b1fc
Child:
4:17d5b53b8815

File content as of revision 2:7fa08670b1fc:

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

int snake[28][26] {                                   //design of snake on initial page
    { 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 },
    { 0,0,0,0,1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1 },
    { 0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1 },
    { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1 },
    { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,1 },
    { 0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,0,0,1 },
    { 0,1,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,1,0 },
    { 1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0 },
    { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0 },
    { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0 },
    { 1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0 },
    { 0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0 },
    { 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0 },
    { 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0 },
    { 0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0 },
    { 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0 },
};

int skull[23][23] {                                   //design of skull in the help page
    { 0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0 },
    { 0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0 },
    { 1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1 },
    { 1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1 },
    { 1,1,1,0,0,1,1,0,0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,0,0,1,1,0 },
    { 0,0,0,0,1,0,0 },
};


Menu::Menu(N5110 &lcd,Gamepad &pad)
{
    _lcd = lcd;
    _pad = pad;
};

void Menu::initscreen()
{
    while( _pad.start_held() == false) {
        _lcd.clear();
        _lcd.drawSprite(29,1,28,26,(int*)snake);             //initial page
        _lcd.printString("SNAKE",WIDTH/2-15,4);
        _lcd.printString("Press Start",WIDTH/2-30,5);
        _lcd.setContrast( _pad.read_pot1());
        _lcd.refresh();
        wait(1/6);
    }
    _lcd.clear();
}

void Menu::menu_screen()
{
    while(_pad.A_held() == false) {                           //unitl A is pressed, the code
        arrow();                                                //stays on the meun,
        _lcd.refresh();                                          //executing only the code of the arrow
        wait(1/6);
    }

    /*if (_lcd.getPixel(20,27)) {                                  //if A is pressed and the arrow
        movement();                                             //is corresponding to PLAY,
        draw();                                                 //the code executes game, movement, draw functions
        _lcd.refresh();
        wait(1/6);
    }*/
    if (_lcd.getPixel(20,35)) {                                 //if A is pressed and the arrow
        help();                                                 //is corresponding to HELP,
        _lcd.refresh();                                         //the code executes help function
        wait(1/6);
    }
    _lcd.refresh();
}

void Menu::arrow()
{
    _lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);            //main menu design
    _lcd.printString("MENU",WIDTH/2-15,1);
    _lcd.printString("Play",WIDTH/2-13,3);
    _lcd.printString("Help",WIDTH/2-13,4);
    //_lcd.drawSprite(WIDTH/2-26,25,5,7,(int*)selector);
    _lcd.refresh();
    wait(1/6);

    if ( _pad.X_held() == true) {                             //position of arrow in the meun
        _lcd.clear();                                            //according to what button is pressed
        _lcd.drawSprite(WIDTH/2-26,25,5,7,(int*)selector);
        _lcd.refresh();
        wait(1/6);
    }

    if ( _pad.B_held() == true) {                             //position of arrow in the meun
        _lcd.clear();                                            //according to what button is pressed
        _lcd.drawSprite(WIDTH/2-26,33,5,7,(int*)selector);
        _lcd.refresh();
        wait(1/6);
    }
}

void Menu::help()
{
    while(1) {
        _lcd.clear();                                        //help instructions, page 1
        _lcd.printString("Use the",21,0);
        _lcd.printString("buttons",21,1);
        _lcd.printString("to move",21,2);
        _lcd.printString("(press B)",15,5);
        _lcd.refresh();
        wait(1/6);
        if (_pad.B_held()) {
            break;
        }
    }
    wait(0.2);

    while(1) {
        _lcd.clear();                                        //help instructions, page 2
        _lcd.printString("Eat the fruit",5,0);
        _lcd.printString("to score",15,1);
        _lcd.printString("(press B)",16,5);
        _lcd.refresh();
        wait(1/6);

        if (_pad.B_held()) {
            break;
        }
    }
    wait(0.2);

    while(1) {
        _lcd.clear();                                        //help instructions, page 3
        _lcd.printString("If you hit",13,0);
        _lcd.printString("a wall",23,1);
        _lcd.printString("or your tail",5,2);
        _lcd.printString("...",WIDTH/2-7,3);
        _lcd.printString("(press B)",16,5);
        _lcd.refresh();
        wait(1/6);

        if (_pad.B_held()) {
            break;
        }
    }
    wait(0.2);

    while(1) {
        _lcd.clear();                                        //help instructions, final page
        _lcd.printString("YOU DIE!",20,1);
        _lcd.drawSprite(30,16,23,23,(int*)skull);
        _lcd.refresh();
        wait(1/6);

        if (_pad.B_held()) {
            break;
        }
    }
    wait(1/6);
    _lcd.clear();
    menu_screen();                                                 //after help instructions, returns to main menu
    _lcd.refresh();
    wait(1/6);
}