Skip to content

Instantly share code, notes, and snippets.

@Choos37ricK
Last active February 10, 2025 15:33
Show Gist options
  • Save Choos37ricK/a7901685813e56061915e194432f9265 to your computer and use it in GitHub Desktop.
Save Choos37ricK/a7901685813e56061915e194432f9265 to your computer and use it in GitHub Desktop.
namespace functions;
class Program
{
static void Main(string[] args)
{
Console.Clear();
string labelHealth = "Health";
string labelMana = "Mana";
int maxLabelLength = labelHealth.Length > labelMana.Length ? labelHealth.Length : labelMana.Length;
int barsStartPositionX = maxLabelLength + 1;
int barHealthStartPositionY = 0;
int barManaStartPositionY = 1;
int barHealthFillPercentage = 50;
int barHealthMaxLength = 20;
int barManaFillPercentage = 90;
int barManaMaxLength = 20;
DrawBarInConsole(labelHealth, barHealthFillPercentage, barHealthMaxLength, ConsoleColor.Green, barsStartPositionX, barHealthStartPositionY, '#');
DrawBarInConsole(labelMana, barManaFillPercentage, barManaMaxLength, ConsoleColor.Blue, barsStartPositionX, barManaStartPositionY, '$');
}
static void DrawBarInConsole(string barLabel, int fillPercentage, int maxLength, ConsoleColor color, int positionX, int positionY, char fillSymbol = ' ')
{
char emptyFillSymbol = ' ';
Console.Write($"{barLabel}: ");
Console.SetCursorPosition(positionX, positionY);
Console.Write("[");
Console.BackgroundColor = color;
int percentageAsLength = maxLength * fillPercentage / 100;
DrawSymbol(fillSymbol, percentageAsLength);
Console.ResetColor();
DrawSymbol(emptyFillSymbol, maxLength, percentageAsLength);
Console.WriteLine("]");
}
static void DrawSymbol(char symbol, int count, int startFrom = 0)
{
for (int i = startFrom; i < count; i++)
{
Console.Write(symbol);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment