Advertisement
MeGaLiGhT14

5.3 Render

Oct 26th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lesson5_3
  4. {
  5.     public static class Render
  6.     {
  7.         public static void RenderUnit(int positionX , int positionY , char unit , ConsoleColor color = ConsoleColor.DarkGray)
  8.         {
  9.             Console.SetCursorPosition(positionX , positionY);
  10.             Console.ForegroundColor = color;
  11.             Console.Write(unit);
  12.             Console.ResetColor();
  13.         }
  14.     }
  15.  
  16.     public class Unit
  17.     {
  18.         public int PositionX { get; private set; }
  19.         public int PositionY { get; private set; }
  20.  
  21.         public char UnitChar { get; private set; }
  22.  
  23.         public Unit(char unitChar , int positionX = 0 , int positionY = 0)
  24.         {
  25.             PositionX = positionX;
  26.             PositionY = positionY;
  27.             UnitChar = unitChar;
  28.         }
  29.  
  30.         public void StepUnit(int stepX , int stepY)
  31.         {
  32.             PositionX += stepX;
  33.             PositionY += stepY;
  34.         }
  35.     }
  36.  
  37.     class Program
  38.     {
  39.         static void Main(string[] args)
  40.         {
  41.             Unit unit = new Unit('?' , 9 , 9);
  42.  
  43.             bool end = false;
  44.  
  45.             Console.CursorVisible = false;
  46.  
  47.             do
  48.             {
  49.                 Render.RenderUnit(unit.PositionX , unit.PositionY , unit.UnitChar , ConsoleColor.Green);
  50.  
  51.                 ConsoleKeyInfo userInput = Console.ReadKey();
  52.  
  53.                 switch (userInput.Key)
  54.                 {
  55.                     case ConsoleKey.UpArrow:
  56.                         unit.StepUnit(0 , -1);
  57.                         break;
  58.                     case ConsoleKey.DownArrow:
  59.                         unit.StepUnit(0 , 1);
  60.                         break;
  61.                     case ConsoleKey.LeftArrow:
  62.                         unit.StepUnit(-1 , 0);
  63.                         break;
  64.                     case ConsoleKey.RightArrow:
  65.                         unit.StepUnit(1 , 0);
  66.                         break;
  67.                     case ConsoleKey.Spacebar:
  68.                     case ConsoleKey.Enter:
  69.                     case ConsoleKey.Escape:
  70.                         end = true;
  71.                         break;
  72.                 }
  73.                 Console.Clear();
  74.             }
  75.             while (!end);
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement