health-o-meter

Blitz3D Forums/Blitz3D Beginners Area/health-o-meter

olo(Posted 2008) [#1]
in the code archives i once saw this bar which displays the players health in a game. Unfortunately i cant find it anymore so i dont have the code.

This feature would fit really nicely to my game so if anyone has any ideas on how to make something like this i would much appreciate it.
I have tried a few things but came up with nothing. What i am aiming for is that whenever the player collides with a certain object, the bar gets shorter. When the 'health' of the player decreases to 0 ( the bar is gone) then he loses.

thanks all


Ross C(Posted 2008) [#2]
You could use an image. And make use of drawimageblock, which draws only a part of an image.


olo(Posted 2008) [#3]
so i would make an image of a bar and each time there is a collision i delete that image and replace it with a different one?

if so, i am no sure how i would do this..

I do not know how i would hide this image and replace with another one and again and again until there is no image. I dont know how to make the you lose screen pop up when there is no image...

thanks for the idea though and if you can help me with any of these i'll be grateful ;)


big10p(Posted 2008) [#4]
No, you don't need separate images.

Just have one image for your health bar and then use DrawImageRect to draw the amount of the image that equates to how much health is left.

Say your health bar image is 300 pixels wide and the maximum health is 150: divide 300 by 150 to find how much of the image relates to a single health point. Then, when drawing the health bar image using DrawImageRect, set the width to be drawn as CurrentHealth * (300 / 150). That's the basic idea.

I'm assuming this is a 2D game, though; or is it in a 3D graphics mode?


olo(Posted 2008) [#5]
it is in 3d graphics


olo(Posted 2008) [#6]
could i use the method you told me big10p even though i am creating a 3d game?


big10p(Posted 2008) [#7]
You can, although mixing 2D with 3D isn't considered a very good idea.


olo(Posted 2008) [#8]
oh ok..so there isnt a way for me to do this? and why isnt it a good idea? will it cause problems?


Ross C(Posted 2008) [#9]
You could create a sprite, and scale it to make it smaller everytime the health goes down :o)


josk(Posted 2008) [#10]
I use the following for a fuel bar in a 2D game.
Function fuel()
Color 0,255,0
Rect 350,10,fuel,10,1
Text 250,10,"fuel  "+  fuel 
ClsColor 0,0,0
fuel=fuel-0.02
If fuel=<0.0
red()
End If
End Function

Not sure why it cant be used in 3D games. I think it may slow things down.


Ross C(Posted 2008) [#11]
Yeah generally switching between 2d and 3d incurs a slight slowdown. It can be more severe on some systems. The TEXT command is the main culprit.


olo(Posted 2008) [#12]
thanks all, but to be honest i dont understand what i should do. Couldnt i make a rectangle (scaled cube) in 3d and each time a collision occurs it gets smaller by a few pixels? If i can, then please explain the code i would use.

Thanks to all that posted ;)


Ross C(Posted 2008) [#13]
Just use a sprite instead :o)

The equation is very simple. To get a percentage out of a number:

percentage = current_number / max_number


percentage will always equal a number between 0 and 1, assuming the current number never goes below 0 and never above the maximum. Remember percentage means between 0 and 100, but i'm using this term because that's what it is basically, if you multiplied it by 100. I'm keeping it between 0 and 1 because it works best this way. Also, the number on the right, can NEVER be ZERO. In theory it never should be, if your trying to work out a percentage :o)

Me personally, i've found this to be very very very handy! Important thing to know in games programming i say.

If you haven't guessed already, this is perfect for what you need here. To somehow create a number that goes from 1 to zero, based on the numbers you have. So:

Health# = 20
Max_Health# = 100


I have made the numbers above floating point, to help generate a floating point result.

So:

percentage = Health / Max_Health

percentage = 0.2


ok, so what do you do with 0.2? Well, you would multiply the scale width of your sprite by this number. So:

Sprite_Width# = 4 ; again make this a float so the calculation will result in floating point number
Sprite_Height# = 1 ; these two dimensions will transform a square sprite into a rectangle

ScaleSprite health_sprite,Sprite_Width * percentage , Sprite_Height


This would scale your health sprite by the amount of health you have.


olo(Posted 2008) [#14]
lots of thanks to all that helped, you really helped me understand this stuff. This forum is great, post a question and you get a reply in a minute!