若无特殊说明,默认是在script.rpy
文件中进行编写
参考资料 本文参考Ren’py中文论坛文章:[原创] [教程]音乐进度条显示,全长和已播放进度,附加播放/暂停
Ren’py官方文档相关链接:
音乐空间
时长位置
更新时长
视频教程 基础部分
进阶操作
前提条件 想要实现音乐空间需要一些前提条件,详见下述代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 image music_background: "music_background.png" size (1920 , 1080 ) init python: mr = MusicRoom(fadein = 1.0 , fadeout = 1.0 ) mr.add("bgm01.ogg" ,always_unlocked = True ) mr.add("bgm02.ogg" ) mr.add("bgm03.ogg" ) mr.add("bgm04.ogg" ) init python: def get_audio_duration (channel="music" ): duration = renpy.music.get_duration(channel) return convert_format(int (duration)) def get_audio_position (channel="music" ): music_pos = renpy.music.get_pos(channel) if music_pos: return convert_format(int (music_pos)) return "00:00" def convert_format (second ): minute = second // 60 second = second % 60 result = "" if minute: if minute < 10 : result = '0' + str (minute) + ":" + str (second) if second < 10 : result ='0' + str (minute) + ":" '0' + str (second) else : result = str (minute) + ":" + str (second) if second < 10 : result = str (minute) + '0' + str (second) else : if second < 10 : result = '00:0' + str (second) else : result = '00:' + str (second) return result
界面编写 前提条件编写完成后,我们就可以着手编写音乐空间界面了,详见下述代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 screen music_room: timer 0.1 : action [SetVariable('duration' ,get_audio_duration()),SetVariable('music_pos' ,get_audio_position())] repeat True zorder 2 tag menu add "music_background" viewport id "music_list" : mousewheel True xysize (500 , 500 ) align (0.3 , 0.4 ) draggable True vbox: spacing 50 textbutton "bgm01" action mr.Play("bgm01.ogg" ) if mr.is_unlocked("bgm02.ogg" ): textbutton "bgm02" action mr.Play("bgm02.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm03.ogg" ): textbutton "bgm03" action mr.Play("bgm03.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm04.ogg" ): textbutton "bgm04" action mr.Play("bgm04.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm04.ogg" ): textbutton "bgm04" action mr.Play("bgm04.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm04.ogg" ): textbutton "bgm04" action mr.Play("bgm04.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm04.ogg" ): textbutton "bgm04" action mr.Play("bgm04.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm04.ogg" ): textbutton "bgm04" action mr.Play("bgm04.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm04.ogg" ): textbutton "bgm04" action mr.Play("bgm04.ogg" ) else : textbutton "???" action NullAction() if mr.is_unlocked("bgm04.ogg" ): textbutton "bgm04" action mr.Play("bgm04.ogg" ) else : textbutton "???" action NullAction() vbar: xysize (10 , 500 ) align (0.9 , 0.4 ) value YScrollValue("music_list" ) hbox: align (0.5 , 0.8 ) spacing 200 textbutton "下一首" action mr.Next() imagebutton: idle "pause.png" hover "pause.png" selected_idle "continue.png" selected_hover "continue.png" if not renpy.music.is_playing() and not renpy.music.get_pause(): action mr.Play("bgm01.ogg" ) else : action PauseAudio(channel="music" ,value="toggle" ) textbutton "上一首" action mr.Previous() textbutton "随机播放" action mr.RandomPlay() bar: value AudioPositionValue(channel='music' , update_interval=0.1 ) xysize (800 ,5 ) align (0.5 , 0.7 ) thumb "dot.png" thumb_offset 3 vbox: xpos 0.5 ypos 0.2 python: duration = get_audio_duration() music_pos = get_audio_position() hbox: spacing 20 align (0.8 , 0.7 ) text music_pos text "/" text duration textbutton "返回" : align (0.0 , 0.98 ) action Return() on "replace" action mr.Play() on "replaced" action Play("music" , "bgm01.ogg" )
添加入口 完成上述操作后,我们还需要添加主界面进入按钮,打开screens.rpy
,找到screen navigation()
(287行左右),在合适位置添加下面的代码1 textbutton "音乐空间" action ShowMenu("music_room" )
现在运行游戏,即可在主界面看到音乐空间进入按钮,请检查各功能是否可用。