Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace CSLight
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- const ConsoleKey CommandMoveUp = ConsoleKey.UpArrow;
- const ConsoleKey CommandMoveDown = ConsoleKey.DownArrow;
- const ConsoleKey CommandMoveLeft = ConsoleKey.LeftArrow;
- const ConsoleKey CommandMoveRight = ConsoleKey.RightArrow;
- bool isRunnig = true;
- char[,] map =
- {
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
- };
- char emptySymbol = ' ';
- char playerSymbol = '@';
- int playerPositionX = 1;
- int playerPositionY = 1;
- int moveDistance = 1;
- Console.CursorVisible = false;
- while (isRunnig)
- {
- Console.Clear();
- DrawMap(map);
- DrawPlayer(playerSymbol, playerPositionX, playerPositionY);
- ConsoleKeyInfo pressedKey = Console.ReadKey();
- HandleInput(pressedKey, CommandMoveUp, CommandMoveDown, CommandMoveLeft, CommandMoveRight, ref playerPositionX, ref playerPositionY, map, emptySymbol, moveDistance);
- }
- }
- private static void HandleInput(ConsoleKeyInfo pressedKey, ConsoleKey CommandMoveUp, ConsoleKey CommandMoveDown, ConsoleKey CommandMoveLeft, ConsoleKey CommandMoveRight, ref int positionX, ref int positionY, char[,] map, char emptySymbol, int moveDistance)
- {
- int[] direction = GetDirection(pressedKey, CommandMoveUp, CommandMoveDown, CommandMoveLeft, CommandMoveRight, moveDistance);
- int nextPositionX = positionX + direction[0];
- int nextPositionY = positionY + direction[1];
- if (map[nextPositionY, nextPositionX] == emptySymbol)
- {
- positionX = nextPositionX;
- positionY = nextPositionY;
- }
- }
- private static int[] GetDirection(ConsoleKeyInfo pressedKey, ConsoleKey CommandMoveUp, ConsoleKey CommandMoveDown, ConsoleKey CommandMoveLeft, ConsoleKey CommandMoveRight, int moveDistance)
- {
- int[] direction = { 0, 0 };
- if (pressedKey.Key == CommandMoveUp)
- {
- direction[1] = -moveDistance;
- }
- else if (pressedKey.Key == CommandMoveDown)
- {
- direction[1] = moveDistance;
- }
- else if (pressedKey.Key == CommandMoveLeft)
- {
- direction[0] = -moveDistance;
- }
- else if (pressedKey.Key == CommandMoveRight)
- {
- direction[0] = moveDistance;
- }
- return direction;
- }
- private static void DrawMap(char[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- private static void DrawPlayer(char playerSymbol, int positionX, int positionY)
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.Write(playerSymbol);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement