Advertisement
Rodunskiy

Untitled

May 8th, 2025
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. namespace CSLight
  2. {
  3.  
  4.  
  5.     public class Program
  6.     {
  7.         private const char WALL_CHAR = '#';
  8.         private const char EMPTY_CHAR = ' ';
  9.         private const char PLAYER_CHAR = 'P';
  10.  
  11.         private const ConsoleKey MOVE_UP_KEY = ConsoleKey.W;
  12.         private const ConsoleKey MOVE_DOWN_KEY = ConsoleKey.S;
  13.         private const ConsoleKey MOVE_LEFT_KEY = ConsoleKey.A;
  14.         private const ConsoleKey MOVE_RIGHT_KEY = ConsoleKey.D;
  15.         private const ConsoleKey EXIT_KEY = ConsoleKey.Q;
  16.  
  17.         private static char[,] map = {
  18.         { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  19.         { '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#' },
  20.         { '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#' },
  21.         { '#', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', '#' },
  22.         { '#', ' ', '#', '#', '#', '#', ' ', '#', ' ', '#' },
  23.         { '#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#' },
  24.         { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }
  25.     };
  26.  
  27.         public static void Main()
  28.         {
  29.             int playerX = 1, playerY = 1;
  30.             bool isRunning = true;
  31.  
  32.             while (isRunning)
  33.             {
  34.                 DrawMap();
  35.                 DrawPlayer(playerX, playerY);
  36.  
  37.                 ReadInput(ref isRunning, out int deltaX, out int deltaY);
  38.                 MovePlayer(ref playerX, ref playerY, deltaX, deltaY);
  39.             }
  40.         }
  41.  
  42.         private static void DrawMap()
  43.         {
  44.             Console.Clear();
  45.             for (int y = 0; y < map.GetLength(0); y++)
  46.             {
  47.                 for (int x = 0; x < map.GetLength(1); x++)
  48.                 {
  49.                     Console.Write(map[y, x] + " ");
  50.                 }
  51.                 Console.WriteLine();
  52.             }
  53.         }
  54.  
  55.         private static void DrawPlayer(int playerX, int playerY)
  56.         {
  57.             Console.SetCursorPosition(playerX * 2, playerY);
  58.             Console.Write(PLAYER_CHAR);
  59.         }
  60.  
  61.         private static void ReadInput(ref bool isRunning, out int deltaX, out int deltaY)
  62.         {
  63.             deltaX = 0;
  64.             deltaY = 0;
  65.  
  66.             switch (Console.ReadKey(true).Key)
  67.             {
  68.                 case MOVE_UP_KEY: deltaY = -1; break;
  69.                 case MOVE_DOWN_KEY: deltaY = 1; break;
  70.                 case MOVE_LEFT_KEY: deltaX = -1; break;
  71.                 case MOVE_RIGHT_KEY: deltaX = 1; break;
  72.                 case EXIT_KEY: isRunning = false; break;
  73.             }
  74.         }
  75.  
  76.         private static void MovePlayer(ref int playerX, ref int playerY, int deltaX, int deltaY)
  77.         {
  78.             int newX = playerX + deltaX;
  79.             int newY = playerY + deltaY;
  80.  
  81.             if (CanMoveTo(newX, newY))
  82.             {
  83.                 playerX = newX;
  84.                 playerY = newY;
  85.             }
  86.         }
  87.  
  88.         private static bool CanMoveTo(int x, int y)
  89.         {
  90.             return x >= 0 && x < map.GetLength(1) &&
  91.                    y >= 0 && y < map.GetLength(0) &&
  92.                    map[y, x] != WALL_CHAR;
  93.         }
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement