Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- const string CreateMapCommand = "1";
- const string StartPlayCommand = "2";
- const string ExitMenuCommand = "3";
- bool isWorking = true;
- char[,] map = new char[0, 0];
- char symbolWall = ' ';
- while (isWorking)
- {
- Console.Clear();
- Console.WriteLine("1 <=== Создать карту \n2 <=== Начать игру \n3 <=== Выход");
- string input = Console.ReadLine();
- switch (input)
- {
- case CreateMapCommand:
- CreateMap(ref map, ref symbolWall);
- break;
- case StartPlayCommand:
- PlayGame(map, symbolWall);
- break;
- case ExitMenuCommand:
- isWorking = false;
- break;
- }
- }
- }
- private static void CreateMap(ref char[,] map, ref char symbolWall)
- {
- Console.Write("Введите размер стен карты: ");
- int mapSize = Convert.ToInt32(Console.ReadLine());
- Console.Write("Введите символ для отрисовки стен: ");
- symbolWall = Convert.ToChar(Console.ReadLine());
- map = new char[mapSize, mapSize];
- BuilderMapWall(map, symbolWall);
- Console.WriteLine("Нажмите любую клавишу для продолжения");
- Console.ReadKey();
- }
- private static void BuilderMapWall(char[,] map, char symbolWall)
- {
- int firstPosition = 0;
- int offsetStep = 1;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- map[firstPosition, i] = symbolWall;
- map[map.GetLength(1) - offsetStep, i] = symbolWall;
- }
- for (int i = 1; i < map.GetLength(0) - 1; i++)
- {
- map[i, firstPosition] = symbolWall;
- map[i, map.GetLength(0) - offsetStep] = symbolWall;
- }
- }
- static void DrawMap(char[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- private static void PlayGame(char[,] map, char symbolWall)
- {
- Console.Write("Введите символ для отображения персонажа: ");
- char playerChar = Convert.ToChar(Console.ReadLine());
- bool gameStarted = true;
- int playerPositionX = 2;
- int playerPositionY = 1;
- Console.CursorVisible = false;
- while (gameStarted)
- {
- Console.Clear();
- DrawMap(map);
- Console.SetCursorPosition(playerPositionX, playerPositionY);
- Console.Write(playerChar);
- MovmentController(map, ref playerPositionX, ref playerPositionY, symbolWall);
- }
- }
- static void MovmentController(char[,] map, ref int positionX, ref int positionY, char symbolWall)
- {
- ConsoleKeyInfo charKey = Console.ReadKey();
- switch (charKey.Key)
- {
- case ConsoleKey.UpArrow:
- MovePlayerPosition(map, charKey, positionX, positionY - 1, ref positionY, symbolWall);
- break;
- case ConsoleKey.DownArrow:
- MovePlayerPosition(map, charKey, positionX, positionY + 1, ref positionY, symbolWall);
- break;
- case ConsoleKey.LeftArrow:
- MovePlayerPosition(map, charKey, positionX - 1, positionY, ref positionX, symbolWall);
- break;
- case ConsoleKey.RightArrow:
- MovePlayerPosition(map, charKey, positionX + 1, positionY, ref positionX, symbolWall);
- break;
- }
- }
- static void MovePlayerPosition(char[,] map, ConsoleKeyInfo charKey, int positionX, int positionY, ref int position, char symbolWall)
- {
- bool isWall = false;
- CheckBorder(map, positionX, positionY, ref isWall, symbolWall);
- MovePlayer(charKey, ref position, isWall);
- }
- static bool CheckBorder(char[,] map, int positionX, int positionY, ref bool isWall, char symbolWall)
- {
- if (map[positionX, positionY] == symbolWall)
- {
- isWall = true;
- }
- return isWall;
- }
- static void MovePlayer(ConsoleKeyInfo charKey, ref int position, bool isWall)
- {
- if (isWall == false)
- {
- if (charKey.Key == ConsoleKey.DownArrow || charKey.Key == ConsoleKey.RightArrow)
- {
- position++;
- }
- else if (charKey.Key == ConsoleKey.UpArrow || charKey.Key == ConsoleKey.LeftArrow)
- {
- position--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement