Public Group

# Hit bar?

This topic is 3697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to add a hit bar for the player in a 2d game I am making. How is this done usually?/What would be the best method to accomplish this? I use c++ and directx. Any info is appreciated. Thanks.

##### Share on other sites
Quote:
 Original post by AntonymI want to add a hit bar for the player in a 2d game I am making. How is this done usually?/What would be the best method to accomplish this? I use c++ and directx. Any info is appreciated.Thanks.
What do you mean by 'hit bar'? (If it's a standard term, I'm unfamiliar with it...)

Do you mean a health bar?

##### Share on other sites
yeah sorry ^_^ that rectangular bar that shortens the more you get hurt.

##### Share on other sites
Design-wise, it's really up to you, but here's one way you could do it:

- Make a 'frame' graphic (e.g. a rectangular outline) of whatever size and shape you want the health bar to be.

- For each frame (and for each health bar), first render a simple rectangle in a solid color. The 'default' size of the rectangle should be such that it shows through the frame graphic, but not outside it. The length of the rectangle is determined by the current health of the entity in question: if the health is at its maximum, you render the full length; if the health is at 0, you don't render it at all; if the health is somewhere in between, you compute the rectangle length using simply linear interpolation. Lastly, you render the frame graphic over the bar.

##### Share on other sites
Wouldn't it be better to just use a single rectangle and change the width of the rect when blitting it? This lets you make an arbitrary length bar

Making a separate rectangle for each possible health level isn't really feasible when you have say, 500 hp

##### Share on other sites
Quote:
 Original post by StoryyellerWouldn't it be better to just use a single rectangle and change the width of the rect when blitting it? This lets you make an arbitrary length barMaking a separate rectangle for each possible health level isn't really feasible when you have say, 500 hp
Was that in reference to my post? If so, I didn't say anything about using a separate rectangle for each possible health level. I basically suggested exactly what you described above: render a single rectangle in a solid color whose length is proportional to the entity's health.

##### Share on other sites
As jyk said:

___________________|===========      |-------------------

where the = is actually a single stretched rect and the frame is a fixed-length border possibly with nice graphics or just an outline.

In terms of code implementation of such a thing, depending on how you've set up your character this would be the basic idea of a decorator:
decorator pattern

In this way you could conditionally add or remove or change the health bar at run time easily. (If a player died remove it, regular display, and invulnerable display a star animation around your character. Just as an example.)

Just going based off the local character center you could relatively attach the decorator and render on draw of the character.

##### Share on other sites
Wouldn't it be easiest to scale a rectangle, and set it's width to
(health/max health) * [max bar width]
?

##### Share on other sites
Well, I think it'd be equally easy to either scale or clip it. It really depends on how you want it to look. Personally, I'd go with clipping it, but if you want the design to scale instead, go ahead.

I think it would like something like this
 SDL_Rect screenposition;SDL_Rect bar_size;bar_size.x=0;bar_size.y=0;bar_size.h=BARHEIGHT;bar_size.w=(int) ((currenthp*BARLENGTH)/maxhp);screenposition.x= XOFFSET;screenposition.y= YOFFSET;SDL_BlitSurface( hitbar, &bar_size, screen, &screenposition );

##### Share on other sites
A stretched rectangle is what happens in either case, if you're clipping anything it is the image textured on the rectangle.

Rectangle != Texture

Thought I admit I am rusty with SDL, I just use OpenGL to draw lately, but you could achieve a stretched texture or a clipped texture by specifying coordinates for the texture on the rectangle (with OpenGL). I was not discussing the actual texturing method so much as the parts that made up the health bar.

Quote:
 Wouldn't it be easiest to scale a rectangle, and set it's width to (health/max health) * [max bar width]?

That is basically what I was describing. But then you also have a background of some type that is a bit wider and taller than the maximum health amount which serves to show what the maximum is so that when you are at half health you can see relatively how much health you have to your max.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 18
• 12
• 12
• 11
• 9
• ### Forum Statistics

• Total Topics
634741
• Total Posts
3019117
×