Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

help; how to make a 'health bar'

this isn't entirely programming-related, but i figure any good programmer can probably help me out here

i'm in the process of building my GUI/HUD in darkbasic, and i've run into a bit of a snag - i am trying to make a visible 'health bar' (doesn't actually represent health, but another element in the game) that pretty much just does what health bars do (fill, and drain in accordance to the health variables). unfortunately i am terrible with math, and i'm having a hard time trying to come up with a process/algorithm to translate these health variables into a visual health bar.

the health bar in question is 158 pixels wide, and i'm using darkbasic's primitive 'draw box' functions to represent it (to good effect, mind you), so what i need to achieve is a process that can get an accurate percentage of [current health] in relation to [max health], and somehow translate that further into a whole number between 0 and 158, 0 being 0% and 158 being 100%. :l

btw, just so i'm clear, it's not the syntax i'm having any trouble with. i'm efficient with darkbasic, i just dont know how to pull off these kinds of functions with basic math. i'm giving myself a huge headache trying to figure this one out, so if anyone can help in any way i'd appreciate it very much!

here's a mock-up of the meter in question, for whatever it's worth;
voltbardg1.png
 

Wichu

Member

Do you just want a formula for the width of the health bar? If so, try (currentHP*158)/totalHP. The percentage is similar; (currentHP*100)/totalHP (if you need it). Hope this helps!
 
In practice, Wichu's formula will always yield better results. Even though mathematically they are the same, depending on the language / interpreter, they could return different answers. CurrentHP & TotalHP are most likely integers. Unless you explicitly cast them as floating points, CurrentHP / TotalHP could return an integer, which would get rounded to either 0 or 1, yielding an result of 0% or 100% only.
Darkbasic might do automatic casting, but it's still a good practice to multiply first, then divide.

Be Well
 
you guys are awesome <3 ,the percentage problem was going to be my next question!

and DB does have a function to round numbers to whole numbers when dealing with integers, so it's all good. these formulas are working just wonderfully for me! :}
 
But how would you calculate it out if you want it a specific width? Such as MMORPGS have a bar that stays the same size but goes down slower.
 
darknesscloud":2sk7nvh2 said:
But how would you calculate it out if you want it a specific width? Such as MMORPGS have a bar that stays the same size but goes down slower.

(CurrentHP * BarWidth) / TotalHP

to get how long the bar should be... the bar is probably going slower because it's short
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top