Health Bar

Blitz3D Forums/Blitz3D Programming/Health Bar

Yue(Posted 2015) [#1]


Hi, I have an image of 128 pixels wide representing my Barar health , the data of the maximum life reaches the player's health is 100, I need to know it is how to adjust the data 100 to fit bar health is 128 pixels wide , Divide , Multiply ?


steve_ancell(Posted 2015) [#2]
The variable health can be changed in value between 0 and 100. 128 represents the width of the visible bar.
barWidth = (health / 100) * 128



Matty(Posted 2015) [#3]
Make sure you either convert health to a float or premultiply by 128 first otherwise integer division will round down to zero most of the time.

(Health * 128) / 100 will prevent this problem if you dont want to use a float.


steve_ancell(Posted 2015) [#4]
Thanks for piping up about floats Matty, I completely forgot to declare those variables as floats.

Should be more like this:
SetBuffer BackBuffer()


Repeat
	Local health# = 50
	Local barWidth# = (health# / 100) * 128
	
	Cls
	Text(10, 10, barWidth#)
	Flip
	
Until KeyHit(1)

End



steve_ancell(Posted 2015) [#5]
I just tried it with a complete range check, all seems good to go! ;-)

Graphics 1280, 1024
SetBuffer BackBuffer()


Repeat
	
	Local health#
	Local barWidth#
	
	Cls
	
	For health# = 0 To 100
		barWidth# = (health# / 100) * 128
		
		Text(10, health# * 10, barWidth#)
		
	Next
	
	Flip
	
Until KeyHit(1)

End



Yue(Posted 2015) [#6]
Thanks you! :)
DrawImagePart (imgBarraSaludVida\img%, GraphicsWidth()-75, 25,(vidaJugador# *128)/100,128)