简单的敌人血条
虽然这种脚本已经很多了,但是这是我目前学习手册能写出来的第一个脚本。效果大致是可以在敌人的备注里设置多管血,然后可以设置x轴、y轴的偏移量,以及血条长度(8的倍数)
在敌人的备注中即n条血,即x轴的偏移量(貌似只能是正整数,不知怎么改),同理,这个是手册里的。
https://rpg.blue/data/attachment/forum/202003/01/183540hpfwbgz0m22pz6bo.png
使用的话因为高光是用图片绘制的,所以需要把下面的图导入pictures中(高光图很小,24*8,而且是白色的,在两行之间,也可以订制自己的高光,左边8*8是左圆角,中间是用来重复填充的,右侧同左)
https://rpg.blue/data/attachment/forum/202003/01/183539zbyhbjl347l49x86.png
效果大概如下图,动图录太大了导致无法上传。。
https://rpg.blue/data/attachment/forum/202003/01/183650l7i7pys9sff7p955.png
下面是代码,参考Sprite_timer写的。
#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 updateend#--------------------------------------------------------------------------# ● 初始化参数#--------------------------------------------------------------------------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 superend#--------------------------------------------------------------------------# ● 生成位图#--------------------------------------------------------------------------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] endend#--------------------------------------------------------------------------# ● 血条颜色(变动矩形)#--------------------------------------------------------------------------def color2 @colorset[@life % @colorset.size ]end#--------------------------------------------------------------------------# ● 获取生命条数#--------------------------------------------------------------------------def getLife // =~ @enemy.enemy.note ? $1.to_s : 0end#--------------------------------------------------------------------------# ● 血条长度#--------------------------------------------------------------------------def flexlen if @tmphp % @perlife != 0 return (@tmphp % @perlife) * @length / @perlife end if (@tmphp != 0) && (@tmphp % @perlife == 0) return @length endend#--------------------------------------------------------------------------# ● 更新画面#--------------------------------------------------------------------------def update super update_tmphp update_life update_bitmap update_position update_visibilityend#--------------------------------------------------------------------------# ● 更新临时血量(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 endend#--------------------------------------------------------------------------# ● 更新命数(life)#--------------------------------------------------------------------------def update_life @life = @life - 1 if @tmphp/@perlife < @lifeend#--------------------------------------------------------------------------# ● 更新源位图(Source Bitmap)#--------------------------------------------------------------------------def update_bitmap redrawend#--------------------------------------------------------------------------# ● 重绘#--------------------------------------------------------------------------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 = 200end#--------------------------------------------------------------------------# ● 更新可视状态#--------------------------------------------------------------------------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 updateend#--------------------------------------------------------------------------# ● Hp 精灵生成#--------------------------------------------------------------------------def create_hp @hp_sprites = $game_troop.members.reverse.collect do |enemy| Sprite_Hp.new(@viewport1, enemy) endend#--------------------------------------------------------------------------# ● 更新画面#--------------------------------------------------------------------------def update update_battleback1 update_battleback2 update_enemies update_actors update_pictures update_timer update_hp update_viewportsend#--------------------------------------------------------------------------# ● 更新敌人的精灵#--------------------------------------------------------------------------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) enddef self.white ; Color.new( 255, 255, 255) enddef self.red ; Color.new( 255, 0, 0) enddef self.green ; Color.new( 0, 255, 0) enddef self.blue ; Color.new( 0, 0, 255) enddef self.gray ; Color.new( 128, 128, 128) enddef self.yellow; Color.new( 255, 255, 0) enddef self.purple; Color.new( 255, 0, 255) enddef self.darkRed ; Color.new( 128, 0, 0) enddef self.darkGreen;Color.new( 0, 128, 0) enddef self.darkBlue; Color.new( 0, 0, 128) enddef self.lighRed ; Color.new( 255, 128, 128) endend复制代码
本帖来自P1论坛作者多啦A户,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=480470若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
页:
[1]