# How to make healthbar size relative to hp

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?

width / max_width = hp / max_hp =>

• 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