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.

Bars using height

well, i'm trying to do a custom HUD, but i have a problem:
instead of using the normal bar, (horizontal), i'm trying to do a vertical bar, but i'm having serius troubles when the bar has to update.
i know how to do it horizontal:
def draw_hp(actor, x, y)
back_image = RPG::Cache.picture("HP_Back")
cw = back_image.width
ch = back_image.height
src_rect = Rect.new(0, 0, cw, ch)
self.contents.blt(x + 65, y - ch + 30, back_image, src_rect)
image = RPG::Cache.picture("HP_Meter")
cw = image.width * actor.hp / actor.maxhp
ch = image.height
src_rect = Rect.new(0, 0, cw, ch)
self.contents.blt(x + 65, y - ch + 30, image, src_rect)
end

that would make a normal bar, which draws horizontally, but i need it to be vertical,
i don't know if i explain myself...
hope i do...can anyone help me please?
 
yeah...and thats bassically the def for the method, the one up there is in window_Base
so in anothr window i use:
draw_hp(@actor, 100, 100)
there's no "whole script" since it's added to window_base
 
Try this:
Code:
 def draw_hp(actor, x, y)
back_image = RPG::Cache.picture("HP_Back")
cw = back_image.width
ch = back_image.height
src_rect = Rect.new(0, 0, cw, ch)
self.contents.blt(x + 65, y - ch + 30, back_image, src_rect)
image = RPG::Cache.picture("HP_Meter")
cw = image.width
ch = image.height * actor.hp / actor.maxhp
src_rect = Rect.new(0, 0, cw, ch)
self.contents.blt(x + 65, y - ch + 30, image, src_rect)
end
 
i thought about that too, but when i decrese HP, the bar, instead of being replaced by the other bar, changes the Y position.
...hmm...that sound weird...
well, the empty bar goes behind the full bar, and when HP is reduced, instead of showing a proportional part of the empty bar, the full bar changes Y possition, and goes down and down and down...it's pretty weird...but can't seem to fix it.
 
mmm... try this:
Code:
def draw_hp(actor, x, y)
  back_image = RPG::Cache.picture("HP_Back")
  cw = back_image.width
  ch = back_image.height
  src_rect = Rect.new(0, 0, cw, ch)
  self.contents.blt(x + 65, y - ch + 30, back_image, src_rect)
  image = RPG::Cache.picture("HP_Meter")
  cw = image.width
  ch = image.height
  h = ch * actor.hp / actor.maxhp
  src_rect = Rect.new(0, 0, cw, h)
  self.contents.blt(x + 65, y - ch + 30, image, src_rect)
end

At least... it should fix the Y problem.
 
Ok... try this:
Code:
def draw_hp(actor, x, y)
  back_image = RPG::Cache.picture("HP_Back")
  cw = back_image.width
  ch = back_image.height
  src_rect = Rect.new(0, 0, cw, ch)
  self.contents.blt(x + 65, y - ch + 30, back_image, src_rect)
  image = RPG::Cache.picture("HP_Meter")
  cw = image.width
  ch = image.height
  h = ch * actor.hp / actor.maxhp
  src_rect = Rect.new(0, 0, cw, h)
  self.contents.blt(x + 65, y + 30 - h, image, src_rect)
end
 
This should work:
Code:
def draw_hp(actor, x, y)
  back_image = RPG::Cache.picture("HP_Back")
  cw = back_image.width
  ch = back_image.height
  src_rect = Rect.new(0, 0, cw, ch)
  self.contents.blt(x + 65, y - ch + 30, back_image, src_rect)
  image = RPG::Cache.picture("HP_Meter")
  cw = image.width
  ch = image.height
  h = ch * actor.hp / actor.maxhp
  src_rect = Rect.new(0, ch - h, cw, h)
  self.contents.blt(x + 65, y + 30 - h, image, src_rect)
end

And % is modulo, it returns the remainder of a division:
Code:
print 5 % 2  # 1
print 11 % 3  # 2
print 4 % 2  # 0
 
yay!!! it's finally working! thanks! gracias man!!!!

BTW...can you tell me what was the problem, so i can learn?
and another thing: why do you use modulo? i mean...whats the useful thing about it... =S
 
Sorry if you don't understand me.... I suck explaining :p
The problem was here:
Code:
cw = image.width
  ch = image.height
  h = ch * actor.hp / actor.maxhp
  src_rect = Rect.new(0, ch - h, cw, h)
  self.contents.blt(x + 65, y + 30 - h, image, src_rect)
ch stores the total height of the image, and h the needed height to draw the bar based on the HP.

So, I just change replace the old Y formula (y - ch + 30), to this new one:
Code:
y + 30 - h   # same as  y - ch + 30 + (ch - h)
ch - h is just draw it from the top, based on the max and min height.
And that works... but to make it works better, you should also change the image rectangle:
Code:
src_rect = Rect.new(0, ch - h, cw, h)
just by changing the "start y" from 0 to "max height - min height".

I hope... you got something :s


You can easily see if a number is divisible by other:
Code:
if number % 2 == 0
or limit a variable to a certain value:
Code:
variable = 0
loop do
  variable += 1
  variable %= 10
  break if variable > 10
end
# never will get here
 

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