Advertisement
Suslick

Untitled

Jul 8th, 2023 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1.         static void Main(string[] args)
  2.         {
  3.             char symbolHelthBar = ' ';
  4.             int positionX = 10;
  5.             int positionY = 10;
  6.             double percentHP = 0;
  7.  
  8.             Console.WriteLine("Введите процент жизней.");
  9.             percentHP = double.Parse(Console.ReadLine());
  10.  
  11.             Console.WriteLine("Введите позицию по X");
  12.             positionX = int.Parse(Console.ReadLine());
  13.  
  14.             Console.WriteLine("Введите позицию по Y");
  15.             positionY = int.Parse(Console.ReadLine());
  16.  
  17.             symbolHelthBar = GetSymbol();
  18.  
  19.             RenderHealthBar(percentHP, symbolHelthBar, positionX, positionY);
  20.         }
  21.  
  22.         private static void RenderHealthBar(double percentHP, char symbolHealthBar, int positionX, int positionY, int lengthBar = 10, char openBracket = '[', char closeBracket = ']', char symbolEmptyCells = '_')
  23.         {
  24.             Console.Clear();
  25.             Console.SetCursorPosition(positionX, positionY);
  26.             string bar = string.Empty;
  27.             int maxPercent = 100;
  28.             int minPercent = 0;
  29.  
  30.             if (percentHP == 0)
  31.             {
  32.                 bar = FillEmptyCells(bar,lengthBar);
  33.  
  34.                 Console.WriteLine(openBracket + bar + closeBracket);
  35.             }
  36.             else if (percentHP > minPercent && percentHP <= maxPercent)
  37.             {
  38.                 int countHP = (int)Math.Round(lengthBar * (percentHP / maxPercent));
  39.  
  40.                 bar = FillEmptyCells(bar, countHP, symbolHealthBar);
  41.                 bar = FillEmptyCells(bar, lengthBar - bar.Length);
  42.  
  43.                 Console.WriteLine(openBracket + bar + closeBracket);
  44.             }
  45.             else
  46.             {
  47.                 Console.WriteLine("Вы ввели некорректное число");
  48.             }
  49.         }
  50.  
  51.         private static char GetSymbol()
  52.         {
  53.             char symbol = ' ';
  54.  
  55.             Console.WriteLine("Введите символ для Healthbar");
  56.  
  57.             while (!char.TryParse(Console.ReadLine(), out symbol))
  58.             {
  59.                 Console.WriteLine("Неверный ввод");
  60.             }
  61.  
  62.             return symbol;
  63.         }
  64.  
  65.         private static string FillEmptyCells(string bar, int lengthBar, char symbolEmptyCells = '_')
  66.         {
  67.             for (int i = 0; i < count; i++)
  68.             {
  69.                 bar += symbol;
  70.             }
  71.  
  72.             return bar;
  73.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement