Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Clight_22_OOP_ClassPlayer_xy
- {
- class Program
- {
- static void Main(string[] args)
- {
- Player player = new Player();
- Draw drawPlayer = new Draw();
- int frameLines = 20;
- int frameRows = 10;
- char frameSymbol = '#';
- bool isDraw = true;
- while (isDraw)
- {
- Console.CursorVisible = false;
- DrawFrame(frameLines, frameRows, frameSymbol);
- Console.WriteLine("\nПеремещайте игрока с помощью стрелок.\n");
- Console.WriteLine("F1 - Изменить размеры карты.");
- Console.WriteLine("F2 - Задать координаты игрока вручную.");
- Console.WriteLine("F3 - Сменить внешний вид игрока.");
- Console.WriteLine("F5 - Выход.");
- drawPlayer.DrawPlayer(player.PlayerX, player.PlayerY, player.PlayerTexture);
- int dX = 0;
- int dY = 0;
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- dX = 0; dY = -1;
- break;
- case ConsoleKey.DownArrow:
- dX = 0; dY = 1;
- break;
- case ConsoleKey.RightArrow:
- dX = 1; dY = 0;
- break;
- case ConsoleKey.LeftArrow:
- dX = -1; dY = 0;
- break;
- case ConsoleKey.F1:
- int lenghtLine = CheckInputUser("Введите длину карты", 3, 50, frameRows);
- Console.WriteLine();
- int lenghtRow = CheckInputUser("Введите ширину карты", 3, 15, frameRows);
- frameLines = lenghtLine;
- frameRows = lenghtRow;
- player.DefaultXY();
- break;
- case ConsoleKey.F2:
- int userX = CheckInputUser("Введите значение по X", 1, frameLines - 2, frameRows);
- int userY = CheckInputUser("Введите значение по Y", 1, frameRows - 2, frameRows);
- player.DefaultXY(); //player.PlayerX и player.PlayerY = 1
- player.SetXY(userX - 1, userY - 1, frameLines, frameRows);
- break;
- case ConsoleKey.F3:
- char userTexture = CheckInputUser("Введите символ игрока", frameRows);
- player.SetTexture(userTexture);
- break;
- case ConsoleKey.F5:
- Console.SetCursorPosition(0, frameRows + 8);
- Console.WriteLine("Пока!");
- Console.ReadKey();
- Environment.Exit(0);
- break;
- }
- player.SetXY(dX, dY, frameLines, frameRows);
- Console.Clear();
- }
- }
- static void DrawFrame(int frameLine, int frameRow, char symbol)
- {
- for (int j = 0; j < frameRow; j++)
- {
- for (int i = 0; i < frameLine; i++)
- {
- if ((i == 0 || j == 0) || (i == frameLine - 1 || j == frameRow - 1))
- {
- Console.SetCursorPosition(i, j);
- Console.Write(symbol);
- }
- }
- Console.WriteLine();
- }
- }
- static int CheckInputUser(string text, int minValue, int maxValue, int frameRows)
- {
- bool goodCheckInput;
- bool isCheckInput = true;
- int valueInput = 1;
- Console.CursorVisible = true;
- while (isCheckInput)
- {
- Console.SetCursorPosition(0, frameRows + 8);
- Console.Write($"{text} от {minValue} до {maxValue}: ");
- string userInput = Console.ReadLine();
- goodCheckInput = Int32.TryParse(userInput, out int value);
- if (goodCheckInput != true || value < minValue || value > maxValue)
- {
- Console.WriteLine("Введите корректное число.");
- Console.ReadKey();
- Console.SetCursorPosition(0, frameRows + 8);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- else
- {
- valueInput = value;
- isCheckInput = false;
- Console.SetCursorPosition(0, frameRows + 8);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return valueInput;
- }
- static char CheckInputUser(string text, int frameRows)
- {
- bool goodCheckInput;
- bool isCheckInput = true;
- char charInput = ' ';
- Console.CursorVisible = true;
- while (isCheckInput)
- {
- Console.SetCursorPosition(0, frameRows + 8);
- Console.Write($"{text}: ");
- string userInput = Console.ReadLine();
- goodCheckInput = Char.TryParse(userInput, out char symbol);
- if (goodCheckInput != true)
- {
- Console.WriteLine("Введите любой, но только один символ.");
- Console.ReadKey();
- Console.SetCursorPosition(0, frameRows + 8);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- else
- {
- charInput = symbol;
- isCheckInput = false;
- Console.SetCursorPosition(0, frameRows + 8);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return charInput;
- }
- }
- class Player
- {
- private int _playerX;
- private int _playerY;
- private char _playerTexture;
- public int PlayerX { get; }
- public int PlayerY { get; }
- public char PlayerTexture { get; }
- public Player(int x, int y, char texture)
- {
- _playerX = x;
- _playerY = y;
- _playerTexture = texture;
- }
- public Player()
- {
- _playerX = 1;
- _playerY = 1;
- _playerTexture = '@';
- }
- public void SetXY(int dX, int dY, int frameLines, int frameRows)
- {
- if ((_playerX + dX > 0 && _playerY + dY > 0) &&
- (_playerX + dX < frameLines - 1 && _playerY + dY < frameRows - 1))
- {
- _playerX += dX;
- _playerY += dY;
- }
- }
- public void DefaultXY()
- {
- _playerX = 1;
- _playerY = 1;
- }
- public void SetTexture(char symbol)
- {
- _playerTexture = symbol;
- }
- }
- class Draw
- {
- public void DrawPlayer(int x, int y, char texture)
- {
- Console.SetCursorPosition(x, y);
- Console.Write(texture);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement