Advertisement
AziLif

Brave new World

Apr 1st, 2025 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.51 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int currentPositionByY = 7;
  10.             int currentPositionByX = 6;
  11.  
  12.             bool isDrawMap = true;
  13.  
  14.             ConsoleKey commandExit = ConsoleKey.Escape;
  15.  
  16.             string createPlayerSymbol = "Выберите символ для игрока:";
  17.             string createWallSymbol = "Выберите символ для стены:";
  18.  
  19.             char stone = CreatSymbol(createPlayerSymbol);
  20.             char signPlayer = CreatSymbol(createWallSymbol);
  21.  
  22.             Console.Clear();
  23.  
  24.             Console.CursorVisible = false;
  25.  
  26.             char[,] map =
  27.             {
  28.                 {stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone},
  29.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  30.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  31.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  32.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  33.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  34.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  35.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  36.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  37.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  38.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  39.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  40.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  41.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  42.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  43.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  44.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  45.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  46.                 {stone,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',stone,},
  47.                 {stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone,stone},
  48.             };
  49.  
  50.             while (isDrawMap)
  51.             {
  52.                 Console.SetCursorPosition(5, 22);
  53.                 Console.WriteLine($"Для продолжения нажмите любую кнопку." +
  54.                     $"Для выхода нажмите {commandExit}");
  55.  
  56.                 ConsoleKeyInfo move = Console.ReadKey();
  57.  
  58.                 if (move.Key == commandExit)
  59.                 {
  60.                     isDrawMap = false;
  61.  
  62.                     Console.WriteLine("Спасибо за игру!");
  63.                 }
  64.                 else
  65.                 {
  66.                     DrawMap(map);
  67.  
  68.                     DrawSymbolPlayer(signPlayer, currentPositionByY, currentPositionByX);
  69.  
  70.                     MovePlayer(map, ref currentPositionByX, ref currentPositionByY);
  71.                 }
  72.             }
  73.         }
  74.  
  75.         static void DrawMap(char[,] map)
  76.         {
  77.             Console.SetCursorPosition(0, 0);
  78.  
  79.             for (int i = 0; i < map.GetLength(0); i++)
  80.             {
  81.                 for (int j = 0; j < map.GetLength(1); j++)
  82.                 {
  83.                     Console.Write(map[i, j]);
  84.                 }
  85.  
  86.                 Console.WriteLine();
  87.             }
  88.         }
  89.  
  90.         static char CreatSymbol(string text)
  91.         {
  92.             Console.WriteLine(text);
  93.  
  94.             int requiredNumberCharacters = 1;
  95.  
  96.             string userInput = Console.ReadLine();
  97.  
  98.             if (userInput == "")
  99.             {
  100.                 char defaultSymbol = '@';
  101.  
  102.                 userInput = Convert.ToString(defaultSymbol);
  103.  
  104.                 Console.WriteLine($"Нужен хоть какой то символ... Символ по умолчанию - {defaultSymbol}");
  105.             }
  106.  
  107.             char symbol = userInput[0];
  108.  
  109.             if (userInput.Length > requiredNumberCharacters)
  110.             {
  111.                 Console.WriteLine("Ой, придется взять только первый символ...");
  112.  
  113.                 symbol = userInput[0];
  114.             }
  115.  
  116.             return symbol;
  117.         }
  118.  
  119.         static void DrawSymbolPlayer(char playerSymbol, int positionByX, int positionByY)
  120.         {
  121.             Console.SetCursorPosition(positionByX, positionByY);
  122.             Console.Write(playerSymbol);
  123.         }
  124.  
  125.         static int[] GetDirection()
  126.         {
  127.             const ConsoleKey MovementUp = ConsoleKey.UpArrow;
  128.             const ConsoleKey MovementDown = ConsoleKey.DownArrow;
  129.             const ConsoleKey MovementLeft = ConsoleKey.LeftArrow;
  130.             const ConsoleKey MovementRight = ConsoleKey.RightArrow;
  131.  
  132.             int[] direction = { 0, 0 };
  133.  
  134.             ConsoleKeyInfo move = Console.ReadKey();
  135.  
  136.             switch (move.Key)
  137.             {
  138.                 case MovementUp:
  139.                     direction[1] = -1;
  140.                     break;
  141.  
  142.                 case MovementDown:
  143.                     direction[1] = 1;
  144.                     break;
  145.  
  146.                 case MovementLeft:
  147.                     direction[0] = -1;
  148.                     break;
  149.  
  150.                 case MovementRight:
  151.                     direction[0] = 1;
  152.                     break;
  153.             }
  154.  
  155.             return direction;
  156.         }
  157.  
  158.         static void MovePlayer(char[,] map, ref int moveByX, ref int moveByY)
  159.         {
  160.             int[] direction = GetDirection();
  161.  
  162.             int nextPlayerPositionByX = moveByX + direction[1];
  163.             int nextPlayerPositionByY = moveByY + direction[0];
  164.  
  165.             if (map[nextPlayerPositionByX, nextPlayerPositionByY] == ' ')
  166.             {
  167.                 moveByX = nextPlayerPositionByX;
  168.                 moveByY = nextPlayerPositionByY;
  169.             }
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement