Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <thread>
- #include <chrono>
- #ifdef _WIN32 // Windows-specific code
- #include <conio.h>
- const int upArrow = 72; // Up arrow key (Windows, because it uses the _getch() function from the <conio.h> header)
- const int downArrow = 80; // Down arrow key (Windows, because it uses the _getch() function from the <conio.h> header)
- const int enterKey = 13; // '\r' / Enter key (Windows, because it uses the _getch() function from the <conio.h> header)
- void ClearScreen()
- {
- std::system("cls");
- }
- int KeyboardInput()
- {
- return _getch();
- }
- #else // Unix/Linux/MacOS-specific code
- #include <termios.h>
- #include <unistd.h>
- const int upArrow = 65; // Up arrow key (Unix/Linux/MacOS)
- const int downArrow = 66; // Down arrow key (Unix/Linux/MacOS)
- const int enterKey = 10; // '\n' / Enter key (Unix/Linux/MacOS)
- void ClearScreen()
- {
- std::cout << "\033[2J\033[1;1H"; // ANSI escape sequence to clear the screen
- }
- int KeyboardInput()
- {
- struct termios oldSettings, newSettings;
- tcgetattr(STDIN_FILENO, &oldSettings);
- newSettings = oldSettings;
- newSettings.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newSettings);
- int input = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldSettings);
- return input;
- }
- #endif
- template <typename T>
- void MainMenu(T[], size_t, std::string[], size_t, std::string&, int);
- void DisplayMenu(std::string[], size_t, std::string&, const size_t&);
- template <typename T>
- int MenuMovement(T[], int, size_t, int);
- void GameStart(int);
- void Controls(int);
- void Credits(int);
- void ExitGame(int);
- bool IsKeyPressedToGoBackToThePreviousMenu(int);
- template <typename T>
- void DifficultyMenu(T[], size_t, std::string[], size_t, std::string&, int);
- void CoutCentered(const std::string&);
- int main()
- {
- typedef void (*menuOptions)(int);
- menuOptions mainMenuOptions[] = { GameStart, Controls, Credits, ExitGame };
- size_t sizeOfTheArray = sizeof(mainMenuOptions) / sizeof(mainMenuOptions[0]);
- std::string mainMenu[] = { "Start Game", "Controls", "Credits", "Exit" };
- size_t sizeOfTheStringMenu = std::size(mainMenu);
- std::string text = "Main Menu";
- MainMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, 0);
- return 0;
- }
- template <typename T>
- void MainMenu(T mainMenuOptions[], size_t sizeOfTheArray, std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
- {
- int key = 0;
- int previousSelectedOption = 0;
- while (true)
- {
- DisplayMenu(mainMenu, sizeOfTheStringMenu, text, selectedOption);
- previousSelectedOption = selectedOption;
- selectedOption = MenuMovement(mainMenu, selectedOption, sizeOfTheStringMenu, key);
- if (IsKeyPressedToGoBackToThePreviousMenu(key))
- {
- if (text == "Main Menu")
- {
- return;
- }
- else if (text == "Difficulty")
- {
- DifficultyMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, previousSelectedOption);
- }
- }
- if (key == enterKey)
- {
- mainMenuOptions[selectedOption](selectedOption);
- }
- }
- }
- void DisplayMenu(std::string menu[], size_t sizeOfTheStringMenu, std::string& text, const size_t& selectedOption)
- {
- ClearScreen();
- CoutCentered(text);
- for (size_t i = 0; i < sizeOfTheStringMenu; ++i)
- {
- if (i == selectedOption)
- {
- std::cout << ">> ";
- }
- else
- {
- std::cout << " ";
- }
- std::cout << menu[i] << std::endl;
- }
- }
- template <typename T>
- int MenuMovement(T mainMenu[], int selectedOption, size_t sizeOfTheStringMenu, int key)
- {
- key = KeyboardInput();
- if (key == upArrow)
- {
- --selectedOption;
- if (selectedOption == -1)
- {
- selectedOption = sizeOfTheStringMenu - 1;
- }
- }
- else if (key == downArrow)
- {
- ++selectedOption;
- if (selectedOption == static_cast<int>(sizeOfTheStringMenu))
- {
- selectedOption = 0;
- }
- }
- return selectedOption;
- }
- void GameStart(int selectedOption)
- {
- std::cout << "Starting the game..." << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(2));
- }
- void Controls(int selectedOption)
- {
- std::cout << "Displaying controls..." << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(2));
- }
- void Credits(int selectedOption)
- {
- std::cout << "Displaying credits..." << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(2));
- }
- void ExitGame(int selectedOption)
- {
- std::cout << "Exiting the game..." << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(2));
- }
- bool IsKeyPressedToGoBackToThePreviousMenu(int key)
- {
- return key == 'b' || key == 'B';
- }
- template <typename T>
- void DifficultyMenu(T mainMenuOptions[], size_t sizeOfTheArray, std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
- {
- int key = 0;
- int previousSelectedOption = 0;
- while (true)
- {
- DisplayMenu(mainMenu, sizeOfTheStringMenu, text, selectedOption);
- previousSelectedOption = selectedOption;
- selectedOption = MenuMovement(mainMenu, selectedOption, sizeOfTheStringMenu, key);
- if (IsKeyPressedToGoBackToThePreviousMenu(key))
- {
- if (text == "Difficulty")
- {
- MainMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, previousSelectedOption);
- }
- }
- if (key == enterKey)
- {
- mainMenuOptions[selectedOption](selectedOption);
- }
- }
- }
- void CoutCentered(const std::string& str)
- {
- int width = 80;
- int spaces = (width - str.length()) / 2;
- std::cout << std::string(spaces, ' ') << str << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement