查看: 766|回复: 3

[有事请教] 如何让计时器数字变化的速度与游戏帧率一样快?

[复制链接]
梦石
0
星屑
230
在线时间
24 小时
注册时间
2021-8-20
回帖
8
发表于 2025-7-21 17:35:40 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
想整一个类似合金装备里警报时的数字显示,最后一位数字的变化速度和游戏帧率一样,或者直接显示当前的帧也行。
使用的计时器脚本
[pre lang="ruby"]#---------
#已经移植到RPG MAKER VX ACE
#Lanza
#---------
#==============================================================================
# ★ 多组多功能计时器 ★       -> by   芯☆淡茹水
#==============================================================================
# ● 使用方法:1,复制脚本,插入到 main 前。
#
#              2,启动计时:
#                 
#                 PS:注意这里!!!!!!!!!!!!!!
#                 改为了$game_timer 而非原版$game_system
#
#                 事件 -> 脚本 :$game_timer.start_time(tit, min, switch, add)
#
#                 括号里的参数:tit    -> 标题 。比如:双倍经验时间;副本时间,等
#                                         代入的参数为 字符串 。由于计时窗口宽度
#                                         有限,标题最好不要超过 5 个汉字。
#
#                               min    -> 计时时间X分钟。(注意是分钟,可以是小数)
#
#                               switch -> 计时完成后打开的开关ID。
#
#                               add    -> 正计时标志。需要正计时写 true ,不需要
#                                         时可不写。
#
#               3,中途取消计时:
#        
#                    事件 -> 脚本:$game_system.end_time(switch_id)
#
#                    括号里的参数:switch_id  -> 对应的计时器打开的开关ID。
#==============================================================================
# ● 说明:
#           该计时器理论上可同时无限个计时。
#
#           计时窗口只在地图场景显示,战斗场景不显示计时窗口,但仍然会计时,
#           其他场景不计时。
#
#           计时完成后自动打开启动计时时设置的开关,需要实现什么功能可通过事件,
#           开关,公共事件,等,来协调完成。这个就自己开脑洞。
#           可参照范例工程的示范(事件,公共事件)。
#     
#==============================================================================
# ● 设置:
module X☆R
  #--------------------------------------------------------------------------
  # 计时窗口 X 坐标。
  TM_WINDOW_X = 280

  #--------------------------------------------------------------------------
  # 计时窗口 Y 坐标。
  TM_WINDOW_Y = 15

  #--------------------------------------------------------------------------
  # 计时窗口标题字体颜色。
  TM_TIT_COLOR = Color.new(255,0,0)

  #--------------------------------------------------------------------------
  # 计时窗时间字体颜色(30 秒以上)。
  TM_TIME_COLOR1 = Color.new(255,0,0)

  #--------------------------------------------------------------------------
  # 计时窗时间字体颜色(10 - 30 秒)。
  TM_TIME_COLOR2 = Color.new(255,0,0)

  #--------------------------------------------------------------------------
  # 计时窗时间字体颜色(0 - 10 秒)。
  TM_TIME_COLOR3 = Color.new(255,0,0)

  #--------------------------------------------------------------------------
  # 最后 10 秒是否声音提示?
  TM_SOUND_TIP = false
  
  #--------------------------------------------------------------------------
  # 声音提示的 SE 文件名。
  TM_SOUND = "Switch1"
end
#==============================================================================
class Time_Meter
  attr_reader     :switch_id, :title, :add_count
  attr_accessor   :sec, :max_sec
  #--------------------------------------------------------------------------
  def initialize(title, min, switch_id, add)
    @title = title
    @max_sec = @sec = Integer(min * 60)
    @sec = 0 if add
    @switch_id = switch_id
    @add_count = add ? 1 : -1
  end
  #--------------------------------------------------------------------------
  def sec_judge
    return @add_count > 0 ? @sec < @max_sec : @sec > 0
  end
  #--------------------------------------------------------------------------
  def update
    @sec += @add_count if sec_judge and Graphics.frame_count % Graphics.frame_rate == 0
    unless sec_judge
      $game_switches[@switch_id] = true
      $game_map.need_refresh = true
    end
    if @data_sec != @sec and @add_count < 0
      @data_sec = @sec
      if @sec < 10 and X☆R::TM_SOUND_TIP and $scene.is_a?(Scene_Map)
        Audio.se_play("Audio/SE/#{X☆R::TM_SOUND}", 80, 100)
      end
    end
  end
end
#==============================================================================
class Window_Tm < Sprite
  #--------------------------------------------------------------------------
  def initialize(x, y, time)
    super()
    self.x = x
    self.y = y
    self.z = 9999
    @time = time
    bitmap = Cache.character($game_player.character_name)
    @b_width = bitmap.width / 8
    @b_height = bitmap.height / 4
  end
  #--------------------------------------------------------------------------
  def dispose
    self.bitmap.dispose if self.bitmap
    super
  end
  #--------------------------------------------------------------------------
  def refresh
    self.bitmap.dispose if self.bitmap
    self.bitmap = Bitmap.new("Graphics/Pictures/Time_meter_back")
    self.bitmap.font.size = 18
    self.bitmap.font.bold = true
    cx = self.bitmap.text_size(@time.title).width
    self.bitmap.font.color = X☆R::TM_TIT_COLOR
    self.bitmap.draw_text(35, 0, cx, 30, @time.title)
    if @time.add_count > 0
      color = X☆R::TM_TIME_COLOR1
    else
      color = @time.sec >= 30 ? X☆R::TM_TIME_COLOR1 :
      (@time.sec >= 10 ? X☆R::TM_TIME_COLOR2 : X☆R::TM_TIME_COLOR3)
    end
    hour = @time.sec / 3600
    min = @time.sec / 60 % 60
    sec = @time.sec % 60
    text = sprintf("%02d:%02d:%02d", hour, min, sec)
    self.bitmap.font.color = color
    self.bitmap.draw_text(130, 0, 90, 30, text, 2)
  end
  #--------------------------------------------------------------------------
  def update
    return if self.disposed?
    super
    if @data_sec != @time.sec
      @data_sec = @time.sec
      refresh
    end
    if self.x < $game_player.screen_x + @b_width and
     self.x + 240 > $game_player.screen_x - @b_width and
     self.y + 30 < $game_player.screen_y and
     self.y > $game_player.screen_y - @b_height
      self.opacity = 80
    else
      self.opacity = 255 if self.opacity < 255
    end
    dispose unless @time.sec_judge
  end
end
#==============================================================================
class Game_Timer
  #--------------------------------------------------------------------------
  alias add_initialize_xr initialize
  def initialize
    add_initialize_xr
    @time_meters = []
  end
  #--------------------------------------------------------------------------
  def time_meters
    return @time_meters
  end
  #--------------------------------------------------------------------------
  def start_time(title="", min=0, switch_id=0, add=false)
    return if min == 0 or switch_id == 0
    for t in @time_meters
      if t.switch_id == switch_id
        t.add_count > 0 ? t.max_sec += Integer(min * 60) : t.sec += Integer(min * 60)
        return
      end
    end
    @time_meters.push(Time_Meter.new(title, min, switch_id, add))
  end
  #--------------------------------------------------------------------------
  def end_time(switch_id)
    for t in @time_meters
      if t.switch_id == switch_id
        @time_meters.delete(t)
      end
    end
  end
  #--------------------------------------------------------------------------
  alias old_update_xr update
  def update
    unless @time_meters.empty?
      for time in @time_meters
        time.update
        unless time.sec_judge
          @time_meters.delete(time)
          next
        end
      end
    end
    old_update_xr
  end
end
#==============================================================================
class Scene_Map
  #--------------------------------------------------------------------------
  alias old_main_xr main
  def main
    @time_window = []
    old_main_xr
    @time_window.each{|t| t.dispose}
  end
  #--------------------------------------------------------------------------
  alias old_update_xr update
  def update
    if @data_time != $game_timer.time_meters
      @data_time = $game_timer.time_meters.clone
      @time_window.each{|t| t.dispose}
      unless $game_timer.time_meters.empty?
        for i in 0...$game_timer.time_meters.size
          time = $game_timer.time_meters
          nx = X☆R::TM_WINDOW_X > 320 ? -240 : 240
          nx = i / 8 * nx
          x = X☆R::TM_WINDOW_X + nx ; y = i % 8 * 30 + X☆R::TM_WINDOW_Y
          @time_window = Window_Tm.new(x, y, time)
        end
      end
    end
    unless @time_window.empty?
      for w in @time_window
        w.update
        @time_window.delete(w) if w.disposed?
      end
    end
    old_update_xr
  end
end
#==============================================================================
[/pre]
求大佬解答
梦石
0
星屑
230
在线时间
24 小时
注册时间
2021-8-20
回帖
8
 楼主| 发表于 2025-7-22 15:30:47 | 显示全部楼层
想了一下直接把原帖链接放到这里更好https://rpg.blue/thread-401351-1-1.html
回复

使用道具 举报

梦石
0
星屑
42235
在线时间
6017 小时
注册时间
2006-11-10
回帖
6966
发表于 2025-7-23 10:21:56 | 显示全部楼层
  1. #---------
  2. #已经移植到RPG MAKER VX ACE
  3. #Lanza
  4. #---------
  5. #==============================================================================
  6. # ★ 多组多功能计时器 ★       -> by   芯☆淡茹水
  7. #==============================================================================
  8. # ● 使用方法:1,复制脚本,插入到 main 前。
  9. #
  10. #              2,启动计时:
  11. #                 
  12. #                 PS:注意这里!!!!!!!!!!!!!!
  13. #                 改为了$game_timer 而非原版$game_system
  14. #
  15. #                 事件 -> 脚本 :$game_timer.start_time(tit, min, switch, add)
  16. #
  17. #                 括号里的参数:tit    -> 标题 。比如:双倍经验时间;副本时间,等
  18. #                                         代入的参数为 字符串 。由于计时窗口宽度
  19. #                                         有限,标题最好不要超过 5 个汉字。
  20. #
  21. #                               min    -> 计时时间X分钟。(注意是分钟,可以是小数)
  22. #
  23. #                               switch -> 计时完成后打开的开关ID。
  24. #
  25. #                               add    -> 正计时标志。需要正计时写 true ,不需要
  26. #                                         时可不写。
  27. #
  28. #               3,中途取消计时:
  29. #        
  30. #                    事件 -> 脚本:$game_system.end_time(switch_id)
  31. #
  32. #                    括号里的参数:switch_id  -> 对应的计时器打开的开关ID。
  33. #==============================================================================
  34. # ● 说明:
  35. #           该计时器理论上可同时无限个计时。
  36. #
  37. #           计时窗口只在地图场景显示,战斗场景不显示计时窗口,但仍然会计时,
  38. #           其他场景不计时。
  39. #
  40. #           计时完成后自动打开启动计时时设置的开关,需要实现什么功能可通过事件,
  41. #           开关,公共事件,等,来协调完成。这个就自己开脑洞。
  42. #           可参照范例工程的示范(事件,公共事件)。
  43. #     
  44. #==============================================================================
  45. # ● 设置:
  46. module X☆R
  47.   #--------------------------------------------------------------------------
  48.   # 计时窗口 X 坐标。
  49.   TM_WINDOW_X = 280

  50.   #--------------------------------------------------------------------------
  51.   # 计时窗口 Y 坐标。
  52.   TM_WINDOW_Y = 15

  53.   #--------------------------------------------------------------------------
  54.   # 计时窗口标题字体颜色。
  55.   TM_TIT_COLOR = Color.new(255,0,0)

  56.   #--------------------------------------------------------------------------
  57.   # 计时窗时间字体颜色(30 秒以上)。
  58.   TM_TIME_COLOR1 = Color.new(255,0,0)

  59.   #--------------------------------------------------------------------------
  60.   # 计时窗时间字体颜色(10 - 30 秒)。
  61.   TM_TIME_COLOR2 = Color.new(255,0,0)

  62.   #--------------------------------------------------------------------------
  63.   # 计时窗时间字体颜色(0 - 10 秒)。
  64.   TM_TIME_COLOR3 = Color.new(255,0,0)

  65.   #--------------------------------------------------------------------------
  66.   # 最后 10 秒是否声音提示?
  67.   TM_SOUND_TIP = false

  68.   #--------------------------------------------------------------------------
  69.   # 声音提示的 SE 文件名。
  70.   TM_SOUND = "Switch1"
  71. end
  72. #==============================================================================
  73. class Time_Meter
  74.   attr_reader     :switch_id, :title, :add_count
  75.   attr_accessor   :sec, :max_sec
  76.   #--------------------------------------------------------------------------
  77.   def initialize(title, min, switch_id, add)
  78.     @title = title
  79.     @max_sec = @sec = Integer(min * 60)
  80.     @sec = 0 if add
  81.     @switch_id = switch_id
  82.     @add_count = add ? 1 : -1
  83.   end
  84.   #--------------------------------------------------------------------------
  85.   def sec_judge
  86.     return @add_count > 0 ? @sec < @max_sec : @sec > 0
  87.   end
  88.   #--------------------------------------------------------------------------
  89.   def update
  90.     @sec += @add_count if sec_judge and Graphics.frame_count % Graphics.frame_rate == 0
  91.     unless sec_judge
  92.       $game_switches[@switch_id] = true
  93.       $game_map.need_refresh = true
  94.     end
  95.     if @data_sec != @sec and @add_count < 0
  96.       @data_sec = @sec
  97.       if @sec < 10 and X☆R::TM_SOUND_TIP and $scene.is_a?(Scene_Map)
  98.         Audio.se_play("Audio/SE/#{X☆R::TM_SOUND}", 80, 100)
  99.       end
  100.     end
  101.   end
  102. end
  103. #==============================================================================
  104. class Window_Tm < Sprite
  105.   #--------------------------------------------------------------------------
  106.   def initialize(x, y, time)
  107.     super()
  108.     self.x = x
  109.     self.y = y
  110.     self.z = 9999
  111.     @time = time
  112.     bitmap = Cache.character($game_player.character_name)
  113.     @b_width = bitmap.width / 8
  114.     @b_height = bitmap.height / 4
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   def dispose
  118.     self.bitmap.dispose if self.bitmap
  119.     super
  120.   end
  121.   #--------------------------------------------------------------------------
  122.   def refresh
  123.     self.bitmap.dispose if self.bitmap
  124.     self.bitmap = Bitmap.new("Graphics/Pictures/Time_meter_back")
  125.     self.bitmap.font.size = 18
  126.     self.bitmap.font.bold = true
  127.     cx = self.bitmap.text_size(@time.title).width
  128.     self.bitmap.font.color = X☆R::TM_TIT_COLOR
  129.     self.bitmap.draw_text(35, 0, cx, 30, @time.title)
  130.     if @time.add_count > 0
  131.       color = X☆R::TM_TIME_COLOR1
  132.     else
  133.       color = @time.sec >= 30 ? X☆R::TM_TIME_COLOR1 :
  134.       (@time.sec >= 10 ? X☆R::TM_TIME_COLOR2 : X☆R::TM_TIME_COLOR3)
  135.     end
  136.     min = @time.sec / 60
  137.     sec = @time.sec % 60
  138.     fra = Graphics.frame_count % Graphics.frame_rate
  139.     text = sprintf("%02d:%02d:%02d", min, sec,fra)
  140.     self.bitmap.font.color = color
  141.     self.bitmap.draw_text(130, 0, 90, 30, text, 2)
  142.   end
  143.   #--------------------------------------------------------------------------
  144.   def update
  145.     return if self.disposed?
  146.     super
  147.     refresh
  148.     if self.x < $game_player.screen_x + @b_width and
  149.      self.x + 240 > $game_player.screen_x - @b_width and
  150.      self.y + 30 < $game_player.screen_y and
  151.      self.y > $game_player.screen_y - @b_height
  152.       self.opacity = 80
  153.     else
  154.       self.opacity = 255 if self.opacity < 255
  155.     end
  156.     dispose unless @time.sec_judge
  157.   end
  158. end
  159. #==============================================================================
  160. class Game_Timer
  161.   #--------------------------------------------------------------------------
  162.   alias add_initialize_xr initialize
  163.   def initialize
  164.     add_initialize_xr
  165.     @time_meters = []
  166.   end
  167.   #--------------------------------------------------------------------------
  168.   def time_meters
  169.     return @time_meters
  170.   end
  171.   #--------------------------------------------------------------------------
  172.   def start_time(title="", min=0, switch_id=0, add=false)
  173.     return if min == 0 or switch_id == 0
  174.     for t in @time_meters
  175.       if t.switch_id == switch_id
  176.         t.add_count > 0 ? t.max_sec += Integer(min * 60) : t.sec += Integer(min * 60)
  177.         return
  178.       end
  179.     end
  180.     @time_meters.push(Time_Meter.new(title, min, switch_id, add))
  181.   end
  182.   #--------------------------------------------------------------------------
  183.   def end_time(switch_id)
  184.     for t in @time_meters
  185.       if t.switch_id == switch_id
  186.         @time_meters.delete(t)
  187.       end
  188.     end
  189.   end
  190.   #--------------------------------------------------------------------------
  191.   alias old_update_xr update
  192.   def update
  193.     unless @time_meters.empty?
  194.       for time in @time_meters
  195.         time.update
  196.         unless time.sec_judge
  197.           @time_meters.delete(time)
  198.           next
  199.         end
  200.       end
  201.     end
  202.     old_update_xr
  203.   end
  204. end
  205. #==============================================================================
  206. class Scene_Map
  207.   #--------------------------------------------------------------------------
  208.   alias old_main_xr main
  209.   def main
  210.     @time_window = []
  211.     old_main_xr
  212.     @time_window.each{|t| t.dispose}
  213.   end
  214.   #--------------------------------------------------------------------------
  215.   alias old_update_xr update
  216.   def update
  217.     if @data_time != $game_timer.time_meters
  218.       @data_time = $game_timer.time_meters.clone
  219.       @time_window.each{|t| t.dispose}
  220.       unless $game_timer.time_meters.empty?
  221.         for i in 0...$game_timer.time_meters.size
  222.           time = $game_timer.time_meters[i]
  223.           nx = X☆R::TM_WINDOW_X > 320 ? -240 : 240
  224.           nx = i / 8 * nx
  225.           x = X☆R::TM_WINDOW_X + nx ; y = i % 8 * 30 + X☆R::TM_WINDOW_Y
  226.           @time_window[i] = Window_Tm.new(x, y, time)
  227.         end
  228.       end
  229.     end
  230.     unless @time_window.empty?
  231.       for w in @time_window
  232.         w.update
  233.         @time_window.delete(w) if w.disposed?
  234.       end
  235.     end
  236.     old_update_xr
  237.   end
  238. end
复制代码


换成它吧,  不过提醒下, 每帧draw可是很浪费资源的, 要自重
回复

使用道具 举报

梦石
0
星屑
230
在线时间
24 小时
注册时间
2021-8-20
回帖
8
 楼主| 发表于 2025-7-23 12:47:31 | 显示全部楼层
灯笼菜刀王 发表于 2025-7-23 10:21
换成它吧,  不过提醒下, 每帧draw可是很浪费资源的, 要自重

大佬厉害,感谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部