Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace CSLight_4._3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- char[,] map = CreateMap();
- int positionX = 1, positionY = 1;
- int positionDX = 0, positionDY = 0;
- int mapHeight, mapWidth;
- string answerUser;
- Console.Write("Нарисовать свою карту? (да/нет): ");
- answerUser = Console.ReadLine();
- if (answerUser.ToLower() == "нет".ToLower())
- {
- Console.Clear();
- ShowMap(map, positionX, positionY);
- while (true)
- {
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key, ref positionDX, ref positionDY);
- if (map[positionX + positionDX, positionY + positionDY] != '#')
- {
- MoveCharacter(map, ref positionX, ref positionY, positionDX, positionDY, key, '@', ' ');
- positionDX = 0;
- positionDY = 0;
- }
- }
- }
- }
- else if (answerUser.ToLower() == "да".ToLower())
- {
- Console.Write("Ведите ширину карты: ");
- mapWidth = Convert.ToInt32(Console.ReadLine());
- Console.Write("Ведите высоту карты карты: ");
- mapHeight = Convert.ToInt32(Console.ReadLine());
- Console.Write("Введите символ полей карты: ");
- char symbol = Convert.ToChar(Console.ReadLine());
- Console.Write("Введите символ персонажа: ");
- char persSymbol = Convert.ToChar(Console.ReadLine());
- char[,] newMap = new char[mapHeight, mapWidth];
- Console.Clear();
- WriteMap(newMap, symbol, persSymbol, positionX, positionY);
- while (true)
- {
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key, ref positionDX, ref positionDY);
- if (newMap[positionX + positionDX, positionY + positionDY] != symbol)
- {
- MoveCharacter(newMap, ref positionX, ref positionY, positionDX, positionDY, key, persSymbol, symbol);
- positionDX = 0;
- positionDY = 0;
- }
- }
- }
- }
- }
- static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- DX = -1; DY = 0;
- break;
- case ConsoleKey.DownArrow:
- DX = 1; DY = 0;
- break;
- case ConsoleKey.LeftArrow:
- DX = 0; DY = -1;
- break;
- case ConsoleKey.RightArrow:
- DX = 0; DY = 1;
- break;
- }
- }
- static void MoveCharacter(char[,] map, ref int posX, ref int posY, int posDX, int posDY, ConsoleKeyInfo key, char persSymbol, char symbol)
- {
- Console.SetCursorPosition(posY, posX);
- Console.Write(map[posX, posY]);
- switch (key.Key)
- {
- case ConsoleKey.Enter:
- map[posX, posY] = symbol;
- break;
- }
- posX += posDX;
- posY += posDY;
- Console.SetCursorPosition(posY, posX);
- Console.Write(persSymbol);
- }
- static void WriteMap(char[,] newMap, char symbol, char persSymbol, int userX, int userY)
- {
- Console.SetCursorPosition(0, 0);
- for (int i = 0; i < newMap.GetLength(0); i++)
- {
- for (int j = 0; j < newMap.GetLength(1); j++)
- {
- if (newMap[i, j] == i || newMap[i, j] == symbol)
- {
- newMap[i, j] = symbol;
- Console.Write(newMap[i, j]);
- }
- else if (i == (newMap.GetLength(0) - 1))
- {
- newMap[i, j] = symbol;
- Console.Write(newMap[i, j]);
- }
- else if (j == 0)
- {
- newMap[i, j] = symbol;
- Console.Write(newMap[i, j]);
- }
- else if (j == newMap.GetLength(1) - 1)
- {
- newMap[i, j] = symbol;
- Console.Write(newMap[i, j]);
- }
- else if (newMap[i, j] != i)
- {
- newMap[i, j] = ' ';
- Console.Write(newMap[i, j]);
- }
- }
- Console.WriteLine();
- }
- Console.SetCursorPosition(userX, userY);
- Console.WriteLine(persSymbol);
- }
- static char[,] CreateMap()
- {
- Console.SetCursorPosition(0, 0);
- char[,] map =
- {
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#','#','#','#','#','#'},
- {'#',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
- };
- return map;
- }
- static void ShowMap(char[,] map, int userX, int userY)
- {
- Console.SetCursorPosition(0, 0);
- 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();
- }
- Console.SetCursorPosition(userX, userY);
- Console.Write('@');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement