Advertisement
TwinFrame

Clight_29_UIElement

May 13th, 2023 (edited)
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         ConsoleKey exitKey = ConsoleKey.F1;
  6.  
  7.         ConsoleKey userInput;
  8.  
  9.         char fillElement = '#';
  10.         char emptyElement = '_';
  11.         char leftBorderElement = '[';
  12.         char rightBorderElement = ']';
  13.  
  14.         int playerHealth;
  15.         int playerMana;
  16.  
  17.         int minPercentages = 0;
  18.         int maxPercentages = 100;
  19.         int lengthBar = 10;
  20.         int healthBarPositionY = 3;
  21.         int manaBarPositionY = 4;
  22.  
  23.         bool isWorking = true;
  24.  
  25.         while (isWorking)
  26.         {
  27.             Console.Clear();
  28.  
  29.             Console.Write($"Введите количество здоровья до {maxPercentages}: ");
  30.             playerHealth = Convert.ToInt32(Console.ReadLine());
  31.             playerHealth = ClampPlayerValue(playerHealth, minPercentages, maxPercentages);
  32.  
  33.             Console.Write($"Введите количество маны до {maxPercentages}: ");
  34.             playerMana = Convert.ToInt32(Console.ReadLine());
  35.             playerMana = ClampPlayerValue(playerMana, minPercentages, maxPercentages);
  36.  
  37.             DrawBar(playerHealth, healthBarPositionY, lengthBar, maxPercentages, fillElement, emptyElement, leftBorderElement, rightBorderElement);
  38.             DrawBar(playerMana, manaBarPositionY, lengthBar, maxPercentages, fillElement, emptyElement, leftBorderElement, rightBorderElement, ConsoleColor.Blue);
  39.  
  40.             Console.WriteLine($"\n\nНажмите {exitKey} чтобы выйти или любую другую клавишу, для повторного ввода значений.");
  41.  
  42.             userInput = Console.ReadKey().Key;
  43.  
  44.             if (userInput == exitKey)
  45.                 isWorking = false;
  46.         }
  47.  
  48.         static void DrawBar(int value, int yBarPosition, int lengthBar, int maxPercentages,
  49.             char fillElement, char emptyElement, char leftBorderElement,
  50.             char rightBorderElement, ConsoleColor barColor = ConsoleColor.Red, ConsoleColor defaultColor = ConsoleColor.White)
  51.         {
  52.             Console.ForegroundColor = barColor;
  53.             Console.SetCursorPosition(0, yBarPosition);
  54.             Console.Write(leftBorderElement);
  55.  
  56.             int filledBarElements = value * lengthBar / maxPercentages;
  57.  
  58.             if (filledBarElements % (maxPercentages / lengthBar) != 0)
  59.                 filledBarElements++;
  60.  
  61.             for (int i = 0; i < filledBarElements; i++)
  62.                 Console.Write(fillElement);
  63.  
  64.             for (int i = filledBarElements; i < lengthBar; i++)
  65.                 Console.Write(emptyElement);
  66.  
  67.             Console.Write(rightBorderElement);
  68.  
  69.             Console.ForegroundColor = defaultColor;
  70.         }
  71.  
  72.         static int ClampPlayerValue(int value, int minValue, int maxValue)
  73.         {
  74.             if (value > maxValue)
  75.                 value = maxValue;
  76.             else if (value < minValue)
  77.                 value = minValue;
  78.  
  79.             return value;
  80.         }
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement