formatting on screen

Blitz3D Forums/Blitz3D Beginners Area/formatting on screen

ColeE(Posted 2015) [#1]
I wrote a function to draw health bars on the screen but I must me making a mistake that I just cannot catch. Maybe it's some sort of round off error, but I'm not sure. playerShip(myCurrentShip%)\shieldStrength%) is the total health that the ship may have, and playerHealth% is the current health. As you should be able to tell, right now I'm only drawing the left bar, I'll worry about the right one one I figure out the math for the left one, which is obviously going to be the same.

Function drawHealthBars()
	Color 0, 0, 0
	Rect 10, 10, GraphicsWidth()/20, GraphicsHeight()-20
	Rect GraphicsWidth()-(GraphicsWidth()/20)-20, 10, GraphicsWidth()/20, GraphicsHeight()-20
	Color 0, 60, 255
	Rect 15, 15 + ((GraphicsHeight()-30)/playerShip(myCurrentShip%)\shieldStrength%)*((playerShip(myCurrentShip%)\shieldStrength%)-playerHealth%), (GraphicsWidth()/20)-10, GraphicsHeight() - 30 -  ((GraphicsHeight()-30)/playerShip(myCurrentShip%)\shieldStrength%)*playerHealth%
	Color 255, 255, 255 
	Text 10 + GraphicsWidth()/40, GraphicsHeight()/2, playerHealth%, True, True
End Function



Stevie G(Posted 2015) [#2]
It's hard to tell as your function is a bit of a mess. It would be better to simplify it.

If you are going to be positioning hud stuff relative to the screen resolution it's sometimes best to store the graphics width and height in a shorter variable for future reference.

It would also make it easier to visualise by assigning variables for the various components too.

Example below, hit space to animate the health bar.

Graphics 1024,768,32,1

Global GW = GraphicsWidth()
Global GH = GraphicsHeight()
SetBuffer BackBuffer()

Global PlayerHealth% = 100
Global SheildStrength% = 100

While Not KeyHit(1)

	Cls
	
	If KeyHit(57) PlayerHealth = Rand( 0,100 )
	
	DRAWhealthBar()
	Flip
	
Wend

Function DRAWhealthBar()

	Local PosX, PosY, MaxY, BarX, BarY

	;Set bottom left position of bar
	PosX = 20
	PosY = GH - 20

	;Set max pixel height of bar assuming 100% health
	MaxY = 200

	;Set bar width (assume this doesn't change)
	BarX = 20
	
	;Draw bar background
	Color 64,64,64
	Rect PosX, PosY - MaxY, BarX, MaxY
	
	;Calculate bar height based on health
	BarY = MaxY * ( PlayerHealth / Float(SheildStrength) )
	
	;Draw health bar
	Color 255,0,0
	Rect PosX, PosY - BarY, BarX, BarY


End Function


If this makes sense to you then you should be able to substitute your own variables.

Stevie