Advertisement
TwinFrame

Game_EditMap_ControlPlayer

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