赞 | 3 |
VIP | 1 |
好人卡 | 40 |
积分 | 1 |
经验 | 93188 |
最后登录 | 2020-7-27 |
在线时间 | 1379 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 76
- 在线时间
- 1379 小时
- 注册时间
- 2012-7-5
- 帖子
- 1698
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 kuerlulu 于 2014-4-19 20:25 编辑
【2014年4月19日20:24:45 更新】
@taroxd 已更新,顺便加了些没用的= =
【惯(fei)例(hua)】做音乐游戏时为了校准时间出来的副产物
【特(bu)色(zu)】
1.在下只给出脚本没给出范例(噗
2.免dll
3.没有淡入淡出等特效
4.不会自动加入update队列
5.在下说这么多缺点肯定没人看了
6.歌曲和歌词文件请都放到Audio/BGM里
【正(jie)文(cao)】
class String @@MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i') @@WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i') def s2u i = @@MultiByteToWideChar.call(0, 0, self, -1, nil, 0) buffer = "\0" * (i*2) @@MultiByteToWideChar.call(0, 0, self, -1, buffer, buffer.size / 2) i = @@WideCharToMultiByte.call(65001, 0, buffer, -1, nil, 0, nil, nil) result = "\0" * i @@WideCharToMultiByte.call(65001, 0, buffer, -1, result, result.size, nil, nil) result.chop! return result end end class Lrc #-------------------------------------------------------------------------- # ● 初始化 #-------------------------------------------------------------------------- def initialize(songname) # 获取歌词 lrc = File.open("Audio/BGM/#{songname}.lrc").read.s2u.split("\n") # 分析歌词 @content = [] for line in lrc unless line[/(?<=\[)\d{2}:\d{2}.\d{2}(?=\])/].nil? t = line.split(']').size - 1 txt = line.split(']').size == 1 ? "" : line[10*t..-1] for i in 0..t l = line[(1+10*i)..(8+10*i)] min = l.split(':')[0].to_i a = l.split(':')[1].to_s sec = a.split('.')[0].to_i msec = a.split('.')[1].to_i unless min*60000+sec*1000+msec*10 == 0 @content.push([min*60000+sec*1000+msec*10, txt]) end end end end @content.sort! @content.push([1800000, '']) # 画图 @sprite = Sprite.new @sprite.bitmap = Bitmap.new(640,22) @sprite.x,@sprite.y = 0,458 #@content.each_with_index{|l,i|@sprite.bitmap.draw_text(0,i*22+2,640,22,l[1],1)} # 记时开始 [url=home.php?mod=space&uid=263426]@temp[/url] = "" start end #-------------------------------------------------------------------------- # ● 刷新精灵 #-------------------------------------------------------------------------- def update @sprite.opacity += 25 unless @sprite.opacity == 255 if now_lrc != @temp @sprite.bitmap.clear @sprite.bitmap.draw_text(0,0,640,22,now_lrc,1) @temp = now_lrc @sprite.opacity = 0 end end #-------------------------------------------------------------------------- # ● 释放精灵 #-------------------------------------------------------------------------- def dispose @sprite.bitmap.dispose @sprite.dispose @temp = nil @content = nil @start_time = nil end #-------------------------------------------------------------------------- # ● 获取当前一句歌词 #-------------------------------------------------------------------------- def now_lrc @content.each_with_index do | t, i | return @content[i-1][1] if current_time - t[0] <= 0 end end #-------------------------------------------------------------------------- # ● 调整纵坐标 #-------------------------------------------------------------------------- def y return @sprite.y end def y=(int) @sprite.y = int end #-------------------------------------------------------------------------- # ● 获取当前毫秒数 #-------------------------------------------------------------------------- def msec t = Time.now t.to_i * 1000 + t.usec / 1000 end #-------------------------------------------------------------------------- # ● 开始 重置 start_time #-------------------------------------------------------------------------- def start @start_time = msec end #-------------------------------------------------------------------------- # ● 获取时间 #-------------------------------------------------------------------------- def current_time msec - @start_time end end
class String
@@MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
@@WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
def s2u
i = @@MultiByteToWideChar.call(0, 0, self, -1, nil, 0)
buffer = "\0" * (i*2)
@@MultiByteToWideChar.call(0, 0, self, -1, buffer, buffer.size / 2)
i = @@WideCharToMultiByte.call(65001, 0, buffer, -1, nil, 0, nil, nil)
result = "\0" * i
@@WideCharToMultiByte.call(65001, 0, buffer, -1, result, result.size, nil, nil)
result.chop!
return result
end
end
class Lrc
#--------------------------------------------------------------------------
# ● 初始化
#--------------------------------------------------------------------------
def initialize(songname)
# 获取歌词
lrc = File.open("Audio/BGM/#{songname}.lrc").read.s2u.split("\n")
# 分析歌词
@content = []
for line in lrc
unless line[/(?<=\[)\d{2}:\d{2}.\d{2}(?=\])/].nil?
t = line.split(']').size - 1
txt = line.split(']').size == 1 ? "" : line[10*t..-1]
for i in 0..t
l = line[(1+10*i)..(8+10*i)]
min = l.split(':')[0].to_i
a = l.split(':')[1].to_s
sec = a.split('.')[0].to_i
msec = a.split('.')[1].to_i
unless min*60000+sec*1000+msec*10 == 0
@content.push([min*60000+sec*1000+msec*10, txt])
end
end
end
end
@content.sort!
@content.push([1800000, ''])
# 画图
@sprite = Sprite.new
@sprite.bitmap = Bitmap.new(640,22)
@sprite.x,@sprite.y = 0,458
#@content.each_with_index{|l,i|@sprite.bitmap.draw_text(0,i*22+2,640,22,l[1],1)}
# 记时开始
[url=home.php?mod=space&uid=263426]@temp[/url] = ""
start
end
#--------------------------------------------------------------------------
# ● 刷新精灵
#--------------------------------------------------------------------------
def update
@sprite.opacity += 25 unless @sprite.opacity == 255
if now_lrc != @temp
@sprite.bitmap.clear
@sprite.bitmap.draw_text(0,0,640,22,now_lrc,1)
@temp = now_lrc
@sprite.opacity = 0
end
end
#--------------------------------------------------------------------------
# ● 释放精灵
#--------------------------------------------------------------------------
def dispose
@sprite.bitmap.dispose
@sprite.dispose
@temp = nil
@content = nil
@start_time = nil
end
#--------------------------------------------------------------------------
# ● 获取当前一句歌词
#--------------------------------------------------------------------------
def now_lrc
@content.each_with_index do | t, i |
return @content[i-1][1] if current_time - t[0] <= 0
end
end
#--------------------------------------------------------------------------
# ● 调整纵坐标
#--------------------------------------------------------------------------
def y
return @sprite.y
end
def y=(int)
@sprite.y = int
end
#--------------------------------------------------------------------------
# ● 获取当前毫秒数
#--------------------------------------------------------------------------
def msec
t = Time.now
t.to_i * 1000 + t.usec / 1000
end
#--------------------------------------------------------------------------
# ● 开始 重置 start_time
#--------------------------------------------------------------------------
def start
@start_time = msec
end
#--------------------------------------------------------------------------
# ● 获取时间
#--------------------------------------------------------------------------
def current_time
msec - @start_time
end
end
【测试用脚本】
l = Lrc.new("无前缀后缀歌曲名") # 如 "周杰伦 - 枫" Audio.bgm_play("带前缀后缀的歌曲名") # 如 "Audio/BGM/周杰伦 - 枫.mp3" loop{Graphics.update;l.update}
l = Lrc.new("无前缀后缀歌曲名") # 如 "周杰伦 - 枫"
Audio.bgm_play("带前缀后缀的歌曲名") # 如 "Audio/BGM/周杰伦 - 枫.mp3"
loop{Graphics.update;l.update}
【补(luan)救(xiang)】
1.淡入淡出特效什么的在update那里加点东西就行了【在下就偷懒不写了
2.加入update队列可以考虑直接alias Graphics.update【在下真是垢了
3.范例真的就不需要了吧?! |
评分
-
查看全部评分
|