Advertisement
RamGaal

Homework 4 ex.2

May 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System;
  2. class MainClass
  3. {
  4.  
  5.     public static void Main(string[] args)
  6.     {
  7.        
  8.         int health = 10;
  9.         int maxHealth = 100;
  10.         int[] position = { 30, 50 };
  11.         char healthChar = '#';
  12.         bool ColorPersent = true; //переменная включает/отключает окрашивание полоски имеющихся хп
  13.                                   //и отображение значения в % при нажатии spacebar
  14.        
  15.  
  16.         while(true)
  17.         {
  18.             HealthBar(position, health, maxHealth, healthChar, ColorPersent);
  19.  
  20.            
  21.             ConsoleKeyInfo spaseDown = Console.ReadKey(true);
  22.             if (spaseDown.Key == ConsoleKey.Spacebar)
  23.             {
  24.                 break;
  25.             }
  26.             else
  27.             {
  28.                 ColorPersent = !ColorPersent;
  29.             }
  30.         }
  31.  
  32.  
  33.     }
  34.  
  35.     public static void HealthBar(int [] position, int health, int maxHealth, char healthChar, bool ColorPersent)
  36.     {
  37.         int healthBarLength = 10;
  38.         string healthBar = "";
  39.         Console.SetCursorPosition(position[0], position[1]);
  40.         string barChar = Convert.ToString(health * 100 / maxHealth);
  41.  
  42.         ConsoleColor defColor = Console.BackgroundColor;
  43.         Console.Write("(");
  44.         for (int i = 0; i < (healthBarLength - barChar.Length) / 2; i++)
  45.         {
  46.             healthBar += healthChar;
  47.         }
  48.         if (ColorPersent)
  49.         {
  50.             healthBar += barChar + "%";
  51.         }
  52.         for (int i = healthBar.Length; i < healthBarLength; i++)
  53.         {
  54.             healthBar += "_";
  55.         }
  56.         if (ColorPersent)
  57.         {
  58.             Console.BackgroundColor = ConsoleColor.DarkRed;
  59.         }
  60.         for (int i = 0; i < health * healthBarLength / maxHealth; i++)
  61.         {
  62.             Console.Write(healthBar[i]);
  63.         }
  64.         if (ColorPersent)
  65.         {
  66.             Console.BackgroundColor = ConsoleColor.Black;
  67.         }
  68.  
  69.         for (int i = health * healthBarLength / maxHealth; i < healthBarLength; i++)
  70.         {
  71.             Console.Write(healthBar[i]);
  72.         }
  73.         if (ColorPersent)
  74.         {
  75.             Console.BackgroundColor = defColor;
  76.         }
  77.  
  78.         Console.Write(")");
  79.         return;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement