Skip to main content
GameDev.net gamedev.net
🔒 Locked

Health bars and such

Started by Gamester3333 Jun 30, 2006 at 10:49 PM 5 replies 1.3k views
Original Post
Gamester3333
Gamester3333
I have an RPG that will have a health bar along with a readout of how much health you have out of the maximum. I am using allegro. My code for drawing this bar is as follows:


healthpercent = playermaxhealth / playerhealth;
                           for(int i = 0; i < 16; i++)
                           {
                                   line(buffer, 66, i + 271, 66 + 890 / healthpercent, i + 271, makecol(255, 0, 0));
                           }
                           
                           textprintf_ex(buffer, font, 350, 276, makecol(255, 255, 255),
                                         -1, "Health:  %d \\ %d", playerhealth, playermaxhealth);


You can see that this is flawed right away. It works, but the rightmost position of the bar only moves every half of the bar, basically, it does this:


]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
]]]]]]]]]]]]]]]]]]]]]]]]]
]]]]]]]]]]]]]]
]]]]]]
]]]
]


It does work with any 2 non-negative values like I thought it would. It works correctly near the end, but how can I make it display the proper percentage on a bar the entire time?
HemoGloben
HemoGloben
"healthpercent = playermaxhealth / playerhealth;"

Shouldn't the percent be, health/maxhealth?

Then again, I suppose you're inverting it later with "66 + 890 / healthpercent"

....But doesn't that make it health/(maxhealth*(66+890))?
if(this.post == SATISFYING){money.send(1.00,&HemoGloben);return thanks;}
Gamester3333
Gamester3333
No, the way I have it, it works, but it rounds the float for healthpercent.

Basically, I can't divide by a float, so it only cuts the bar into halfs, then thirds, fourths, ect.

I need a better way to draw a working health bar.

This is one of the last important things left before my game is actually playable.

EDIT:

Maybe I should clarify my question.

I need a way to find a the total percentage of one number out of another (got that, I think), and draw that percentage as another percentage of one number out of another (the actual health bar).

Frequency
Frequency
OK, I've never used Allegro and I don't know exactly what all those constant numbers are, so hopefully you can translate this into Allegro:

Given PLAYER_HEALTH, PLAYER_MAX_HEALTH, HEALTHBAR_MAX_LENGTH
You'd like to find healthbarLength (that is, the red part)
float playerHealthPercentage = PLAYER_HEALTH / PLAYER_MAX_HEALTH;int healthbarLength = playerHealthPercentage * HEALTHBAR_MAX_LENGTH;allegroCallDealerThing(healthbarLength);// DO make sure that when you find playerHealthPercentage, that the first part of the division// is casted to float first so you get 0.xyz// rather than '0' or '1' (if you did integer division).// Caused ME a lot of trouble, anyway.
It only takes one mistake to wake up dead the next morning.
Gamester3333
Gamester3333
Sorry, I don't know what "casting" is, although it sounds familiar...
Frequency
Frequency
Quote:
Original post by Gamester3333
Sorry, I don't know what "casting" is, although it sounds familiar...

The long answer: Casting in C++
The short & simplified answer: converting an object of one type to another type. For example, the int 2 casted to a float would be 2.0f. A float 3.71 casted to an int would be 3 after the cast. If you divide an int by an int, you will get an int in return, and that is not what you want when storing a percentage - so if you're storing playerHealth and/or playerMaxHealth as some type of integer you'll want to write something like

playerHealthPercentage = float(playerHealth)/PLAYER_MAX_HEALTH;

So you get a decimal value for your percentage rather than a 0 or a 1.
It only takes one mistake to wake up dead the next morning.
Gamester3333
Gamester3333
Awesome! It works, plus I learned more about C++!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.