Anonim_999

бродилка

Mar 12th, 2021
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.84 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. namespace homework
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.Clear();
  10.             Console.CursorVisible = false;
  11.             char[,] map =
  12.             {
  13.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  14.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  15.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  16.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  17.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  18.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  19.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  20.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  21.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  22.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  23.                 {'#',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ','#'},
  24.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  25.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  26.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  27.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  28.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  29.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  30.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  31.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  32.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  33.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  34.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
  35.             };
  36.             bool isPlaying = true;
  37.             int playerX = 3,playerY = 3;
  38.  
  39.             DrawMap(map);
  40.  
  41.             while(isPlaying)
  42.             {
  43.                 if(Console.KeyAvailable)
  44.                 {
  45.                     ConsoleKeyInfo charKey = Console.ReadKey(true);
  46.                     Console.SetCursorPosition(playerY,playerX);
  47.                     Write(" ");
  48.  
  49.                     switch(charKey.Key)
  50.                     {
  51.                     case ConsoleKey.UpArrow:
  52.                         if(map[playerX -1,playerY] != '#')
  53.                         {
  54.                             playerX--;
  55.                         }
  56.                     break;
  57.                     case ConsoleKey.DownArrow:
  58.                         if(map[playerX +1,playerY] != '#')
  59.                         {
  60.                             playerX++;
  61.                         }
  62.                     break;
  63.                     case ConsoleKey.LeftArrow:
  64.                         if(map[playerX,playerY - 1] != '#')
  65.                         {
  66.                             playerY--;
  67.                         }
  68.                     break;
  69.                     case ConsoleKey.RightArrow:
  70.                         if(map[playerX,playerY + 1] != '#')
  71.                         {
  72.                             playerY++;
  73.                         }
  74.                     break;
  75.                     }  
  76.  
  77.                     Console.SetCursorPosition(playerY,playerX);
  78.                     Write("@");
  79.                    
  80.                 }
  81.             }
  82.  
  83.         }
  84.         private static void DrawMap(char[,] map)
  85.         {
  86.             for(int i = 0; i < map.GetLength(0); i++)
  87.             {
  88.  
  89.                 for (int j = 0; j < map.GetLength(1); j++)
  90.                 {
  91.                     Write($"{map[i,j]}");
  92.                 }
  93.  
  94.                 Write("\n");
  95.             }
  96.         }
  97.  
  98.         private static void Write(string text, ConsoleColor color = ConsoleColor.Yellow)
  99.         {
  100.             Console.ForegroundColor = color;
  101.             Console.Write(text);
  102.             Console.ResetColor();
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment