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-04
- Revision:
- 1:21b7a5edb9c2
- Parent:
- 0:b32f6570a26d
- Child:
- 2:917211a4551b
File content as of revision 1:21b7a5edb9c2:
/* @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::main(N5110 &lcd, Gamepad &pad) { 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)) { lcd.clear(); lcd.printString("Playing game", 0, 2); lcd.refresh(); wait(2); // play(); // 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); } } 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::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)) { // maze.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)) { // 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(">Transparent", 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("Transparent", 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(" Transparent", 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; } } }