Advertisement
SnowPhoenix347

4.4

Nov 5th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FifthProject
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.CursorVisible = false;
  10.             int userY = 1;
  11.             int userX = 1;
  12.             int mapSizeX = 0;
  13.             int mapSizeY = 0;
  14.             char[,] map = new char[0, 0];
  15.  
  16.             SettingMap(ref mapSizeX, ref mapSizeY);
  17.             GenerateMap(out map, mapSizeX, mapSizeY);
  18.             while (true)
  19.             {
  20.                 Console.Clear();
  21.                 DrawMap(map);
  22.                 PlayerController(ref userX, ref userY, map);
  23.             }
  24.         }
  25.  
  26.         static int UserInputInt()
  27.         {
  28.             int output;
  29.             bool enterIsCorrect = false;
  30.             do
  31.             {
  32.                 string input = Console.ReadLine();
  33.                 enterIsCorrect = int.TryParse(input, out output);
  34.                 if (!enterIsCorrect)
  35.                 {
  36.                     Console.WriteLine("Error enter. Try again");
  37.                 }
  38.             } while (!enterIsCorrect);
  39.  
  40.             return output;
  41.         }
  42.  
  43.         static void SettingMap(ref int x, ref int y)
  44.         {
  45.             Console.WriteLine("Введите высоту карты");
  46.             x = UserInputInt();
  47.             Console.WriteLine("Введите ширину карты");
  48.             y = UserInputInt();
  49.         }
  50.  
  51.         static void GenerateMap(out char[,] arr, int x, int y)
  52.         {
  53.             char[,] tempMap = new char[x, y];
  54.             for (int i = 0; i < tempMap.GetLength(0); i++)
  55.             {
  56.                 for (int j = 0; j < tempMap.GetLength(1); j++)
  57.                 {
  58.                     if (i == 0 || i == tempMap.GetLength(0) - 1) tempMap[i,j] = '#';
  59.                     else if (j == 0 || j == tempMap.GetLength(1) - 1) tempMap[i,j] = '#';
  60.                     else tempMap[i,j] = ' ';
  61.                 }
  62.  
  63.                 Console.WriteLine();
  64.             }
  65.             arr = tempMap;
  66.         }
  67.  
  68.         static void DrawMap(char[,] map)
  69.         {
  70.             for (int i = 0; i < map.GetLength(0); i++)
  71.             {
  72.                 for (int j = 0; j < map.GetLength(1); j++)
  73.                 {
  74.                     Console.Write(map[i,j]);
  75.                 }
  76.                 Console.WriteLine();
  77.             }
  78.         }
  79.  
  80.         static void PlayerController(ref int y, ref int x,char[,] map)
  81.         {
  82.             Console.SetCursorPosition(y, x);
  83.             Console.Write('@');
  84.  
  85.             ConsoleKeyInfo consoleKey = Console.ReadKey();
  86.  
  87.             switch (consoleKey.Key)
  88.             {
  89.                 case ConsoleKey.LeftArrow:
  90.                     if (map[x, y - 1] != '#')
  91.                     {
  92.                         y--;
  93.                     }
  94.  
  95.                     break;
  96.                 case ConsoleKey.RightArrow:
  97.                     if (map[x, y + 1] != '#')
  98.                     {
  99.                         y++;
  100.                     }
  101.  
  102.                     break;
  103.                 case ConsoleKey.UpArrow:
  104.                     if (map[x - 1, y] != '#')
  105.                     {
  106.                         x--;
  107.                     }
  108.  
  109.                     break;
  110.                 case ConsoleKey.DownArrow:
  111.                     if (map[x + 1, y] != '#')
  112.                     {
  113.                         x++;
  114.                     }
  115.  
  116.                     break;
  117.             }
  118.  
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement