查看: 95|回复: 0

[转载发布] 简单的敌人血条

[复制链接]
  • TA的每日心情
    开心
    昨天 10:15
  • 签到天数: 73 天

    连续签到: 1 天

    [LV.6]常住居民II

    2044

    主题

    86

    回帖

    8602

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    -2
    卡币
    6460
    OK点
    16
    积分
    8602
    发表于 2024-4-12 16:32:49 | 显示全部楼层 |阅读模式
    虽然这种脚本已经很多了,但是这是我目前学习手册能写出来的第一个脚本。
    效果大致是可以在敌人的备注里设置多管血,然后可以设置x轴、y轴的偏移量,以及血条长度(8的倍数)
    在敌人的备注中即n条血,即x轴的偏移量(貌似只能是正整数,不知怎么改),同理,这个是手册里的。

    使用的话因为高光是用图片绘制的,所以需要把下面的图导入pictures中(高光图很小,24*8,而且是白色的,在两行之间,也可以订制自己的高光,左边8*8是左圆角,中间是用来重复填充的,右侧同左)

    效果大概如下图,动图录太大了导致无法上传。。

    下面是代码,参考Sprite_timer写的。
    1. #encoding:utf-8#==============================================================================# ■ Sprite_Hp#------------------------------------------------------------------------------#  显示血条的精灵。根据 绑定敌人 的状态自动变化。#==============================================================================class Sprite_Hp < Sprite  #--------------------------------------------------------------------------  # ● 初始化常量  #--------------------------------------------------------------------------  H = 36                              #位图 高度  DUR = 0.5                           #动画 持续时间  FPS = 60                            #每秒 帧数  #--------------------------------------------------------------------------  # ● 初始化对象  #--------------------------------------------------------------------------  def initialize( viewport, enemy)    super(viewport)    initPara(enemy)    create_bitmap    update  end  #--------------------------------------------------------------------------  # ● 初始化参数  #--------------------------------------------------------------------------  def initPara(enemy)    @enemy = enemy                          #绑定敌人    @colorset = [ MyColor.red, MyColor.green, MyColor.blue, MyColor.purple, MyColor.yellow] #不同管血条的颜色,可以Color.new(r,g,b,a)来设置    @life = getLife.to_i                    #获取生命数        @tmphp = enemy.hp                       #用于动画的 临时血量    @perlife = ( @life == 0 ? enemy.hp : enemy.hp/@life)                                                       #每条命的血量    @dvalue = 0                             #临时血量差值    @anima = false                          #动画状态    setPara(@enemy.enemy.note)    @nameRect = Rect.new( 0, 0, @length, 18)    #名称矩形    @bgRect = Rect.new( 0, 18, @length, 18)     #背景矩形    @hlRect = Rect.new( 4, 20, @length - 16, 8)     #高光矩形  end  #--------------------------------------------------------------------------  # ● 设置参数(通过 注释)  #--------------------------------------------------------------------------  def setPara(note)    @offsetX = (// =~ note ? $1.to_i : 0)    @offsetY = (// =~ note ? $1.to_i : -20)    @length = (// =~ note ? $1.to_i : 128)     #为了绘制高光,建议设置为 8 的倍数  end  #--------------------------------------------------------------------------  # ● 释放  #--------------------------------------------------------------------------  def dispose    self.bitmap.dispose    super  end  #--------------------------------------------------------------------------  # ● 生成位图  #--------------------------------------------------------------------------  def create_bitmap    self.bitmap = Bitmap.new( @length, H)    self.bitmap.fill_rect( @bgRect, color2)     #绘制背景矩形    self.bitmap.font.size = 16    self.bitmap.font.bold = true    self.bitmap.font.italic = true    self.bitmap.font.color.set(255, 255, 255)  end  #--------------------------------------------------------------------------  # ● 底层颜色(始终填充整个矩形)  #--------------------------------------------------------------------------  def color1    if @life == 0       MyColor.gray    else      @colorset[@life % @colorset.size - 1]    end  end  #--------------------------------------------------------------------------  # ● 血条颜色(变动矩形)  #--------------------------------------------------------------------------  def color2    @colorset[@life % @colorset.size ]  end  #--------------------------------------------------------------------------  # ● 获取生命条数  #--------------------------------------------------------------------------  def getLife    // =~ @enemy.enemy.note ? $1.to_s : 0  end  #--------------------------------------------------------------------------  # ● 血条长度  #--------------------------------------------------------------------------  def flexlen    if @tmphp % @perlife != 0      return (@tmphp % @perlife) * @length / @perlife    end    if (@tmphp != 0) && (@tmphp % @perlife == 0)      return @length     end  end  #--------------------------------------------------------------------------  # ● 更新画面  #--------------------------------------------------------------------------  def update    super    update_tmphp    update_life    update_bitmap    update_position    update_visibility  end  #--------------------------------------------------------------------------  # ● 更新临时血量(tmp Hp)  #--------------------------------------------------------------------------  def update_tmphp    if !@anima && (@tmphp != @enemy.hp)      @dvalue =  @tmphp - @enemy.hp      @anima = true      @tmphp = @tmphp - @dvalue/(DUR * FPS)    elsif @anima && (@tmphp == @enemy.hp)      @anima = false    elsif @anima && (@tmphp != @enemy.hp)      @tmphp = @tmphp - @dvalue/(DUR * FPS)      @tmphp = @enemy.hp if @tmphp < @enemy.hp    end  end  #--------------------------------------------------------------------------  # ● 更新命数(life)  #--------------------------------------------------------------------------  def update_life    @life = @life - 1 if @tmphp/@perlife < @life  end  #--------------------------------------------------------------------------  # ● 更新源位图(Source Bitmap)  #--------------------------------------------------------------------------  def update_bitmap    redraw  end  #--------------------------------------------------------------------------  # ● 重绘  #--------------------------------------------------------------------------  def redraw    self.bitmap.clear        self.bitmap.fill_rect( @bgRect, color1)     #绘制背景矩形    self.bitmap.fill_rect( @bgRect.x, @bgRect.y, flexlen, @bgRect.height, color2) if @tmphp != 0                                                #绘制血条矩形    self.bitmap.draw_border( @bgRect, MyColor.black, 2)    #绘制边框    draw_highlight( @hlRect)    #绘制高光    self.bitmap.draw_text(self.bitmap.rect, life_text, 2) if @life != 0      #绘制生命数(当生命数不为0)    self.bitmap.draw_text(@bgRect, @tmphp.to_i.to_s, 1)    #绘制生命值  end  #--------------------------------------------------------------------------  # ● 绘制高光  #--------------------------------------------------------------------------  def draw_highlight( dest_rect)    leftCorner = Rect.new(0,0,8,8)    middle = Rect.new(8,0,8,8)    rightCorner = Rect.new(16,0,8,8)    pic = Cache.picture("hightLight01")    x = dest_rect.x    y = dest_rect.y    i = 1    self.bitmap.stretch_blt(Rect.new( x, y, 8, 8), pic, leftCorner)    while x < (dest_rect.x + dest_rect.width - 8)      x = x + 8      self.bitmap.stretch_blt(Rect.new( x, y, 8, 8), pic, middle)    end    x = x + 8    self.bitmap.stretch_blt(Rect.new( x, y, 8, 8), pic, rightCorner)  end  #--------------------------------------------------------------------------  # ● 生成绘制内容  #--------------------------------------------------------------------------  def life_text    sprintf("x%2d", @life)  end  #--------------------------------------------------------------------------  # ● 更新位置  #--------------------------------------------------------------------------  def update_position    self.x = @enemy.screen_x - self.bitmap.width/2 + @offsetX    self.y = @enemy.screen_y + @offsetY    self.z = 200  end  #--------------------------------------------------------------------------  # ● 更新可视状态  #--------------------------------------------------------------------------  def update_visibility    self.visible = true if @enemy.hp != 0    self.visible = false if (@enemy.hp == 0) && !@anima   endend#encoding:utf-8#==============================================================================# ■ Spriteset_Battle#------------------------------------------------------------------------------#  处理战斗画面的精灵的类。本类在 Scene_Battle 类的内部使用。#==============================================================================class Spriteset_Battle  #--------------------------------------------------------------------------  # ● 初始化对象  #--------------------------------------------------------------------------  def initialize    create_viewports    create_battleback1    create_battleback2    create_enemies    create_actors    create_pictures    create_timer    create_hp    update  end  #--------------------------------------------------------------------------  # ● Hp 精灵生成  #--------------------------------------------------------------------------  def create_hp    @hp_sprites = $game_troop.members.reverse.collect do |enemy|      Sprite_Hp.new(@viewport1, enemy)    end  end  #--------------------------------------------------------------------------  # ● 更新画面  #--------------------------------------------------------------------------  def update    update_battleback1    update_battleback2    update_enemies    update_actors    update_pictures    update_timer    update_hp    update_viewports  end  #--------------------------------------------------------------------------  # ● 更新敌人的精灵  #--------------------------------------------------------------------------  def update_hp    @hp_sprites.each {|sprite| sprite.update }  endend#encoding:utf-8#==============================================================================# ■ Bitmap_extension#------------------------------------------------------------------------------#  Bitmap类的扩展#==============================================================================class Bitmap  #--------------------------------------------------------------------------  # ● 绘制边框  #--------------------------------------------------------------------------  def draw_border( x, y, width, height, color, thick)    self.fill_rect( x, y, width, thick, color)    self.fill_rect( x + width - thick, y, thick, height, color)    self.fill_rect( x, y + height - thick, width, thick, color)    self.fill_rect( x, y, thick, height, color)  end  #--------------------------------------------------------------------------  # ● 绘制边框 参数为 Rect  #--------------------------------------------------------------------------  def draw_border( rect, color, thick)    self.fill_rect( rect.x, rect.y, rect.width, thick, color)    self.fill_rect( rect.x + rect.width - thick, rect.y, thick, rect.height, color)    self.fill_rect( rect.x, rect.y + rect.height - thick, rect.width, thick, color)    self.fill_rect( rect.x, rect.y, thick, rect.height, color)  endend#encoding:utf-8#==============================================================================# ■ Color_extension#------------------------------------------------------------------------------#  mColor类,包含常用色#==============================================================================class MyColor  #--------------------------------------------------------------------------  # ● 常用的颜色  #--------------------------------------------------------------------------  def self.black   ; Color.new(   0,   0,   0)   end  def self.white   ; Color.new( 255, 255, 255)   end  def self.red     ; Color.new( 255,   0,   0)   end  def self.green   ; Color.new(   0, 255,   0)   end  def self.blue    ; Color.new(   0,   0, 255)   end  def self.gray    ; Color.new( 128, 128, 128)   end  def self.yellow  ; Color.new( 255, 255,   0)   end  def self.purple  ; Color.new( 255,   0, 255)   end  def self.darkRed ; Color.new( 128,   0,   0)   end  def self.darkGreen;Color.new(   0, 128,   0)   end  def self.darkBlue; Color.new(   0,   0, 128)   end  def self.lighRed ; Color.new( 255, 128, 128)   endend复制代码
    复制代码
                 本帖来自P1论坛作者多啦A户,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=480470  若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
    天天去同能,天天有童年!
    回复 论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2024-9-21 16:16 , Processed in 0.045851 second(s), 38 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表