Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.