Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- int currentPositionByY = 7;
- int currentPositionByX = 6;
- bool isDrawMap = true;
- ConsoleKey commandExit = ConsoleKey.Escape;
- string createPlayerSymbol = "Выберите символ для игрока:";
- string createWallSymbol = "Выберите символ для стены:";
- char stone = CreatSymbol(createPlayerSymbol);
- char signPlayer = CreatSymbol(createWallSymbol);
- Console.Clear();
- Console.CursorVisible = false;
- char[,] map =
- {
- {stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
- {stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone},
- };
- while (isDrawMap)
- {
- Console.SetCursorPosition(5, 22);
- Console.WriteLine($"Для продолжения нажмите любую кнопку." +
- $"Для выхода нажмите {commandExit}");
- ConsoleKeyInfo move = Console.ReadKey();
- if (move.Key == commandExit)
- {
- isDrawMap = false;
- Console.WriteLine("Спасибо за игру!");
- }
- else
- {
- DrawMap(map);
- DrawSymbolPlayer(signPlayer, currentPositionByY, currentPositionByX);
- MovePlayer(map, ref currentPositionByX, ref currentPositionByY);
- }
- }
- }
- static void DrawMap(char[,] map)
- {
- 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();
- }
- }
- static char CreatSymbol(string text)
- {
- Console.WriteLine(text);
- int requiredNumberCharacters = 1;
- string userInput = Console.ReadLine();
- if (userInput == "")
- {
- char defaultSymbol = '@';
- userInput = Convert.ToString(defaultSymbol);
- Console.WriteLine($"Нужен хоть какой то символ... Символ по умолчанию - {defaultSymbol}");
- }
- char symbol = userInput[0];
- if (userInput.Length > requiredNumberCharacters)
- {
- Console.WriteLine("Ой, придется взять только первый символ...");
- symbol = userInput[0];
- }
- return symbol;
- }
- static void DrawSymbolPlayer(char playerSymbol, int positionByX, int positionByY)
- {
- Console.SetCursorPosition(positionByX, positionByY);
- Console.Write(playerSymbol);
- }
- static int[] GetDirection()
- {
- const ConsoleKey MovementUp = ConsoleKey.UpArrow;
- const ConsoleKey MovementDown = ConsoleKey.DownArrow;
- const ConsoleKey MovementLeft = ConsoleKey.LeftArrow;
- const ConsoleKey MovementRight = ConsoleKey.RightArrow;
- int[] direction = { 0, 0 };
- ConsoleKeyInfo move = Console.ReadKey();
- switch (move.Key)
- {
- case MovementUp:
- direction[1] = -1;
- break;
- case MovementDown:
- direction[1] = 1;
- break;
- case MovementLeft:
- direction[0] = -1;
- break;
- case MovementRight:
- direction[0] = 1;
- break;
- }
- return direction;
- }
- static void MovePlayer(char[,] map, ref int moveByX, ref int moveByY)
- {
- int[] direction = GetDirection();
- int nextPlayerPositionByX = moveByX + direction[1];
- int nextPlayerPositionByY = moveByY + direction[0];
- if (map[nextPlayerPositionByX, nextPlayerPositionByY] == ' ')
- {
- moveByX = nextPlayerPositionByX;
- moveByY = nextPlayerPositionByY;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement