Advertisement
TwinFrame

Game_CollectBerry_PlusLoadMap

Dec 9th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Clight_19_Game_MapControlPlayer
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. bool isOpenMainMenu = true;
  11. bool readyMap = false;
  12.  
  13. int maxLine = 30;
  14. int maxRow = 10;
  15. int numLine = 5;
  16. int numRow = 3;
  17. char[,] map = new char[numRow, numLine];
  18. char[,] mapSave = new char[numRow, numLine];
  19.  
  20. char berry = '%';
  21. char player = '0';
  22. char wall = '#';
  23.  
  24. while (isOpenMainMenu == true)
  25. {
  26. Console.CursorVisible = true;
  27. Console.WriteLine("Меню игры:\n");
  28. Console.WriteLine("F1 - Играть");
  29. Console.WriteLine("F2 - Нарисовать карту");
  30. Console.WriteLine("F3 - Загрузить карту");
  31. Console.WriteLine("F5 - Выход");
  32.  
  33. ConsoleKeyInfo key = Console.ReadKey(true);
  34. switch (key.Key)
  35. {
  36. case ConsoleKey.F1:
  37. bool isPlaying = true;
  38. if (readyMap == false)
  39. {
  40. Console.SetCursorPosition(0, 6);
  41. Console.Write("Сначала создайте карту. Нажмите F1.");
  42. Console.ReadKey(true);
  43. Console.SetCursorPosition(0, 6);
  44. Console.WriteLine(" ");
  45. break;
  46. }
  47. Console.Clear();
  48.  
  49. int xPlayer = 1;
  50. int yPlayer = 1;
  51. int numBerry = 0;
  52. int collectBerry = 0;
  53.  
  54. map = CopyMap(mapSave);
  55. numBerry = FindSumSymbol(map, berry);
  56. PrintMap(numRow, numLine, map);
  57. DeleteSymbol(map, player, ref xPlayer, ref yPlayer); //удаляется символ игрока с карты и считываются координаты
  58.  
  59. while (isPlaying == true)
  60. {
  61. Console.CursorVisible = false;
  62. Console.SetCursorPosition(0, numRow + 2);
  63. Console.WriteLine($"Собрано ягод: {collectBerry}/{numBerry}");
  64. Console.WriteLine("\nF5 - выход");
  65. Console.SetCursorPosition(xPlayer, yPlayer);
  66.  
  67. ConsoleKeyInfo keyPlay = Console.ReadKey(true);
  68. NavigationMenu(keyPlay, player, berry, wall, numRow, numBerry, ref collectBerry, ref isPlaying, ref xPlayer, ref yPlayer, ref map);
  69. }
  70. break;
  71.  
  72. case ConsoleKey.F2:
  73. Console.Write($"Длина карты в символах (не больше {maxLine}): ");
  74. string currentLenghtLine = Console.ReadLine();
  75. CheckUserInput(currentLenghtLine, maxLine, ref numLine);
  76. Console.Write($"Высота карты в символах (не больше {maxRow}): ");
  77. string currentLenghtRow = Console.ReadLine();
  78. CheckUserInput(currentLenghtRow, maxRow, ref numRow);
  79. map = new char[numRow, numLine];
  80. Console.Clear();
  81.  
  82. map = DrawFrameMap(numLine, numRow, wall, ref map);
  83. Console.SetCursorPosition(0, numRow + 2);
  84. Console.WriteLine("Перещайтесь по карте стрелками.\n\n" +
  85. $"F1 - {wall} - Добаить элемент стены.\n" +
  86. $"F2 - {berry} - Добавить ягоду.\n" +
  87. $"F3 - {player} - Установить начальные координаты игрока\n" +
  88. "F4 - - Удалить объект\n" +
  89. "F5 - Выход\n");
  90.  
  91. bool isOpenCreateMenu = true;
  92. int cursorX = 1, cursorY = 1;
  93. Console.SetCursorPosition(cursorX, cursorY);
  94.  
  95. while (isOpenCreateMenu == true)
  96. {
  97. ConsoleKeyInfo keyMenu = Console.ReadKey(true);
  98. NavigationMenu(keyMenu, player, berry, wall, ref isOpenCreateMenu, ref cursorX, ref cursorY, ref map);
  99. mapSave = CopyMap(map);
  100. readyMap = true;
  101. }
  102.  
  103. break;
  104. case ConsoleKey.F3:
  105. mapSave = LoadMap("Map", out bool isLoadMap);
  106. numRow = mapSave.GetLength(0);
  107. numLine = mapSave.GetLength(1);
  108. readyMap = true;
  109. Console.WriteLine("\nКарта загрузилась успешно.\n");
  110. PrintMap(numRow, numLine, mapSave);
  111. Console.ReadKey();
  112.  
  113. Console.Clear();
  114. break;
  115.  
  116. case ConsoleKey.F5:
  117. isOpenMainMenu = false;
  118. map = new char[numRow, numLine];
  119. readyMap = false;
  120. Console.WriteLine("\nПока!");
  121. Console.ReadKey();
  122. Environment.Exit(0);
  123. break;
  124.  
  125. default:
  126. Console.SetCursorPosition(0, 6);
  127. Console.Write("Введите клавишу меню.");
  128. Console.ReadKey(true);
  129. Console.SetCursorPosition(0, 6);
  130. Console.WriteLine(" ");
  131. break;
  132. }
  133. Console.Clear();
  134. isOpenMainMenu = true;
  135. }
  136. }
  137. static char[,] LoadMap(string mapName, out bool isLoadMap)
  138. {
  139. isLoadMap = false;
  140. string[] newFile = File.ReadAllLines($"Map/{mapName}.txt");
  141. char[,] tempMap = new char[newFile.Length, newFile[0].Length];
  142.  
  143. for (int i = 0; i < tempMap.GetLength(0); i++)
  144. {
  145. for (int j = 0; j < tempMap.GetLength(1); j++)
  146. {
  147. tempMap[i, j] = newFile[i][j];
  148. }
  149. }
  150. isLoadMap = true;
  151. return tempMap;
  152. }
  153.  
  154. static char[,] CopyMap(char[,] map)
  155. {
  156. char[,] currentMap = new char[map.GetLength(0), map.GetLength(1)];
  157. for (int i = 0; i < map.GetLength(0); i++)
  158. {
  159. for (int j = 0; j < map.GetLength(1); j++)
  160. {
  161. currentMap[i, j] = map[i, j];
  162. }
  163. }
  164. return currentMap;
  165. }
  166. public static void NavigationMenu(ConsoleKeyInfo key, char player, char berry, char wall, ref bool isOpen, ref int x, ref int y, ref char[,] currentMap)
  167. {
  168. int dX = 0, dY = 0;
  169. switch (key.Key)
  170. {
  171. case ConsoleKey.F1:
  172. Console.Write(wall);
  173. currentMap[y, x] = wall;
  174. break;
  175. case ConsoleKey.F2:
  176. Console.Write(berry);
  177. currentMap[y, x] = berry;
  178. break;
  179. case ConsoleKey.F3:
  180. Console.Write(player);
  181. currentMap[y, x] = player;
  182. break;
  183. case ConsoleKey.F4:
  184. Console.Write(' ');
  185. currentMap[y, x] = ' ';
  186. break;
  187. case ConsoleKey.F5:
  188. isOpen = false;
  189. break;
  190. case ConsoleKey.UpArrow:
  191. dX = 0; dY = -1;
  192. break;
  193. case ConsoleKey.DownArrow:
  194. dX = 0; dY = 1;
  195. break;
  196. case ConsoleKey.LeftArrow:
  197. dX = -1; dY = 0;
  198. break;
  199. case ConsoleKey.RightArrow:
  200. dX = 1; dY = 0;
  201. break;
  202. }
  203. if ((x + dX > 0 && y + dY > 0) && (x + dX < (currentMap.GetLength(1) - 1) && y + dY < (currentMap.GetLength(0) - 1)))
  204. {
  205. x += dX;
  206. y += dY;
  207. }
  208. Console.SetCursorPosition(x, y);
  209. }
  210.  
  211. public static void NavigationMenu(ConsoleKeyInfo key, char player, char berry, char wall, int numRow, int numBerry, ref int collect, ref bool isOpen, ref int x, ref int y, ref char[,] currentMap)
  212. {
  213. int dX = 0, dY = 0;
  214. switch (key.Key)
  215. {
  216. case ConsoleKey.F5:
  217. isOpen = false;
  218. break;
  219. case ConsoleKey.UpArrow:
  220. dX = 0; dY = -1;
  221. break;
  222. case ConsoleKey.DownArrow:
  223. dX = 0; dY = 1;
  224. break;
  225. case ConsoleKey.LeftArrow:
  226. dX = -1; dY = 0;
  227. break;
  228. case ConsoleKey.RightArrow:
  229. dX = 1; dY = 0;
  230. break;
  231. default:
  232. Console.SetCursorPosition(0, numRow + 7);
  233. Console.WriteLine("Управляйте стрелками.");
  234. Console.ReadKey(true);
  235. Console.SetCursorPosition(0, numRow + 7);
  236. Console.WriteLine(" ");
  237. Console.SetCursorPosition(x, y);
  238. break;
  239. }
  240. if (currentMap[y + dY, x + dX] != wall)
  241. {
  242. Console.SetCursorPosition(x, y);
  243. Console.Write(' ');
  244. x += dX;
  245. y += dY;
  246. Console.SetCursorPosition(x, y);
  247. Console.Write(player);
  248. }
  249. if (currentMap[y, x] == berry)
  250. {
  251. collect++;
  252. currentMap[y, x] = ' ';
  253. }
  254. if (collect == numBerry)
  255. {
  256. Console.SetCursorPosition(0, numRow + 7);
  257. Console.WriteLine("Поздравляем, вы выиграли!");
  258. Console.WriteLine("\nНажмите любую клавишу для выхода в меню.");
  259. Console.ReadKey();
  260. Console.Clear();
  261. isOpen = false;
  262. }
  263. }
  264. static void PrintMap(int numRow, int numLine, char[,] map)
  265. {
  266. for (int i = 0; i < numRow; i++)
  267. {
  268. for (int j = 0; j < numLine; j++)
  269. {
  270. Console.Write(map[i, j]);
  271.  
  272. }
  273. Console.WriteLine();
  274. }
  275. }
  276. static int FindSumSymbol(char[,] currentMap, char symbol)
  277. {
  278. int numSymbol = 0;
  279. for (int i = 0; i < currentMap.GetLength(0); i++)
  280. {
  281. for (int j = 0; j < currentMap.GetLength(1); j++)
  282. {
  283. if (currentMap[i, j] == symbol)
  284. {
  285. numSymbol++;
  286. }
  287.  
  288. }
  289. }
  290. return numSymbol;
  291. }
  292. static void DeleteSymbol(char[,] currentMap, char symbol, ref int x, ref int y)
  293. {
  294. for (int i = 0; i < currentMap.GetLength(0); i++)
  295. {
  296. for (int j = 0; j < currentMap.GetLength(1); j++)
  297. {
  298. if (currentMap[i, j] == symbol)
  299. {
  300. currentMap[i, j] = ' ';
  301. y = i;
  302. x = j;
  303. }
  304.  
  305. }
  306. }
  307. }
  308. static void CheckUserInput(string currentMenu, int maxNumMenu, ref int currentNum)
  309. {
  310. bool goodMenu = Int32.TryParse(currentMenu, out int Num);
  311. if (goodMenu == true && (Num > 0 && Num <= maxNumMenu))
  312. {
  313. currentNum = Num;
  314. }
  315. else
  316. {
  317. Console.WriteLine("Не корректный ввод. Попробуйте еще раз.");
  318. }
  319. }
  320.  
  321. static char[,] DrawFrameMap(int numLine, int numRow, char element, ref char[,] map)
  322. {
  323. char[,] currentMap = map;
  324. for (int i = 0; i < numRow; i++)
  325. {
  326. for (int j = 0; j < numLine; j++)
  327. {
  328. if (i == 0 || i == numRow - 1)
  329. {
  330. Console.SetCursorPosition(j, i);
  331. Console.Write(element);
  332. currentMap[i, j] = element;
  333. }
  334. else if (j == 0 || j == numLine - 1)
  335. {
  336. Console.SetCursorPosition(j, i);
  337. Console.Write(element);
  338. currentMap[i, j] = element;
  339. }
  340. }
  341. Console.WriteLine();
  342. }
  343. return currentMap;
  344. }
  345. }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement