Advertisement
Guest User

Untitled

a guest
May 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 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 ConsoleApp3
  8. {
  9.     class MainClass
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             int health = 20;
  14.             int maxHealth = 1000;
  15.             while(true)
  16.                 HealthBar(health, maxHealth, 3, 3, ConsoleColor.Red);
  17.         }
  18.  
  19.         public static void HealthBar(int currentValue, int maxValue,  int posX, int posY, ConsoleColor color, char barchar = ' ')
  20.         {
  21.             ConsoleColor defaultColor = Console.BackgroundColor;
  22.             Console.BackgroundColor = color;
  23.             string bar = "", hpChar = Convert.ToString(currentValue * 100 / maxValue);
  24.             int barLength = 10;
  25.  
  26.             for(int i = 0; i < currentValue * barLength/maxValue; i++)
  27.             {
  28.                 if (i == barLength / 2)
  29.                 {
  30.                     bar += hpChar + '%';
  31.                 }
  32.                 else
  33.                 {
  34.                     bar += barchar;
  35.                 }
  36.                
  37.             }
  38.  
  39.             Console.SetCursorPosition(posX, posY);
  40.  
  41.             Console.BackgroundColor = defaultColor;
  42.             Console.Write('[');
  43.             Console.BackgroundColor = color;
  44.             Console.Write(bar);
  45.  
  46.             bar = "";
  47.             for (int i = currentValue * barLength/maxValue; i < barLength; i++)
  48.             {
  49.                if (i == barLength / 2)
  50.                 {
  51.                     bar += hpChar + '%';
  52.                 }
  53.                 bar += barchar;
  54.             }
  55.  
  56.            
  57.             Console.BackgroundColor = defaultColor;
  58.             Console.Write(bar);
  59.             Console.Write(']');
  60.  
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement