官方文档论坛
统计游戏时间并显示在主界面
预先准备
renpy.get_game_runtime()
返回游戏运行时间计数器。
游戏运行时间计数器返回用户从顶层上下文(context)等待用户输入经过的秒数。(在主菜单和游戏菜单消耗的时间不计入。)
renpy.clear_game_runtime()
重置游戏运行时间计数器。
define config.python_callbacks = [ ]
一个函数列表。列表中的函数会在初始化阶段之外的任何时候被调用,不使用任何入参。
这种函数的可能用途之一,是某个变量每次调整后,都需要使用一个函数将其值限制在某个范围内的情况。
当Ren’Py启动时游戏未启动前,这些函数就可以被调用,而且可能这些函数相关的变量还未进行初始化。这些函数被要求处理这种情况,通过使用 hasattr(store, 'varname')检查某个变量是否定义过。
持久化数据
Ren’Py支持持久化数据,保存游戏中与某个特定时间点无关的数据。绑定了变量 persistent 的持久化对象字段(field),可以读写并实现持久化数据。
实现
实时保存游玩时间 script.rpy
init python
:
if persistent
.runtime
is None:
persistent
.runtime
= 0
def calc_total_run():
persistent
.runtime
+= renpy
.get_game_runtime
()
renpy
.clear_game_runtime
()
config
.python_callbacks
.append
(calc_total_run
)
展示在主界面 screens.rpy
screen main_menu
():
tag menu
style_prefix
"main_menu"
add gui
.main_menu_background
frame
:
pass
use navigation
if gui
.show_name
:
vbox
:
text
"[config.name!t]":
style
"main_menu_title"
text
"[config.version]":
style
"main_menu_version"
$ m
, s
= divmod(int(persistent
.runtime
), 60)
$ h
, m
= divmod(m
, 60)
text
"[h]:[m]:[s]"