| 
 
| 赞 | 1 |  
| VIP | 246 |  
| 好人卡 | 87 |  
| 积分 | 1 |  
| 经验 | 34142 |  
| 最后登录 | 2015-1-15 |  
| 在线时间 | 323 小时 |  
 Lv1.梦旅人 
	梦石0 星屑55 在线时间323 小时注册时间2010-8-21帖子666 | 
| 
本帖最后由 沙漠点灰 于 2011-4-3 11:22 编辑
x
加入我们,或者,欢迎回来。您需要 登录 才可以下载或查看,没有帐号?注册会员  
 清单
 1.[方便] 随机播放BGM
 2.[扩展] 获取mp3文件时间长度 (有bug  ←等于没说)
 3.[扩展] Graphics块 模仿 RGSS2部分功能
 4.[扩展] Bitmap类   模仿 RGSS2部分功能
 5.[美化] 真 窗口移动
 6.[美化] 伪 窗口移动
 7.[扩展] 子选项窗口(http://rpg.blue/thread-174280-1-1.html)
 
 脚本如下:
 1+2 :
 使用方法 脚本置顶复制代码#==============================================================================
# ■ Audio 
#------------------------------------------------------------------------------
#  随机播放BGM    
#   Audio/BGM/下的文件,RTP什么的不会读...因为涉及到注册表什么的
#   可以读取 C:/Program Files/RPG Maker XP/RGSS/Standard/ 的东西
#        我能做的只有这些了,高手来解决吧
#==============================================================================
def Audio.random_bgm_play
  # 查找根目录下的BGM文件
  list = Dir["Audio/BGM/*"]
  list = list[rand(list.size)]
  v = Game_System.method_defined?("bgm_v") ? $game_system.bgm_v : 80 rescue return
  unless list
    # 根目录下没有BGM时,试图找C:/Program Files/RPG Maker XP/RGSS/Standard/下的文件
    old_dir = Dir.pwd
    Dir.chdir("C:/Program Files/RPG Maker XP/RGSS/Standard/") rescue return Dir.chdir(old_dir)
    list = Dir["Audio/BGM/*"]
    list = list[rand(list.size)]
    Dir.chdir(old_dir)
  end
  # 试图失败时,直接返回
  return unless list
  self.bgm_play(list, v, 100)
  # $game_system 为空时,直接返回
  return unless $game_system
  # 获取文件名,省事,
  $game_system.playing_bgm = RPG::AudioFile.new(File.basename(list),v,100)
end
class Game_System
  def playing_bgm=(bgm)
    @playing_bgm = bgm
  end
end
#--------------------------------------------------------------------------
# ●  获取mp3文件时间长度
#--------------------------------------------------------------------------
def Audio.mp3_length(name)
  @play ||= Win32API.new('winmm.dll', 'mciSendString',%(p p l l), 'l')
  old_dir = Dir.pwd
  Dir.chdir("Audio/BGM/")
  name += ".mp3" if File.extname(name) == ""
  length = " " * 10
  File.rename(name, "temp.mp3")
  @play.call("status temp.mp3 length", length, 512, 0)
  File.rename("temp.mp3", name)
  Dir.chdir(old_dir)
  return length.to_i
end
1.Audio.random_bgm_play
 2.Audio.mp3_length(name)   name = mp3文件名
 
 3:
 模仿RGSS2清单:复制代码#==============================================================================
# ■ Graphics 
#------------------------------------------------------------------------------
#  图像模块
#==============================================================================
#--------------------------------------------------------------------------
# ● 更待指定帧数 
#--------------------------------------------------------------------------
def Graphics.wait(n)
  n.times{self.update;yield if defined? yield}
end
#--------------------------------------------------------------------------
# ● 获取窗口宽
#--------------------------------------------------------------------------
def Graphics.width
  @width ||= 640
end
#--------------------------------------------------------------------------
# ● 获取窗口高
#--------------------------------------------------------------------------
def Graphics.height
  @height ||= 480
end
#--------------------------------------------------------------------------
# ● 更改窗口大小
#--------------------------------------------------------------------------
def Graphics.resize_screen(width, height)
  @width  ||= 640
  @height ||= 480
  
  游戏ini名=".\\Game.ini"
  val = "\0"*256
  gps = Win32API.new('kernel32', 'GetPrivateProfileString','pppplp', 'l')
  gps.call("Game", "Title", "", val, 256, 游戏ini名)
  val.delete!("\0")
  title = val
  fw = Win32API.new('user32', 'FindWindow', 'pp', 'i')
  hWnd = fw.call("RGSS Player", title)
  swp = Win32API.new('user32', 'SetWindowPos', 'lliiiii', 'i')
  
  pointwds = [0,0,0,0].pack('llll')
  pointcet = [0, 0].pack('ll')
  wdsrect = Win32API.new('user32.dll', 'GetWindowRect', 'lp', 'l')
  client_screen = Win32API.new("user32", "ClientToScreen", 'ip', 'i')
  
  wdsrect.call(hWnd,pointwds)
  client_screen.call(hWnd, pointcet)
  wds = pointwds.unpack('llll')
  cet = pointcet.unpack('ll')
  addw =  wds[2] - wds[0] - @width
  addh =  wds[3] - wds[1] - @height
  x = wds[0] - (width - @width) / 2
  y = wds[1] - (height - @height) / 2
  
  @width  = width
  @height = height
  
  swp.call(hWnd, 0, x, y, @width + addw, @height + addh, 0x20) 
end
1.等待指定帧数
 Graphics.wait(n)
 n = 帧数
 
 加上了扩展(代码块)
 Graphics.wait(n){...}
 
 比如向左移动一窗口55像素
 原来是:
 55.times{Graphics.update;窗口.x -= 1}
 现在
 Graphics.wait(55){窗口.x -= 1}
 (好像没区别...?)
 
 2.更改窗口大小
 Graphics.resize_screen(width, height)
 width = 宽   height 高
 
 3+4:获取窗口宽 高
 Graphics.width   Graphics.height
 
 4:(原    ——by 猫哥哥  改    ——by 沙漠.灰(未经过原作者同意,请见谅))
 清单 :复制代码#==============================================================================
# ■ Bitmap
#------------------------------------------------------------------------------
#    位图类    RGSS扩充
#                  原    ——by 猫哥哥
#                  改    ——by 沙漠.灰          
#=============================================================================#
class Bitmap
  #--------------------------------------------------------------------------
  # ● 渐进色彩填充
  #    vertical : 横(竖)填充
  #--------------------------------------------------------------------------
  def gradient_fill_rect(x,y,width,height=false,color1=255,color2=nil,vertical=false,opacity=255)
    # 矩形判断
    if x.is_a?(Rect)
      s1,s2,s3,s4=x.x,x.y,x.width,x.height
      return self.gradient_fill_rect(s1,s2,s3,s4,y,width,height,color1)
    end
    # 渐进步长(step)判断
    vertical == false ? step = width - x : step = height - y;color = color1;color.alpha = opacity
    # 渐进_红
    key_re = Math.sqrt(((color2.red   - color1.red  )/step)**2)
    # 渐进_绿
    key_gr = Math.sqrt(((color2.green - color1.green)/step)**2)
    # 渐进_蓝
    key_bl = Math.sqrt(((color2.blue  - color1.blue )/step)**2)
    # 反渐进方向判断
    key_re *= -1 if color2.red  < color1.red
    key_gr *= -1 if color2.green< color1.green
    key_bl *= -1 if color2.blue < color1.blue
    # 横(竖)填充
    if vertical
        (height-y).times{
          self.fill_rect(x, y, width, 1, color)
          y=y+1;color.red =color.red + key_re
          color.green =color.green + key_gr;color.blue =color.blue + key_bl
         }
    else
      (width-x).times{
          self.fill_rect(x, y, 1, height, color)
          x=x+1;color.red =color.red + key_re
          color.green =color.green + key_gr;color.blue =color.blue + key_bl
      }
    end
  end
  #--------------------------------------------------------------------------
  # ● 清除位图指定地方
  #--------------------------------------------------------------------------   
  def clear_rect(x, y=0,width=0,height=0)
    # 矩形判断
    if x.is_a?(Rect)
      self.fill_rect(x,Color.new(0,0,0,0))
    else
      self.fill_rect(x, y, width, height, Color.new(0,0,0,0)) 
    end
  end
  #--------------------------------------------------------------------------
  # ● 位图重叠模糊(壳)
  #-------------------------------------------------------------------------- 
  def fragment_bluring(blur=4,during=3)
    for i in 1..blur
      self.fragment_blur(i)
      self.blt(0, 0, self, self.rect)
      during.times{Graphics.update}    
    end
  end
  #--------------------------------------------------------------------------
  # ● 模糊算法(壳)
  #     times      重做模糊次数
  #     during     经历帧数
  #     blur       分散半径
  #--------------------------------------------------------------------------   
  def blur(times=1,during =1,blur=1)
    times.times{blur_r(blur);during.times{Graphics.update if during >1}} 
  end
  #--------------------------------------------------------------------------
  # ● 玻璃(半透明)渐进填充
  #    vertical   横(竖)渐进
  #--------------------------------------------------------------------------    
  def glass_fill_rect(x, y=nil,width=nil,height=false,color1=nil,color2=nil,vertical=false)
    # 矩形判断
    if x.is_a?(Rect)
      s1,s2,s3,s4=x.x,x.y,x.width,x.height
      return self.glass_fill_rect(s1,s2,s3,s4,y,width,height)
    end
    # 生成临时位图
    a1 = Bitmap.new(width, height);a2 = Bitmap.new(width, height)
    # 并填充
    a1.fill_rect(x, y, width, height, color1);a2.fill_rect(x, y, width, height, color2)
    # 横(竖)渐进
    unless vertical
      key =[[255.0/(width - x),255.0].min,1.0].min
      src_rect = Rect.new(x,y,1,height);k = x
      for i in x..width
        self.blt(x, y, a1, src_rect, (width-i)*key)
        self.blt(x, y, a2, src_rect, (i-k)*key)
        x += 1
      end
    else
      key =[[255.0/(height - x),255.0].min,1.0].min
      src_rect = Rect.new(x,y,width,1);k = y
      for i in y..height
        self.blt(x, y, a1, src_rect, (height-i)*key)
        self.blt(x, y, a2, src_rect, (i-k)*key)
        y += 1
      end
    end
    # 释放临时位图
    a1.dispose;a2.dispose
  end
  # 以下方法私有化
  private
  #--------------------------------------------------------------------------
  # ● 位图重叠模糊(执行)
  #--------------------------------------------------------------------------  
  def fragment_blur(radius=6)
    src_bitmap = self;rect = self.rect;key = 255/7
    self.blt(0,0,src_bitmap,rect,key*4)
    self.blt(0,-radius,src_bitmap,rect,key*2)
    self.blt(radius,0,src_bitmap,rect,key*2)
    self.blt(-radius,0,src_bitmap,rect,key*2)
    self.blt(0,radius,src_bitmap,rect,key*2)
    self.blt(radius,-radius,src_bitmap,rect,key*1)
    self.blt(-radius,radius,src_bitmap,rect,key*1)
    self.blt(-radius,-radius,src_bitmap,rect,key*1)
    self.blt(radius,radius,src_bitmap,rect,key*1)
  end
  #--------------------------------------------------------------------------
  # ● 模糊算法(执行)
  #      radius          分散半径
  #      p.s. 写得好乱...和我有的一拼,不过好多废代码....
  #--------------------------------------------------------------------------  
  def blur_r(radius = 2)
    # 分散半径值修正
    radius = [radius,1].max
    src_bitmap = self
    rect = self.rect
    
    ta_l = 1+radius*2
    ta = Table.new(ta_l,ta_l)  #过滤器 
    
    ta_l.times{|i|ta_l.times{|j|ta[i,j] = 1}}
    ta[((ta.xsize+1)/2)-1,((ta.xsize+1)/2)-1] = 2**(1+radius)
    main = ta[((ta.xsize+1)/2)-1,((ta.xsize+1)/2)-1]#找到中心点
    nn = 2;j = ((ta.xsize+1)/2)-1;line =[]#主干道
    
    for i in 0..((ta.xsize+1)/2)-2
      ta[i,j] = nn
      ta[j,i] = nn
      ta[ta.xsize-1-i,j] = nn
      ta[j,ta.xsize-1-i] = nn
      line.push ta[i,((ta.xsize+1)/2)-1]
      nn *= 2
    end
    
    for j in 0...line.size
       for i in 0..((ta.xsize+1)/2)-2
        line[j] = line[j]/2
        break if line[j] == 1
        ta[((ta.xsize+1)/2)-2-i ,j           ] = line[j]
        ta[((ta.xsize+1)/2)+i   ,j           ] = line[j]
        ta[((ta.xsize+1)/2)-2-i ,ta.xsize-1-j] = line[j]
        ta[((ta.xsize+1)/2)+i   ,ta.xsize-1-j] = line[j]
      end
    end
    
    key_a = [];key_p = main
    for i in 1..main
      if key_p == 1
        key_a << main
        break
      else
      key_p /= 2
      key_a << key_p
      end
    end
    key_k = 0
    for i in 0...key_a.size
      key_k += key_a[i]
    end
    key = [255.0/key_k,1].max 
    for i in 0...ta.xsize
      for j in 0...ta.ysize
        # 值的修正
        key_main = [[key*ta[i,j],255].min,1].max
        self.blt(i-(((ta.xsize+1)/2)-1),j-(((ta.xsize+1)/2)-1),self,rect,key_main)
      end
    end
  end
end  
1.渐进色彩填充
 参考RMVX Help
 
 2.清除位图指定地方
 同上
 
 3.位图重叠模糊(不知道名字对不对...)
 位图.fragment_bluring
 参考 模糊算法
 
 4.模糊算法
 参考RMVX Help
 
 5.玻璃(半透明)渐进填充
 位图.glass_fill_rect(XXXXXXXXXXXXXXXX)
 参考 渐进色彩填充
 
 五.
 上面代码置顶复制代码#==============================================================================
# ■ Window
#------------------------------------------------------------------------------
#  游戏中全部窗口的超级类。
#==============================================================================
class Window
  #--------------------------------------------------------------------------
  # ● X 坐标
  #--------------------------------------------------------------------------
  alias isa_dust_x x
  def x
    @true_x ||= self.isa_dust_x.to_f
    return @true_x
  end
  alias isa_dust_x= x=
  def x=(new_x)
    @true_x = new_x.to_f
    self.isa_dust_x = @true_x
  end
  #--------------------------------------------------------------------------
  # ● Y 坐标
  #--------------------------------------------------------------------------
  alias isa_dust_y y
  def y
    @true_y ||= self.isa_dust_y.to_f
    return @true_y
  end
  alias isa_dust_y= y=
  def y=(new_y)
    @true_y = new_y.to_f
    self.isa_dust_y = @true_y
  end
end
上面代码插到Main前Window_Base后复制代码#==============================================================================
# ■ Window_Base
#------------------------------------------------------------------------------
#  游戏中全部窗口的超级类。
#==============================================================================
class Window_Base < Window
  #--------------------------------------------------------------------------
  # ● 初始化
  #--------------------------------------------------------------------------
  alias isa_dust_initialize initialize
  def initialize(*a)
    isa_dust_initialize(*a)
    @move_count = 0
    @move_to_x  = 0
    @move_to_y  = 0
  end
  #--------------------------------------------------------------------------
  # ● 真 移动
  #--------------------------------------------------------------------------
  def move_to(x,y,count)
    @move_to_x = x
    @move_to_y = y
    @move_count= count
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  alias isa_dust_update update
  def update
    isa_dust_update
    # 移动完毕时,直接返回
    return if @move_count <= 0
    self.x += (@move_to_x - self.x)/@move_count.to_f
    self.y += (@move_to_y - self.y)/@move_count.to_f
    @move_count -= 1
  end
end
 用法:
 窗口.move_to(x坐标,y坐标,帧数)
 注.必须调用update方法,帧数(默认)40帧1秒(不同于事件的图片移动)
 
 例:(用上面的等待指定帧数)
 
 窗口.move_to(600,0,100)
 Graphics.wait(100){窗口.update}
 
 100帧让叫窗口的窗口对象移动到 600,0 处
 
 六.代码如下
 这个是纯美化功能,动态淡出淡入复制代码class Window_Base < Window
  #--------------------------------------------------------------------------
  # ● 伪 移动
  #--------------------------------------------------------------------------
  def not_really_move(d="right")
    d == "right" ? d = 1 : d = -1
    self.x        += d * 5
    self.ox       += d * 5
    self.width    -= d * 5
    self.opacity  += d * 15
    self.contents_opacity += d * 15
  end
end
一般先初始化(必须是17)
 17.times{@folder_window.not_really_move("left ")}
 Graphics.wait(17){@folder_window.not_really_move("right")}
 
 场景最后释放前
 Graphics.wait(17){@folder_window.not_really_move("left ")}
 
 具体例子见:http://rpg.blue/thread-174282-1-1.html
 
 七.略
 
 打字好辛苦~~
 
 
 | 
 |