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 and sp over/under charecter

ISMY

Member

Hi im making a orpg with rmxp and im 57 percent done.I need a hud thats right under or over
the character if possible.

Thanks in advanced.
 

khmp

Sponsor

I got ya covered give me an hour or so.

Code:
#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs map screen processing.
#==============================================================================

class Scene_Map
  #--------------------------------------------------------------------------
  # * Constant Variables
  #--------------------------------------------------------------------------
  BAR_LENGTH = 32
  BAR_HEIGHT = 6
  OVER_PLAYER = true
  OPACITY = 128
  #--------------------------------------------------------------------------
  # * Constant Variables
  #--------------------------------------------------------------------------
  attr_accessor :hpsp_visible
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias_method :ismy_hpsphud_scene_map_main, :main
  alias_method :ismy_hpsphud_scene_map_update, :update
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # By default the HUD is visible.
    @hpsp_visible = true
    
    # Create are sprite and its bitmap as well as adjusting the z to always
    # visible.
    @hpsp_bar = Sprite.new
    @hpsp_bar.bitmap = Bitmap.new(BAR_LENGTH + 2, BAR_HEIGHT * 2 + 3)
    @hpsp_bar.opacity = OPACITY
    @hpsp_bar.z = 999
    
    # Top Bar
    @hpsp_bar.bitmap.fill_rect(0, 0, 
      BAR_LENGTH + 2, 1, Color.new(0, 0, 0))
    # Left Bar
    @hpsp_bar.bitmap.fill_rect(0, 0, 
      1, BAR_HEIGHT * 2 + 3, Color.new(0, 0, 0))
    # Bottom Bar
    @hpsp_bar.bitmap.fill_rect(0, BAR_HEIGHT * 2 + 2, 
      BAR_LENGTH + 2, 1, Color.new(0, 0, 0))
    # Right Bar
    @hpsp_bar.bitmap.fill_rect(BAR_LENGTH + 1, 0, 
      1, BAR_HEIGHT * 2 + 3, Color.new(0, 0, 0))
    # Draw separating line.
    @hpsp_bar.bitmap.fill_rect(0, BAR_HEIGHT + 1, 
      BAR_LENGTH + 2, 1, Color.new(0, 0, 0))
    
    # Call the old code.
    ismy_hpsphud_scene_map_main
    
    # Dispose of our resources.
    @hpsp_bar.bitmap.dispose unless @hpsp_bar.bitmap.disposed?
    @hpsp_bar.dispose unless @hpsp_bar.disposed?
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Decide whether or not the thing should be seen.
    @hpsp_bar.visible = @hpsp_visible
    
    # Only update and or redraw if its visible.
    if @hpsp_bar.visible
      if OVER_PLAYER
        # Draw the bats above the player.
        @hpsp_bar.x, @hpsp_bar.y = 
          ($game_player.real_x / 4 + 16) - (BAR_LENGTH / 2), 
          $game_player.real_y / 4 - 32
      else
        # Draw the bars below the player.
        @hpsp_bar.x, @hpsp_bar.y = 
          ($game_player.real_x / 4 + 16) - (BAR_LENGTH / 2), 
          $game_player.real_y / 4 + 32
      end
      # See if the hp/sp needs to be redrawn.
      update_bars
    end
    
    # Call the old code.
    ismy_hpsphud_scene_map_update
  end
  #--------------------------------------------------------------------------
  # * Update the Bars
  #--------------------------------------------------------------------------
  def update_bars
    # If HP has changed from last time.
    if @hp != $game_party.actors.first.hp
      @hp = $game_party.actors.first.hp
      draw_hp_bar
    end
    
    # If SP has changed from the last time.
    if @sp != $game_party.actors.first.sp
      @sp = $game_party.actors.first.sp
      draw_sp_bar
    end
  end
  #--------------------------------------------------------------------------
  # * Draw HP Bar
  #--------------------------------------------------------------------------
  def draw_hp_bar
    # Clear out the health portion.
    @hpsp_bar.bitmap.fill_rect(1, 1, 
      BAR_LENGTH, BAR_HEIGHT, Color.new(0, 0, 0, 0))

    # Determine the actual amount of pixels to draw.
    health = BAR_LENGTH * 
      ($game_party.actors.first.hp / $game_party.actors.first.maxhp.to_f)
    
    # Draw the health bar.
    @hpsp_bar.bitmap.fill_rect(1, 1, 
      health, BAR_HEIGHT, Color.new(200, 0, 0))
  end
  #--------------------------------------------------------------------------
  # * Draw SP Bar
  #--------------------------------------------------------------------------
  def draw_sp_bar
    # Clear out the skill portion.
    @hpsp_bar.bitmap.fill_rect(1, BAR_HEIGHT + 2, 
      BAR_LENGTH, BAR_HEIGHT, Color.new(0, 0, 0, 0))

    # Determine the actual amount of pixels to draw.
    skill = BAR_LENGTH * 
      ($game_party.actors.first.sp / $game_party.actors.first.maxsp.to_f)

    # Draw the skill bar.
    @hpsp_bar.bitmap.fill_rect(1, BAR_HEIGHT + 2, 
      skill, BAR_HEIGHT, Color.new(0, 0, 200))
  end
end

There's a bunch of constants at the top so you can change them to whatever you like.

[edit] I corrected the original code to use the player's real_x/real_y instead of x/y. There's no longer a popping when the player is moving just smooth movement.

Good luck with it ISMY! :thumb:
 

khmp

Sponsor

:tongue:

If it makes you feel better you can change the class declaration line to:
Code:
class Scene_Map < SDK::Scene_Base
But it works with or without the change.
 

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