Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace Clight_19_Game_MapControlPlayer
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool isOpenMainMenu = true;
- bool readyMap = false;
- int maxLine = 30;
- int maxRow = 10;
- int numLine = 5;
- int numRow = 3;
- char[,] map = new char[numRow, numLine];
- char[,] mapSave = new char[numRow, numLine];
- char berry = '%';
- char player = '0';
- char wall = '#';
- while (isOpenMainMenu == true)
- {
- Console.CursorVisible = true;
- Console.WriteLine("Меню игры:\n");
- Console.WriteLine("F1 - Играть");
- Console.WriteLine("F2 - Нарисовать карту");
- Console.WriteLine("F3 - Загрузить карту");
- Console.WriteLine("F5 - Выход");
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.F1:
- bool isPlaying = true;
- if (readyMap == false)
- {
- Console.SetCursorPosition(0, 6);
- Console.Write("Сначала создайте карту. Нажмите F1.");
- Console.ReadKey(true);
- Console.SetCursorPosition(0, 6);
- Console.WriteLine(" ");
- break;
- }
- Console.Clear();
- int xPlayer = 1;
- int yPlayer = 1;
- int numBerry = 0;
- int collectBerry = 0;
- map = CopyMap(mapSave);
- numBerry = FindSumSymbol(map, berry);
- PrintMap(numRow, numLine, map);
- DeleteSymbol(map, player, ref xPlayer, ref yPlayer); //удаляется символ игрока с карты и считываются координаты
- while (isPlaying == true)
- {
- Console.CursorVisible = false;
- Console.SetCursorPosition(0, numRow + 2);
- Console.WriteLine($"Собрано ягод: {collectBerry}/{numBerry}");
- Console.WriteLine("\nF5 - выход");
- Console.SetCursorPosition(xPlayer, yPlayer);
- ConsoleKeyInfo keyPlay = Console.ReadKey(true);
- NavigationMenu(keyPlay, player, berry, wall, numRow, numBerry, ref collectBerry, ref isPlaying, ref xPlayer, ref yPlayer, ref map);
- }
- break;
- case ConsoleKey.F2:
- Console.Write($"Длина карты в символах (не больше {maxLine}): ");
- string currentLenghtLine = Console.ReadLine();
- CheckUserInput(currentLenghtLine, maxLine, ref numLine);
- Console.Write($"Высота карты в символах (не больше {maxRow}): ");
- string currentLenghtRow = Console.ReadLine();
- CheckUserInput(currentLenghtRow, maxRow, ref numRow);
- map = new char[numRow, numLine];
- Console.Clear();
- map = DrawFrameMap(numLine, numRow, wall, ref map);
- Console.SetCursorPosition(0, numRow + 2);
- Console.WriteLine("Перещайтесь по карте стрелками.\n\n" +
- $"F1 - {wall} - Добаить элемент стены.\n" +
- $"F2 - {berry} - Добавить ягоду.\n" +
- $"F3 - {player} - Установить начальные координаты игрока\n" +
- "F4 - - Удалить объект\n" +
- "F5 - Выход\n");
- bool isOpenCreateMenu = true;
- int cursorX = 1, cursorY = 1;
- Console.SetCursorPosition(cursorX, cursorY);
- while (isOpenCreateMenu == true)
- {
- ConsoleKeyInfo keyMenu = Console.ReadKey(true);
- NavigationMenu(keyMenu, player, berry, wall, ref isOpenCreateMenu, ref cursorX, ref cursorY, ref map);
- mapSave = CopyMap(map);
- readyMap = true;
- }
- break;
- case ConsoleKey.F3:
- mapSave = LoadMap("Map", out bool isLoadMap);
- numRow = mapSave.GetLength(0);
- numLine = mapSave.GetLength(1);
- readyMap = true;
- Console.WriteLine("\nКарта загрузилась успешно.\n");
- PrintMap(numRow, numLine, mapSave);
- Console.ReadKey();
- Console.Clear();
- break;
- case ConsoleKey.F5:
- isOpenMainMenu = false;
- map = new char[numRow, numLine];
- readyMap = false;
- Console.WriteLine("\nПока!");
- Console.ReadKey();
- Environment.Exit(0);
- break;
- default:
- Console.SetCursorPosition(0, 6);
- Console.Write("Введите клавишу меню.");
- Console.ReadKey(true);
- Console.SetCursorPosition(0, 6);
- Console.WriteLine(" ");
- break;
- }
- Console.Clear();
- isOpenMainMenu = true;
- }
- }
- static char[,] LoadMap(string mapName, out bool isLoadMap)
- {
- isLoadMap = false;
- string[] newFile = File.ReadAllLines($"Map/{mapName}.txt");
- char[,] tempMap = new char[newFile.Length, newFile[0].Length];
- for (int i = 0; i < tempMap.GetLength(0); i++)
- {
- for (int j = 0; j < tempMap.GetLength(1); j++)
- {
- tempMap[i, j] = newFile[i][j];
- }
- }
- isLoadMap = true;
- return tempMap;
- }
- static char[,] CopyMap(char[,] map)
- {
- char[,] currentMap = new char[map.GetLength(0), map.GetLength(1)];
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- currentMap[i, j] = map[i, j];
- }
- }
- return currentMap;
- }
- public static void NavigationMenu(ConsoleKeyInfo key, char player, char berry, char wall, ref bool isOpen, ref int x, ref int y, ref char[,] currentMap)
- {
- int dX = 0, dY = 0;
- switch (key.Key)
- {
- case ConsoleKey.F1:
- Console.Write(wall);
- currentMap[y, x] = wall;
- break;
- case ConsoleKey.F2:
- Console.Write(berry);
- currentMap[y, x] = berry;
- break;
- case ConsoleKey.F3:
- Console.Write(player);
- currentMap[y, x] = player;
- break;
- case ConsoleKey.F4:
- Console.Write(' ');
- currentMap[y, x] = ' ';
- break;
- case ConsoleKey.F5:
- isOpen = false;
- break;
- case ConsoleKey.UpArrow:
- dX = 0; dY = -1;
- break;
- case ConsoleKey.DownArrow:
- dX = 0; dY = 1;
- break;
- case ConsoleKey.LeftArrow:
- dX = -1; dY = 0;
- break;
- case ConsoleKey.RightArrow:
- dX = 1; dY = 0;
- break;
- }
- if ((x + dX > 0 && y + dY > 0) && (x + dX < (currentMap.GetLength(1) - 1) && y + dY < (currentMap.GetLength(0) - 1)))
- {
- x += dX;
- y += dY;
- }
- Console.SetCursorPosition(x, y);
- }
- 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)
- {
- int dX = 0, dY = 0;
- switch (key.Key)
- {
- case ConsoleKey.F5:
- isOpen = false;
- break;
- case ConsoleKey.UpArrow:
- dX = 0; dY = -1;
- break;
- case ConsoleKey.DownArrow:
- dX = 0; dY = 1;
- break;
- case ConsoleKey.LeftArrow:
- dX = -1; dY = 0;
- break;
- case ConsoleKey.RightArrow:
- dX = 1; dY = 0;
- break;
- default:
- Console.SetCursorPosition(0, numRow + 7);
- Console.WriteLine("Управляйте стрелками.");
- Console.ReadKey(true);
- Console.SetCursorPosition(0, numRow + 7);
- Console.WriteLine(" ");
- Console.SetCursorPosition(x, y);
- break;
- }
- if (currentMap[y + dY, x + dX] != wall)
- {
- Console.SetCursorPosition(x, y);
- Console.Write(' ');
- x += dX;
- y += dY;
- Console.SetCursorPosition(x, y);
- Console.Write(player);
- }
- if (currentMap[y, x] == berry)
- {
- collect++;
- currentMap[y, x] = ' ';
- }
- if (collect == numBerry)
- {
- Console.SetCursorPosition(0, numRow + 7);
- Console.WriteLine("Поздравляем, вы выиграли!");
- Console.WriteLine("\nНажмите любую клавишу для выхода в меню.");
- Console.ReadKey();
- Console.Clear();
- isOpen = false;
- }
- }
- static void PrintMap(int numRow, int numLine, char[,] map)
- {
- for (int i = 0; i < numRow; i++)
- {
- for (int j = 0; j < numLine; j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- static int FindSumSymbol(char[,] currentMap, char symbol)
- {
- int numSymbol = 0;
- for (int i = 0; i < currentMap.GetLength(0); i++)
- {
- for (int j = 0; j < currentMap.GetLength(1); j++)
- {
- if (currentMap[i, j] == symbol)
- {
- numSymbol++;
- }
- }
- }
- return numSymbol;
- }
- static void DeleteSymbol(char[,] currentMap, char symbol, ref int x, ref int y)
- {
- for (int i = 0; i < currentMap.GetLength(0); i++)
- {
- for (int j = 0; j < currentMap.GetLength(1); j++)
- {
- if (currentMap[i, j] == symbol)
- {
- currentMap[i, j] = ' ';
- y = i;
- x = j;
- }
- }
- }
- }
- static void CheckUserInput(string currentMenu, int maxNumMenu, ref int currentNum)
- {
- bool goodMenu = Int32.TryParse(currentMenu, out int Num);
- if (goodMenu == true && (Num > 0 && Num <= maxNumMenu))
- {
- currentNum = Num;
- }
- else
- {
- Console.WriteLine("Не корректный ввод. Попробуйте еще раз.");
- }
- }
- static char[,] DrawFrameMap(int numLine, int numRow, char element, ref char[,] map)
- {
- char[,] currentMap = map;
- for (int i = 0; i < numRow; i++)
- {
- for (int j = 0; j < numLine; j++)
- {
- if (i == 0 || i == numRow - 1)
- {
- Console.SetCursorPosition(j, i);
- Console.Write(element);
- currentMap[i, j] = element;
- }
- else if (j == 0 || j == numLine - 1)
- {
- Console.SetCursorPosition(j, i);
- Console.Write(element);
- currentMap[i, j] = element;
- }
- }
- Console.WriteLine();
- }
- return currentMap;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement