Advertisement
MayloGreen

DZ_functions_DrawBar

Dec 7th, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DZ_functions_DrawBar
  4. {
  5.     class Program
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             ConsoleKeyInfo key;
  10.             ConsoleColor red = ConsoleColor.Red;
  11.             ConsoleColor blue = ConsoleColor.Blue;
  12.  
  13.             int maxValue = 10;
  14.             int dividerInterestBar = 10;
  15.             int positionY = 54;
  16.             int positionX = 8;
  17.             int cursorOffset = 2;
  18.             string healthBar = "Health Bar";
  19.             string manaBar = "Mana Bar";
  20.             string barArray = "";
  21.  
  22.             do
  23.             {
  24.                 Console.WriteLine("Добро пожаловать!!!");
  25.                 Console.Write("\nДанная программа рисует Health Bar и Mana Bar.\nдля выхода из программы нажмите клавишу ESC, для продолжения нажмите клавишу Enter.");
  26.                 Console.WriteLine("\nДля ввода принамаются слудующие значения в процентах: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.");
  27.                 Console.WriteLine("\nНажмите клавишу Enter для продолжения или Escape для выхода из программы: ");
  28.  
  29.                 key = Console.ReadKey();
  30.  
  31.                 switch (key.Key)
  32.                 {
  33.                     case ConsoleKey.Enter:
  34.                         DrawBar(barArray, maxValue, dividerInterestBar, positionY, positionX, cursorOffset, healthBar, red);
  35.                         DrawBar(barArray, maxValue, dividerInterestBar, positionY - cursorOffset, positionX + cursorOffset, cursorOffset, manaBar, blue);
  36.                         break;
  37.  
  38.                     default:
  39.                         if (key.Key != ConsoleKey.Escape)
  40.                         {
  41.                             Console.Clear();
  42.                             Console.WriteLine("Неверный ввод!!! Повторите ввод.");
  43.                         }
  44.                         break;
  45.                 }
  46.  
  47.                 Console.Write("\nНажмите любую клавишу для продолжения...");
  48.                 Console.ReadKey();
  49.  
  50.                 Console.Clear();
  51.  
  52.             } while (key.Key != ConsoleKey.Escape);
  53.         }
  54.  
  55.         static void DrawBar(string barArray, int maxValue, int dividerInterestBar, int positionY, int positionX, int cursorOffset, string bar, ConsoleColor color)
  56.         {
  57.             int interestBar;
  58.             string tempBar = bar;
  59.  
  60.             Console.Write($"\nНа сколько процентов {tempBar} вы хотите заполнить?: ");
  61.             interestBar = Convert.ToInt32(Console.ReadLine());
  62.  
  63.             if (interestBar >= 0 & interestBar < 101 & interestBar == 10 | interestBar == 20 | interestBar == 30 | interestBar == 40 | interestBar == 50 | interestBar == 60 | interestBar == 70 | interestBar == 80 | interestBar == 90 | interestBar == 100)
  64.             {
  65.                 OutputBar(barArray, interestBar, color, maxValue, dividerInterestBar, positionY, positionX);
  66.             }
  67.  
  68.             else
  69.             {
  70.                 Console.SetCursorPosition(positionY, positionX);
  71.                 Console.Write("Неверный ввод!!! Повторите ввод позже.\n");
  72.             }
  73.         }
  74.  
  75.         static void OutputBar(string bar, int interestBar, ConsoleColor color, int maxValue, int dividerInterestBar, int positionY, int positionX)
  76.         {
  77.             int numberIterations = interestBar / dividerInterestBar;
  78.  
  79.             for (int i = 0; i < numberIterations; i++)
  80.                 bar += '_';
  81.  
  82.             Console.SetCursorPosition(positionY, positionX);
  83.             Console.Write('[');
  84.             Console.BackgroundColor = color;
  85.             Console.Write(bar);
  86.             ConsoleColor defaultColor = ConsoleColor.Black;
  87.             Console.BackgroundColor = defaultColor;
  88.  
  89.             bar = "";
  90.  
  91.             for (int i = numberIterations; i < maxValue; i++)
  92.                 bar += '_';
  93.  
  94.             Console.WriteLine(bar + ']');
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement