Advertisement
Rodunskiy

Untitled

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