Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace CSLight
- {
- public class Program
- {
- private const char WALL_CHAR = '#';
- private const char EMPTY_CHAR = ' ';
- private const char PLAYER_CHAR = 'P';
- private const ConsoleKey MOVE_UP_KEY = ConsoleKey.W;
- private const ConsoleKey MOVE_DOWN_KEY = ConsoleKey.S;
- private const ConsoleKey MOVE_LEFT_KEY = ConsoleKey.A;
- private const ConsoleKey MOVE_RIGHT_KEY = ConsoleKey.D;
- private const ConsoleKey EXIT_KEY = ConsoleKey.Q;
- private static char[,] map = {
- { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
- { '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#' },
- { '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#' },
- { '#', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', '#' },
- { '#', ' ', '#', '#', '#', '#', ' ', '#', ' ', '#' },
- { '#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#' },
- { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }
- };
- public static void Main()
- {
- int playerX = 1, playerY = 1;
- bool isRunning = true;
- while (isRunning)
- {
- DrawMap();
- DrawPlayer(playerX, playerY);
- ReadInput(ref isRunning, out int deltaX, out int deltaY);
- MovePlayer(ref playerX, ref playerY, deltaX, deltaY);
- }
- }
- private static void DrawMap()
- {
- Console.Clear();
- for (int y = 0; y < map.GetLength(0); y++)
- {
- for (int x = 0; x < map.GetLength(1); x++)
- {
- Console.Write(map[y, x] + " ");
- }
- Console.WriteLine();
- }
- }
- private static void DrawPlayer(int playerX, int playerY)
- {
- Console.SetCursorPosition(playerX * 2, playerY);
- Console.Write(PLAYER_CHAR);
- }
- private static void ReadInput(ref bool isRunning, out int deltaX, out int deltaY)
- {
- deltaX = 0;
- deltaY = 0;
- switch (Console.ReadKey(true).Key)
- {
- case MOVE_UP_KEY: deltaY = -1; break;
- case MOVE_DOWN_KEY: deltaY = 1; break;
- case MOVE_LEFT_KEY: deltaX = -1; break;
- case MOVE_RIGHT_KEY: deltaX = 1; break;
- case EXIT_KEY: isRunning = false; break;
- }
- }
- private static void MovePlayer(ref int playerX, ref int playerY, int deltaX, int deltaY)
- {
- int newX = playerX + deltaX;
- int newY = playerY + deltaY;
- if (CanMoveTo(newX, newY))
- {
- playerX = newX;
- playerY = newY;
- }
- }
- private static bool CanMoveTo(int x, int y)
- {
- return x >= 0 && x < map.GetLength(1) &&
- y >= 0 && y < map.GetLength(0) &&
- map[y, x] != WALL_CHAR;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement