Advertisement
Rodunskiy

Untitled

Feb 28th, 2025
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. namespace CSLight
  2. {
  3.     public class Program
  4.     {
  5.         static char[,] map = {
  6.         { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  7.         { '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#' },
  8.         { '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#' },
  9.         { '#', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', '#' },
  10.         { '#', ' ', '#', '#', '#', '#', ' ', '#', ' ', '#' },
  11.         { '#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#' },
  12.         { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }
  13.         };
  14.  
  15.         static int playerX = 1;
  16.         static int playerY = 1;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             bool isRunning = true;
  21.  
  22.             while (isRunning)
  23.             {
  24.                 DrawMap();
  25.  
  26.                 ConsoleKeyInfo keyInfo = Console.ReadKey(true);
  27.                 switch (keyInfo.Key)
  28.                 {
  29.                     case ConsoleKey.W:
  30.                         MovePlayer(0, -1);
  31.                         break;
  32.                     case ConsoleKey.S:
  33.                         MovePlayer(0, 1);
  34.                         break;
  35.                     case ConsoleKey.A:
  36.                         MovePlayer(-1, 0);
  37.                         break;
  38.                     case ConsoleKey.D:
  39.                         MovePlayer(1, 0);
  40.                         break;
  41.                     case ConsoleKey.Enter:
  42.                         isRunning = false;
  43.                         break;
  44.                 }
  45.             }
  46.         }
  47.  
  48.         static void DrawMap()
  49.         {
  50.             Console.Clear();
  51.             for (int y = 0; y < map.GetLength(0); y++)
  52.             {
  53.                 for (int x = 0; x < map.GetLength(1); x++)
  54.                 {
  55.                     if (x == playerX && y == playerY)
  56.                     {
  57.                         Console.Write('0');
  58.                     }
  59.                     else
  60.                     {
  61.                         Console.Write(map[y, x]);
  62.                     }
  63.                 }
  64.                 Console.WriteLine();
  65.             }
  66.         }
  67.  
  68.         static void MovePlayer(int deltaX, int deltaY)
  69.         {
  70.             int newX = playerX + deltaX;
  71.             int newY = playerY + deltaY;
  72.  
  73.             if (newX >= 0 && newX < map.GetLength(1) && newY >= 0 && newY < map.GetLength(0))
  74.             {
  75.                 if (map[newY, newX] != '#')
  76.                 {
  77.                     playerX = newX;
  78.                     playerY = newY;
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement