Advertisement
SaNik74

Edit bars

May 1st, 2024 (edited)
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. internal class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         string nameHealthBar = "Health";
  6.         string nameArmorBar = "Armor";
  7.         string nameStaminaBar = "Stamina";
  8.         string nameAmmoBar = "Ammo";
  9.         int percentHealth = 60;
  10.         int percentArmor = 100;
  11.         int percentStamina = 20;
  12.         int percentAmmo = 80;
  13.  
  14.         DrawBar(nameHealthBar, percentHealth, ConsoleColor.Red, 0, 0);
  15.         DrawBar(nameArmorBar, percentArmor, ConsoleColor.DarkYellow, 12, 0);
  16.         DrawBar(nameStaminaBar, percentStamina, ConsoleColor.DarkBlue, 0, 3);
  17.         DrawBar(nameAmmoBar, percentAmmo, ConsoleColor.DarkGray, 12, 3);
  18.     }
  19.  
  20.     static void DrawBar(string nameBar, int precentOccupancy, ConsoleColor color, int positionX, int positionY, int maxValue = 10)
  21.     {
  22.         int fullLineInPercent = 100;
  23.         int startingLinePosition = 0;
  24.  
  25.         if (precentOccupancy > fullLineInPercent)
  26.         {
  27.             precentOccupancy = fullLineInPercent;
  28.         }
  29.  
  30.         if (precentOccupancy < 0)
  31.         {
  32.             precentOccupancy = startingLinePosition;
  33.         }
  34.  
  35.         ConsoleColor defaultColour = Console.BackgroundColor;
  36.         string bar = "";
  37.         int occupancyValue = maxValue * precentOccupancy / fullLineInPercent;
  38.  
  39.         DrawBitOfBar(startingLinePosition, occupancyValue, ref bar);
  40.  
  41.         Console.SetCursorPosition(positionX, positionY);
  42.         Console.Write('[');
  43.         Console.BackgroundColor = color;
  44.         Console.Write(bar);
  45.         Console.BackgroundColor = defaultColour;
  46.         bar = "";
  47.  
  48.         DrawBitOfBar(occupancyValue, maxValue, ref bar);
  49.  
  50.         Console.Write($"{bar}]");
  51.         Console.SetCursorPosition(positionX + 3, positionY + 1);
  52.         Console.Write(nameBar);
  53.     }
  54.  
  55.     static void DrawBitOfBar(int startingPositionOfLine, int endOfLine, ref string bar)
  56.     {
  57.         for (int i = startingPositionOfLine; i < endOfLine; i++)
  58.         {
  59.             bar += ' ';
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement