Advertisement
RedFlys

Home work - draw bar

Nov 5th, 2021
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int maxHealth = 100;
  14.             int health = 55;
  15.             int healthPositionY = 0;
  16.             ConsoleColor healthColor = ConsoleColor.Red;
  17.  
  18.             int maxMana = 200;
  19.             int mana = 2000;
  20.             int manaPositionY = 1;
  21.             ConsoleColor manaColor = ConsoleColor.Blue;
  22.  
  23.             DrawBar(health, maxHealth, healthPositionY, healthColor);
  24.             DrawBar(mana, maxMana, manaPositionY, manaColor);
  25.  
  26.             Console.ReadKey();
  27.            
  28.         }
  29.  
  30.         static void DrawBar(int value, int maxValue, int positionY, ConsoleColor backgroundColor)
  31.         {
  32.             Console.SetCursorPosition(0, positionY);
  33.  
  34.             if (CheckValueInvalid(value, maxValue))
  35.             {
  36.                 Console.WriteLine("Ошибка данных!");
  37.                 return;
  38.             }
  39.  
  40.             string bar;
  41.             ConsoleColor defaulthColor = Console.BackgroundColor;
  42.             int percentValue = value * 10 / maxValue;
  43.  
  44.             bar = CalculateBar(0, percentValue);
  45.  
  46.             Console.Write("[");
  47.             Console.BackgroundColor = backgroundColor;
  48.             Console.Write(bar);
  49.  
  50.             bar = CalculateBar(percentValue, 10);
  51.  
  52.             Console.BackgroundColor = defaulthColor;
  53.             Console.Write(bar);
  54.             Console.Write("]");
  55.         }
  56.  
  57.         static string CalculateBar (int startValue, int endValue)
  58.         {
  59.             string bar = "";
  60.             char barElement = ' ';
  61.  
  62.             for (int i = startValue; i < endValue; i++)
  63.             {
  64.                 bar += barElement;
  65.             }
  66.  
  67.             return bar;
  68.         }
  69.  
  70.         static bool CheckValueInvalid(int value, int maxValue)
  71.         {
  72.             if (value < 0 || value > maxValue)
  73.             {
  74.                 return true;
  75.             }
  76.             else
  77.             {
  78.                 return false;
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement