Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     enum direction { left, right, up, down}
  7.     class Program
  8.     {
  9.         static direction currentDirection = direction.down;
  10.         static (int x, int y) currentPosition = (0, 0);
  11.         static ConsoleKey lastKeyPressed = ConsoleKey.Enter;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             // выключаем курсор
  16.             Console.CursorVisible = false;
  17.  
  18.             direction? userInput = null;
  19.             for (; ; )
  20.             {
  21.                 switch (lastKeyPressed)
  22.                 {
  23.                     case ConsoleKey.W:
  24.                         userInput = direction.up;
  25.                         break;
  26.                     case ConsoleKey.S:
  27.                         userInput = direction.down;
  28.                         break;
  29.                     case ConsoleKey.A:
  30.                         userInput = direction.left;
  31.                         break;
  32.                     case ConsoleKey.D:
  33.                         userInput = direction.right;
  34.                         break;
  35.                 }
  36.                 currentPosition = Move(userInput, currentPosition);
  37.                 Thread.Sleep(500);
  38.                 // условие из-за которого все работает
  39.                 if (Console.KeyAvailable)
  40.                 {
  41.                     lastKeyPressed = Console.ReadKey().Key;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         static (int x, int y) Move (direction? dir, (int x, int y) currentPos)
  47.         {
  48.             int x = currentPos.x, y = currentPos.y;
  49.  
  50.             Console.SetCursorPosition(x, y);
  51.             Console.Write('\0');
  52.  
  53.             if (dir is null)
  54.             {
  55.                 dir = currentDirection;
  56.             }
  57.             else
  58.             {
  59.                 currentDirection = dir.Value;
  60.             }
  61.                    
  62.             switch (dir.Value)
  63.             {
  64.                 case direction.left:
  65.                     x--;
  66.                     break;
  67.                 case direction.up:
  68.                     y--;
  69.                     break;
  70.                 case direction.down:
  71.                     y++;
  72.                     break;
  73.                 case direction.right:
  74.                     x++;
  75.                     break;
  76.             }
  77.  
  78.             Console.SetCursorPosition(x, y);
  79.             Console.Write('X');
  80.             Console.SetCursorPosition(x, y);
  81.             return (x, y);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement