Advertisement
vovanhik_24

#31

Sep 7th, 2023 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1.         static void Main(string[] args)
  2.         {
  3.             const string CreateMapCommand = "1";
  4.             const string StartPlayCommand = "2";
  5.             const string ExitMenuCommand = "3";
  6.  
  7.             bool isWorking = true;
  8.  
  9.             char[,] map = new char[0, 0];
  10.             char symbolWall = ' ';
  11.  
  12.             while (isWorking)
  13.             {
  14.                 Console.Clear();
  15.                 Console.WriteLine("1 <=== Создать карту \n2 <=== Начать игру \n3 <=== Выход");
  16.                 string input = Console.ReadLine();
  17.  
  18.                 switch (input)
  19.                 {
  20.                     case CreateMapCommand:
  21.                         CreateMap(ref map, ref symbolWall);
  22.                         break;
  23.  
  24.                     case StartPlayCommand:
  25.                         PlayGame(map, symbolWall);
  26.                         break;
  27.  
  28.                     case ExitMenuCommand:
  29.                         isWorking = false;
  30.                         break;
  31.                 }
  32.             }
  33.         }
  34.  
  35.         private static void CreateMap(ref char[,] map, ref char symbolWall)
  36.         {
  37.             Console.Write("Введите размер стен карты: ");
  38.             int mapSize = Convert.ToInt32(Console.ReadLine());
  39.  
  40.             Console.Write("Введите символ для отрисовки стен: ");
  41.             symbolWall = Convert.ToChar(Console.ReadLine());
  42.  
  43.             map = new char[mapSize, mapSize];
  44.  
  45.             BuilderMapWall(map, symbolWall);
  46.  
  47.             Console.WriteLine("Нажмите любую клавишу для продолжения");
  48.             Console.ReadKey();
  49.         }
  50.  
  51.         private static void BuilderMapWall(char[,] map, char symbolWall)
  52.         {
  53.             int firstPosition = 0;
  54.             int offsetStep = 1;
  55.  
  56.             for (int i = 0; i < map.GetLength(0); i++)
  57.             {
  58.                 map[firstPosition, i] = symbolWall;
  59.                 map[map.GetLength(1) - offsetStep, i] = symbolWall;
  60.             }
  61.  
  62.             for (int i = 1; i < map.GetLength(0) - 1; i++)
  63.             {
  64.                 map[i, firstPosition] = symbolWall;
  65.                 map[i, map.GetLength(0) - offsetStep] = symbolWall;
  66.             }
  67.         }
  68.  
  69.         static void DrawMap(char[,] map)
  70.         {
  71.             for (int i = 0; i < map.GetLength(0); i++)
  72.             {
  73.                 for (int j = 0; j < map.GetLength(1); j++)
  74.                 {
  75.                     Console.Write(map[i, j]);
  76.                 }
  77.  
  78.                 Console.WriteLine();
  79.             }
  80.         }
  81.  
  82.         private static void PlayGame(char[,] map, char symbolWall)
  83.         {
  84.             Console.Write("Введите символ для отображения персонажа: ");
  85.             char playerChar = Convert.ToChar(Console.ReadLine());
  86.  
  87.             bool gameStarted = true;
  88.  
  89.             int playerPositionX = 2;
  90.             int playerPositionY = 1;
  91.  
  92.             Console.CursorVisible = false;
  93.  
  94.             while (gameStarted)
  95.             {
  96.                 Console.Clear();
  97.                 DrawMap(map);
  98.  
  99.                 Console.SetCursorPosition(playerPositionX, playerPositionY);
  100.                 Console.Write(playerChar);
  101.  
  102.                 MovmentController(map, ref playerPositionX, ref playerPositionY, symbolWall);
  103.             }
  104.         }
  105.  
  106.         static void MovmentController(char[,] map, ref int positionX, ref int positionY, char symbolWall)
  107.         {
  108.             ConsoleKeyInfo charKey = Console.ReadKey();
  109.  
  110.             switch (charKey.Key)
  111.             {
  112.                 case ConsoleKey.UpArrow:
  113.                     MovePlayerPosition(map, charKey, positionX, positionY - 1, ref positionY, symbolWall);
  114.                     break;
  115.  
  116.                 case ConsoleKey.DownArrow:
  117.                     MovePlayerPosition(map, charKey, positionX, positionY + 1, ref positionY, symbolWall);
  118.                     break;
  119.  
  120.                 case ConsoleKey.LeftArrow:
  121.                     MovePlayerPosition(map, charKey, positionX - 1, positionY, ref positionX, symbolWall);
  122.                     break;
  123.  
  124.                 case ConsoleKey.RightArrow:
  125.                     MovePlayerPosition(map, charKey, positionX + 1, positionY, ref positionX, symbolWall);
  126.                     break;
  127.             }
  128.         }
  129.  
  130.         static void MovePlayerPosition(char[,] map, ConsoleKeyInfo charKey, int positionX, int positionY, ref int position, char symbolWall)
  131.         {
  132.             bool isWall = false;
  133.  
  134.             CheckBorder(map, positionX, positionY, ref isWall, symbolWall);
  135.             MovePlayer(charKey, ref position, isWall);
  136.         }
  137.  
  138.         static bool CheckBorder(char[,] map, int positionX, int positionY, ref bool isWall, char symbolWall)
  139.         {
  140.             if (map[positionX, positionY] == symbolWall)
  141.             {
  142.                 isWall = true;
  143.             }
  144.  
  145.             return isWall;
  146.         }
  147.  
  148.         static void MovePlayer(ConsoleKeyInfo charKey, ref int position, bool isWall)
  149.         {
  150.             if (isWall == false)
  151.             {
  152.                 if (charKey.Key == ConsoleKey.DownArrow || charKey.Key == ConsoleKey.RightArrow)
  153.                 {
  154.                     position++;
  155.                 }
  156.                 else if (charKey.Key == ConsoleKey.UpArrow || charKey.Key == ConsoleKey.LeftArrow)
  157.                 {
  158.                     position--;
  159.                 }
  160.             }
  161.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement