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.

[XP] HP & SP Bars

Hello scripters!  I would like it if someone could explain to me how to add HP and SP bars to a HUD (I already know how to make a HUD myself).  If you have the time, I'd appreciate it if you helped me out with this!  Thanks!  :thumb:
 

khmp

Sponsor

How so? Like a bar or meter? There's actually tons of different ways I've seen. Simplest way is just draw a rectangle let's say 100 pixels long for the sake of math.

Let's say the player's health is 768 when at its maximum. To figure out how much of 100 pixels we should draw. You first need to figure what percentage of the player's current health is to his/her corresponding maximum. So let's damage the player by 60. Bringing the current health to 708. Divide that number by the maximum health.

708 / 768 = 0.92

That represents the percentage of the bar we should draw. Now let's figure out how long the bar should be. Let's multiply the width of the bar by the percentage.

100 * .92 = 92 *Phew that was tough*

That is the width of the bar that represents current health. As it is helpful to provide some point of reference you might want to draw the full bar in half opacity so there's something to compare where his health is and where it could be.

We can draw it using bitmaps' fill_rect method.
Code:
Bitmap.fill_rect(Rect, Color)

If we're in a window like with your HUD we would do something like this:

Code:
# Decide the bar width and height. These can be constants.
bar_width, bar_height = 100, 10

# Make sure the divisor is a float so the result is also a float. Not sure about that I 
# just tend to do that when the numbers are both Fixnums.
percent = $game_party.actors[0].hp / $game_party.actors[1].maxhp.to_f 

self.contents.fill_rect(Rect.new(0,0, bar_width, bar_height), Color.new(0, 0, 0, 64))
self.contents.fill_rect(Rect.new(0,0, bar_width * percent, bar_height), Color.new(200, 0, 0, 255))

[edit] Corrected because of idiocy on my part :lol:

Good luck with it AbyssalLord! :thumb:
 

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