Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MainClass
- {
- public static void Main(string[] args)
- {
- int health = 10;
- int maxHealth = 100;
- int[] position = { 30, 50 };
- char healthChar = '#';
- bool ColorPersent = true; //переменная включает/отключает окрашивание полоски имеющихся хп
- //и отображение значения в % при нажатии spacebar
- while(true)
- {
- HealthBar(position, health, maxHealth, healthChar, ColorPersent);
- ConsoleKeyInfo spaseDown = Console.ReadKey(true);
- if (spaseDown.Key == ConsoleKey.Spacebar)
- {
- break;
- }
- else
- {
- ColorPersent = !ColorPersent;
- }
- }
- }
- public static void HealthBar(int [] position, int health, int maxHealth, char healthChar, bool ColorPersent)
- {
- int healthBarLength = 10;
- string healthBar = "";
- Console.SetCursorPosition(position[0], position[1]);
- string barChar = Convert.ToString(health * 100 / maxHealth);
- ConsoleColor defColor = Console.BackgroundColor;
- Console.Write("(");
- for (int i = 0; i < (healthBarLength - barChar.Length) / 2; i++)
- {
- healthBar += healthChar;
- }
- if (ColorPersent)
- {
- healthBar += barChar + "%";
- }
- for (int i = healthBar.Length; i < healthBarLength; i++)
- {
- healthBar += "_";
- }
- if (ColorPersent)
- {
- Console.BackgroundColor = ConsoleColor.DarkRed;
- }
- for (int i = 0; i < health * healthBarLength / maxHealth; i++)
- {
- Console.Write(healthBar[i]);
- }
- if (ColorPersent)
- {
- Console.BackgroundColor = ConsoleColor.Black;
- }
- for (int i = health * healthBarLength / maxHealth; i < healthBarLength; i++)
- {
- Console.Write(healthBar[i]);
- }
- if (ColorPersent)
- {
- Console.BackgroundColor = defColor;
- }
- Console.Write(")");
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement