这个功能很多游戏都支持,但是查找了下发现有古早renpy文档讲如何支持这个功能。但是阅读文档《对话历史》这个章节,发现里面其实有个变量叫voice的,可以拿到voice文件。从而实现播放语音的功能。下面这个代码是比较简略版本,用了一个textbutton来播放,大家也可以直接写自己需要的。
文档:https://www.renpy.cn/doc/history.html
核心实现播放语音的代码如下,需要把他加入到history这个screen里面去。这个代码的逻辑就是判断,是不是有voice,如果有,点击这个按钮就会播放这个语音。
if h.voice.filename:
textbutton "播放":
action Play('voice', h.voice.filename)
下面是完整的代码:
修改label start用来测试
label start:
scene
show street night
voice "voice/voice1.ogg"
"这里是测试语音播放按钮。"
"这一句是没有语音的。"
voice "voice/voice2.ogg"
"这一句是有语音的。"
"结束!"
return 修改history screen,把刚才的核心代码加入进去。
screen history():
tag menu
## 避免预缓存此界面,因为它可能非常大。
predict False
use game_menu(_("历史"), scroll=("vpgrid" if gui.history_height else "viewport"), yinitial=1.0):
style_prefix "history"
for h in _history_list:
window:
## 此语句可确保如果“history_height”为“None”的话仍可正常显示条
## 目。
has fixed:
yfit True
if h.who:
label h.who:
style "history_name"
substitute False
## 若角色颜色已设置,则从“Character”对象中读取颜色到叙述
## 人文本中。
if "color" in h.who_args:
text_color h.who_args["color"]
$ what = renpy.filter_text_tags(h.what, allow=gui.history_allow_tags)
text what:
substitute False
if h.voice.filename:
textbutton "播放":
action Play('voice', h.voice.filename)
if not _history_list:
label _("尚无对话历史记录。")