Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Like in most games with healthbars, the units in my game have a hp_bar object which is a frame, with a slightly smaller rectangle inside which should get smaller (and change color) as a unit loses hp, but I'm unsure how to work the math which will make the rectangle size an integer which represents an percentage rather than the actual hp.

Say the hp bar is 40 pixels in width, and hp is some arbitrary integer amount, what is the python magic to convert the hp to hp bar width?

share|improve this question
up vote 2 down vote accepted

Well, consider the following:

The ratio between the max width of the bar and the real width of the bar is the same as the ratio between max HP and HP; so:

width / max_width = hp / max_hp =>

width = hp / max_hp * max_width
share|improve this answer
ie (hp/max_hp) is % hp. %hp * max_width gives width in pixels that hp bar should be. – bobobobo Apr 27 '13 at 15:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.