Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static char[,] map = new char[,]
- {
- {'#','#','#','#','#','#','#','#','#','#'},
- {'#',' ',' ','#',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#','#','#',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#','#','#','#',' ','#',' ','#',' ','#'},
- {'#',' ',' ',' ',' ','#',' ','#',' ','#'},
- {'#',' ','#','#','#','#',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ','#',' ','#'},
- {'#',' ',' ','#',' ','#',' ','#',' ','#'},
- {'#','#','#','#','#','#','#','#','#','#'},
- };
- static void DrawMap(int playerX, int playerY)
- {
- 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();
- }
- Console.SetCursorPosition(playerX, playerY);
- Console.Write('@');
- Console.SetCursorPosition(0, map.GetLength(0));
- }
- static void TryMove(int deltaX, int deltaY, ref int x, ref int y)
- {
- int newX = x + deltaX;
- int newY = y + deltaY;
- if (newX >= 0 && newX < map.GetLength(1) &&
- newY >= 0 && newY < map.GetLength(0))
- {
- if (map[newY, newX] != '#')
- {
- x = newX;
- y = newY;
- }
- }
- }
- static void Main()
- {
- int playerX = 1;
- int playerY = 1;
- Console.CursorVisible = false;
- while (true)
- {
- DrawMap(playerX, playerY);
- ConsoleKeyInfo keyInfo = Console.ReadKey(true);
- switch (keyInfo.Key)
- {
- case ConsoleKey.UpArrow:
- TryMove(0, -1, ref playerX, ref playerY);
- break;
- case ConsoleKey.DownArrow:
- TryMove(0, 1, ref playerX, ref playerY);
- break;
- case ConsoleKey.LeftArrow:
- TryMove(-1, 0, ref playerX, ref playerY);
- break;
- case ConsoleKey.RightArrow:
- TryMove(1, 0, ref playerX, ref playerY);
- break;
- case ConsoleKey.Escape:
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement