Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace ConsoleApp1
- {
- enum direction { left, right, up, down}
- class Program
- {
- static direction currentDirection = direction.down;
- static (int x, int y) currentPosition = (0, 0);
- static ConsoleKey lastKeyPressed = ConsoleKey.Enter;
- static void Main(string[] args)
- {
- // выключаем курсор
- Console.CursorVisible = false;
- direction? userInput = null;
- for (; ; )
- {
- switch (lastKeyPressed)
- {
- case ConsoleKey.W:
- userInput = direction.up;
- break;
- case ConsoleKey.S:
- userInput = direction.down;
- break;
- case ConsoleKey.A:
- userInput = direction.left;
- break;
- case ConsoleKey.D:
- userInput = direction.right;
- break;
- }
- currentPosition = Move(userInput, currentPosition);
- Thread.Sleep(500);
- // условие из-за которого все работает
- if (Console.KeyAvailable)
- {
- lastKeyPressed = Console.ReadKey().Key;
- }
- }
- }
- static (int x, int y) Move (direction? dir, (int x, int y) currentPos)
- {
- int x = currentPos.x, y = currentPos.y;
- Console.SetCursorPosition(x, y);
- Console.Write('\0');
- if (dir is null)
- {
- dir = currentDirection;
- }
- else
- {
- currentDirection = dir.Value;
- }
- switch (dir.Value)
- {
- case direction.left:
- x--;
- break;
- case direction.up:
- y--;
- break;
- case direction.down:
- y++;
- break;
- case direction.right:
- x++;
- break;
- }
- Console.SetCursorPosition(x, y);
- Console.Write('X');
- Console.SetCursorPosition(x, y);
- return (x, y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement