Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main()
- {
- ConsoleKey exitKey = ConsoleKey.F1;
- ConsoleKey userInput;
- char fillElement = '#';
- char emptyElement = '_';
- char leftBorderElement = '[';
- char rightBorderElement = ']';
- int playerHealth;
- int playerMana;
- int minPercentages = 0;
- int maxPercentages = 100;
- int lengthBar = 10;
- int healthBarPositionY = 3;
- int manaBarPositionY = 4;
- bool isWorking = true;
- while (isWorking)
- {
- Console.Clear();
- Console.Write($"Введите количество здоровья до {maxPercentages}: ");
- playerHealth = Convert.ToInt32(Console.ReadLine());
- playerHealth = ClampPlayerValue(playerHealth, minPercentages, maxPercentages);
- Console.Write($"Введите количество маны до {maxPercentages}: ");
- playerMana = Convert.ToInt32(Console.ReadLine());
- playerMana = ClampPlayerValue(playerMana, minPercentages, maxPercentages);
- DrawBar(playerHealth, healthBarPositionY, lengthBar, maxPercentages, fillElement, emptyElement, leftBorderElement, rightBorderElement);
- DrawBar(playerMana, manaBarPositionY, lengthBar, maxPercentages, fillElement, emptyElement, leftBorderElement, rightBorderElement, ConsoleColor.Blue);
- Console.WriteLine($"\n\nНажмите {exitKey} чтобы выйти или любую другую клавишу, для повторного ввода значений.");
- userInput = Console.ReadKey().Key;
- if (userInput == exitKey)
- isWorking = false;
- }
- static void DrawBar(int value, int yBarPosition, int lengthBar, int maxPercentages,
- char fillElement, char emptyElement, char leftBorderElement,
- char rightBorderElement, ConsoleColor barColor = ConsoleColor.Red, ConsoleColor defaultColor = ConsoleColor.White)
- {
- Console.ForegroundColor = barColor;
- Console.SetCursorPosition(0, yBarPosition);
- Console.Write(leftBorderElement);
- int filledBarElements = value * lengthBar / maxPercentages;
- if (filledBarElements % (maxPercentages / lengthBar) != 0)
- filledBarElements++;
- for (int i = 0; i < filledBarElements; i++)
- Console.Write(fillElement);
- for (int i = filledBarElements; i < lengthBar; i++)
- Console.Write(emptyElement);
- Console.Write(rightBorderElement);
- Console.ForegroundColor = defaultColor;
- }
- static int ClampPlayerValue(int value, int minValue, int maxValue)
- {
- if (value > maxValue)
- value = maxValue;
- else if (value < minValue)
- value = minValue;
- return value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement