Nemo048

Untitled

Aug 16th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace lesson2_4potok
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = false;
  14.             int X = 0, Y = 0;
  15.  
  16.             //у пользователя есть координаты X Y, необходимо сделать так, чтобы при нажатии клавиш WASD
  17.             //происходило изменение координат пользователя и каждую итерацию необходимо выводить эти координаты
  18.             //в консоль
  19.  
  20.             //усовершенствовать этот код, чтобы не выбрасывало исключение при выходе за границы консоли
  21.  
  22.             ConsoleKey playerKey = Console.ReadKey(true).Key;
  23.            
  24.             while(playerKey != ConsoleKey.Escape)
  25.             {
  26.                 switch(playerKey)
  27.                 {
  28.                     case ConsoleKey.A:
  29.                         X--;
  30.                         break;
  31.                     case ConsoleKey.W:
  32.                         Y--;
  33.                         break;
  34.                     case ConsoleKey.D:
  35.                         X++;
  36.                         break;
  37.                     case ConsoleKey.S:
  38.                         Y++;
  39.                         break;
  40.                 }
  41.  
  42.                 Console.Clear();
  43.                 Console.SetCursorPosition(X, Y);
  44.                 Console.WriteLine("{0} {1}", X, Y);
  45.  
  46.                 playerKey = Console.ReadKey(true).Key;
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment