Advertisement
PIBogdanov

sd

Jul 1st, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <thread>
  4. #include <chrono>
  5.  
  6. #ifdef _WIN32 // Windows-specific code
  7.  
  8. #include <conio.h>
  9.  
  10. const int upArrow = 72; // Up arrow key (Windows, because it uses the _getch() function from the <conio.h> header)
  11.  
  12. const int downArrow = 80; // Down arrow key (Windows, because it uses the _getch() function from the <conio.h> header)
  13.  
  14. const int enterKey = 13; // '\r' / Enter key (Windows, because it uses the _getch() function from the <conio.h> header)
  15.  
  16. void ClearScreen()
  17. {
  18. std::system("cls");
  19. }
  20.  
  21. int KeyboardInput()
  22. {
  23. return _getch();
  24. }
  25.  
  26. #else // Unix/Linux/MacOS-specific code
  27.  
  28. #include <termios.h>
  29. #include <unistd.h>
  30.  
  31. const int upArrow = 65; // Up arrow key (Unix/Linux/MacOS)
  32. const int downArrow = 66; // Down arrow key (Unix/Linux/MacOS)
  33. const int enterKey = 10; // '\n' / Enter key (Unix/Linux/MacOS)
  34.  
  35. void ClearScreen()
  36. {
  37. std::cout << "\033[2J\033[1;1H"; // ANSI escape sequence to clear the screen
  38. }
  39.  
  40. int KeyboardInput()
  41. {
  42. struct termios oldSettings, newSettings;
  43. tcgetattr(STDIN_FILENO, &oldSettings);
  44. newSettings = oldSettings;
  45. newSettings.c_lflag &= ~(ICANON | ECHO);
  46. tcsetattr(STDIN_FILENO, TCSANOW, &newSettings);
  47.  
  48. int input = getchar();
  49.  
  50. tcsetattr(STDIN_FILENO, TCSANOW, &oldSettings);
  51.  
  52. return input;
  53. }
  54.  
  55. #endif
  56.  
  57. template <typename T>
  58. void MainMenu(T[], size_t, std::string[], size_t, std::string&, int);
  59.  
  60. void DisplayMenu(std::string[], size_t, std::string&, const size_t&);
  61.  
  62. template <typename T>
  63. int MenuMovement(T[], int, size_t, int);
  64.  
  65. void GameStart(int);
  66.  
  67. void Controls(int);
  68.  
  69. void Credits(int);
  70.  
  71. void ExitGame(int);
  72.  
  73. bool IsKeyPressedToGoBackToThePreviousMenu(int);
  74.  
  75. template <typename T>
  76. void DifficultyMenu(T[], size_t, std::string[], size_t, std::string&, int);
  77.  
  78. void CoutCentered(const std::string&);
  79.  
  80. int main()
  81. {
  82. typedef void (*menuOptions)(int);
  83.  
  84. menuOptions mainMenuOptions[] = { GameStart, Controls, Credits, ExitGame };
  85.  
  86. size_t sizeOfTheArray = sizeof(mainMenuOptions) / sizeof(mainMenuOptions[0]);
  87.  
  88. std::string mainMenu[] = { "Start Game", "Controls", "Credits", "Exit" };
  89.  
  90. size_t sizeOfTheStringMenu = std::size(mainMenu);
  91.  
  92. std::string text = "Main Menu";
  93.  
  94. MainMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, 0);
  95.  
  96. return 0;
  97. }
  98.  
  99. template <typename T>
  100. void MainMenu(T mainMenuOptions[], size_t sizeOfTheArray, std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
  101. {
  102. int key = 0;
  103. int previousSelectedOption = 0;
  104.  
  105. while (true)
  106. {
  107. DisplayMenu(mainMenu, sizeOfTheStringMenu, text, selectedOption);
  108.  
  109. previousSelectedOption = selectedOption;
  110.  
  111. selectedOption = MenuMovement(mainMenu, selectedOption, sizeOfTheStringMenu, key);
  112.  
  113. if (IsKeyPressedToGoBackToThePreviousMenu(key))
  114. {
  115. if (text == "Main Menu")
  116. {
  117. return;
  118. }
  119. else if (text == "Difficulty")
  120. {
  121. DifficultyMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, previousSelectedOption);
  122. }
  123. }
  124.  
  125. if (key == enterKey)
  126. {
  127. mainMenuOptions[selectedOption](selectedOption);
  128. }
  129. }
  130. }
  131.  
  132. void DisplayMenu(std::string menu[], size_t sizeOfTheStringMenu, std::string& text, const size_t& selectedOption)
  133. {
  134. ClearScreen();
  135.  
  136. CoutCentered(text);
  137.  
  138. for (size_t i = 0; i < sizeOfTheStringMenu; ++i)
  139. {
  140. if (i == selectedOption)
  141. {
  142. std::cout << ">> ";
  143. }
  144. else
  145. {
  146. std::cout << " ";
  147. }
  148.  
  149. std::cout << menu[i] << std::endl;
  150. }
  151. }
  152.  
  153. template <typename T>
  154. int MenuMovement(T mainMenu[], int selectedOption, size_t sizeOfTheStringMenu, int key)
  155. {
  156. key = KeyboardInput();
  157.  
  158. if (key == upArrow)
  159. {
  160. --selectedOption;
  161.  
  162. if (selectedOption == -1)
  163. {
  164. selectedOption = sizeOfTheStringMenu - 1;
  165. }
  166. }
  167. else if (key == downArrow)
  168. {
  169. ++selectedOption;
  170.  
  171. if (selectedOption == static_cast<int>(sizeOfTheStringMenu))
  172. {
  173. selectedOption = 0;
  174. }
  175. }
  176.  
  177. return selectedOption;
  178. }
  179.  
  180. void GameStart(int selectedOption)
  181. {
  182. std::cout << "Starting the game..." << std::endl;
  183. std::this_thread::sleep_for(std::chrono::seconds(2));
  184. }
  185.  
  186. void Controls(int selectedOption)
  187. {
  188. std::cout << "Displaying controls..." << std::endl;
  189. std::this_thread::sleep_for(std::chrono::seconds(2));
  190. }
  191.  
  192. void Credits(int selectedOption)
  193. {
  194. std::cout << "Displaying credits..." << std::endl;
  195. std::this_thread::sleep_for(std::chrono::seconds(2));
  196. }
  197.  
  198. void ExitGame(int selectedOption)
  199. {
  200. std::cout << "Exiting the game..." << std::endl;
  201. std::this_thread::sleep_for(std::chrono::seconds(2));
  202. }
  203.  
  204. bool IsKeyPressedToGoBackToThePreviousMenu(int key)
  205. {
  206. return key == 'b' || key == 'B';
  207. }
  208.  
  209. template <typename T>
  210. void DifficultyMenu(T mainMenuOptions[], size_t sizeOfTheArray, std::string mainMenu[], size_t sizeOfTheStringMenu, std::string& text, int selectedOption)
  211. {
  212. int key = 0;
  213. int previousSelectedOption = 0;
  214.  
  215. while (true)
  216. {
  217. DisplayMenu(mainMenu, sizeOfTheStringMenu, text, selectedOption);
  218.  
  219. previousSelectedOption = selectedOption;
  220.  
  221. selectedOption = MenuMovement(mainMenu, selectedOption, sizeOfTheStringMenu, key);
  222.  
  223. if (IsKeyPressedToGoBackToThePreviousMenu(key))
  224. {
  225. if (text == "Difficulty")
  226. {
  227. MainMenu(mainMenuOptions, sizeOfTheArray, mainMenu, sizeOfTheStringMenu, text, previousSelectedOption);
  228. }
  229. }
  230.  
  231. if (key == enterKey)
  232. {
  233. mainMenuOptions[selectedOption](selectedOption);
  234. }
  235. }
  236. }
  237.  
  238. void CoutCentered(const std::string& str)
  239. {
  240. int width = 80;
  241. int spaces = (width - str.length()) / 2;
  242.  
  243. std::cout << std::string(spaces, ' ') << str << std::endl;
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement