-2

I'm making a text adventure I'm using

def do_health
    print health,"/ 200"

to display health but I want to convert it to a percentage and print something like

|----------          |
         50%

depending on what percentage of health the player has left but I can't find anything elsewhere on making a health bar for idle.

Thanks in advance.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nathbesspop
  • 5
  • 1
  • 4
  • You may be able to use some of the solutions here: https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console – PM 2Ring Dec 30 '17 at 15:45

1 Answers1

1

All that needs to be done is a few simple conversion to convert your current health into a number of dashes and define the max number of dashes (20 in this case: healthDashes) to be equivalent to your max health (200: maxHealth).

Consider you have 80 health left. So for example if we take healthDashes(20)/maxHealth(200) you get 10, this is the value that we divide our health by to convert it into the number of dashes that we want. You can then take your current health being 80 and the number of dashes is: 80/10 => 8 dashes. The percentage is straight forward: (health(80)/maxHealth(200))*100 = > 40 percent.

Now in python you just apply that lodic above and you will get:

health = 80.0      # Current Health (float so division doesn't make an int)
maxHealth = 200    # Max Health
healthDashes = 20  # Max Displayed dashes

def do_health():
  dashConvert = int(maxHealth/healthDashes)            # Get the number to divide by to convert health to dashes (being 10)
  currentDashes = int(health/dashConvert)              # Convert health to dash count: 80/10 => 8 dashes
  remainingHealth = healthDashes - currentDashes       # Get the health remaining to fill as space => 12 spaces

  healthDisplay = '-' * currentDashes                  # Convert 8 to 8 dashes as a string:   "--------"
  remainingDisplay = ' ' * remainingHealth             # Convert 12 to 12 spaces as a string: "            "
  percent = str(int((health/maxHealth)*100)) + "%"     # Get the percent as a whole number:   40%

  print("|" + healthDisplay + remainingDisplay + "|")  # Print out textbased healthbar
  print("         " + percent)                         # Print the percent

If you call the method you get the result:

do_health()
>
|--------            |
         40%

Here are a few more examples with changing the value of health:

|----------          |  # health = 100
         50%
|--------------------|  # health = 200
         100%
|                    |  # health = 0
         0%
|------              |  # health = 68
         34%
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • Instead of using `''.join(['-' for i in range(currentDashes)])`, it would be cleaner to do `'-'*currentDashes`. – Joseph Camacho Aug 17 '19 at 02:29
  • @JosephCamacho Good point, this was a few years back so it was probably a bit more rusty back then :P. I've also changed the `health` so it's a float due to how Python 2 handles integer division since I noticed OP tagged it in the question. Now it should have the correct answer in 2 and 3. – Spencer Wieczorek Aug 17 '19 at 02:53