实现
init python:
# 获取当前label名
def label_callback(name, abnormal):
store.current_label = name
config.label_callback = label_callback
# 写入脚本分支结构
# 在分支处用‘/’标记
chapter_tree = """
|--start/
|--a0
|--a1
|--a2/
| |--b0
| |--b1#
| |--b2
| |--b3/
| | |--c0
| | |--c1
| | |--c2#
"""
def next_choice(tree,key):
index = tree.find(key)+len(key)
if tree[index] == '/' or tree[index] == '#':
return key
# 返回前缀‘/’和后缀‘|--’的下一个分支名
suffix_index1 = tree.find('/',index)
suffix_index2 = tree.find('#',index)
suffix_index = (suffix_index1 if suffix_index1 < suffix_index2 else suffix_index2)
prefix_index = tree.rfind('|--',index,suffix_index)
return tree[prefix_index+3:suffix_index]
def last_choice(tree,key):
index = tree.find(key)
suffix_index = tree.rfind('/',0,index)
prefix_index = tree.rfind('|--',0,suffix_index)
return tree[prefix_index+3:suffix_index]
调用
# 创建两个按钮放入 screen 测试用
style mystyle_button_text:
color "#f00"
hover_color "#fff"
screen choice_jump_btn:
style_prefix "mystyle"
vbox:
textbutton _("上一个选项") action Jump(last_choice(chapter_tree,store.current_label))
textbutton _("下一个选项") action Jump(next_choice(chapter_tree,store.current_label))
# 显示 screen choice_jump_btn
show screen choice_jump_btn
script.rpy 全文
创建新工程后替换可查看效果
# 游戏的脚本可置于此文件中。
init python:
# 获取当前label名
def label_callback(name, abnormal):
store.current_label = name
config.label_callback = label_callback
# 写入脚本分支结构
# 在分支处用‘/’标记
chapter_tree = """
|--start/
|--a0
|--a1
|--a2/
| |--b0
| |--b1#
| |--b2
| |--b3/
| | |--c0
| | |--c1
| | |--c2#
"""
def next_choice(tree,key):
index = tree.find(key)+len(key)
if tree[index] == '/' or tree[index] == '#':
return key
# 返回前缀‘/’和后缀‘|--’的下一个分支名
suffix_index1 = tree.find('/',index)
suffix_index2 = tree.find('#',index)
suffix_index = (suffix_index1 if suffix_index1 < suffix_index2 else suffix_index2)
prefix_index = tree.rfind('|--',index,suffix_index)
return tree[prefix_index+3:suffix_index]
def last_choice(tree,key):
index = tree.find(key)
suffix_index = tree.rfind('/',0,index)
prefix_index = tree.rfind('|--',0,suffix_index)
return tree[prefix_index+3:suffix_index]
# 创建两个按钮放入 screen 测试用
style mystyle_button_text:
color "#f00"
hover_color "#fff"
screen choice_jump_btn:
style_prefix "mystyle"
vbox:
textbutton _("上一个选项") action Jump(last_choice(chapter_tree,store.current_label))
textbutton _("下一个选项") action Jump(next_choice(chapter_tree,store.current_label))
# 声明此游戏使用的角色。颜色参数可使角色姓名着色。
define e = Character("艾琳")
# 游戏在此开始。
label start:
# 显示一个背景。此处默认显示占位图,但您也可以在图片目录添加一个文件
# (命名为“bg room.png”或“bg room.jpg”)来显示。
scene bg room
# 显示角色立绘。此处使用了占位图,但您也可以在图片目录添加命名为
# “eileen happy.png”的文件来将其替换掉。
show eileen happy
# 此处显示各行对话。
e "您已创建一个新的 Ren'Py 游戏。"
e "当您完善了故事、图片和音乐之后,您就可以向全世界发布了!"
# 显示 screen choice_jump_btn
show screen choice_jump_btn
label a0:
e "此处是章节 a0"
label a1:
e "此处是章节 a1"
label a2:
menu:
"此处是选择支a2"
"b0":
jump b0
"b2":
jump b2
return
label b0:
e "此处是章节 bo"
label b1:
e "此处是章节 b1,分支到此结束"
return
label b2:
e "此处是章节 b2"
label b3:
menu:
"此处是选择支b3"
"c0":
jump c0
return
label c0:
e "此处是章节 co"
label c1:
e "此处是章节 c1"
label c2:
e "此处是章节 c2,分支到此结束"
return
这篇博客介绍了如何在RenPy游戏引擎中实现跳转至上/下一个选项的功能,包括具体的实现方法、调用方式以及在`script.rpy`文件中的完整代码示例,通过创建新工程并替换内容来验证效果。
1968

被折叠的 条评论
为什么被折叠?



