我想在NetLogo中制作类似Frogger的游戏,我需要创建一个倒计时的计时器.但是,我查看了Frogger并使用了创建计时器的相同程序,但它不起作用.请指教.
这是如何实现倒计时器的概述.这适用于实时倒计时或模拟时间倒计时.
实现变量以包含剩余时间或已用时间.
变量通常是一个全局变量,除非每个代理必须有自己的倒计时.然后变量将是代理的一个变量.
globals [ count-down ]
;; or
turtles-own [ count-down ]
Run Code Online (Sandbox Code Playgroud)我认为通常最好跟踪剩余时间.倒计数变量初始化为倒计时的持续时间.这样可以轻松(在游戏中)实现延长倒计时的奖励,以及通过简单地从剩余时间添加或减少来减少它的惩罚.跟踪倒计时到期的"实际"时间(使用timer + duration或类似的东西)通常不太有用,特别是如果您的游戏可以暂停.您可能需要编写不受欢迎的效果.
实施一个程序来初始化倒计时.
to setup-timer
set count-down 30 ;; a 30 tick timer
;; if you have a display of the remaining time,
;; you might want to initialize it here, for example:
ask patch max-pxcor max-pycor
[ set plabel-color white
set plabel count-down
]
end
;; this example is for global count-down.
;; for a per-agent count-down, each agent would need
;; to initialize its own count-down variable
Run Code Online (Sandbox Code Playgroud)实施一个程序来减少剩余时间.
to decrement-timer
set count-down count-down - 1
end
Run Code Online (Sandbox Code Playgroud)实施一个程序来测试倒计时是否已过期.
to-report timer-expired?
report ( count-down <= 0 )
end
Run Code Online (Sandbox Code Playgroud)实现一种显示剩余时间或已用时间的方法.例如:
使用补丁标签显示时间:
to update-timer-display
ask patch max-pxcor max-pycor [ set plabel count-down ]
end
Run Code Online (Sandbox Code Playgroud)使用具有时钟形状的特别定义的乌龟来显示流逝的时间.NetLogo模型库中存在此示例
实现计时器到期时发生的操作.
这完全取决于你.
这可能涉及重置计时器以进行另一次倒计时.
在适当的时候初始化倒计时计时器(例如游戏或一轮游戏开始时).
更改并测试计时器.
这可能是每"tick"一次,或基于实时的计算.
对过期的计时器采取行动.
;; a "once-per-tick" count-down
decrement-timer
update-timer-display
if timer-expired? [ act-on-expired-timer ]
;; rest of the go procedure, then...
tick
;; a "once-per-second" count-down
every 1 ;; this block runs only once per second
[ decrement-timer
update-timer-display
if timer-expired? [ act-on-expired-timer ]
]
;; the rest of the go procedure
tick
Run Code Online (Sandbox Code Playgroud)如果你需要的是一种每N个刻度触发重复事件的方法,你可以在你的程序中简单地使用mod操作员和ticks计数器go:
if ticks mod 30 = 0 [ perform-recurring-event ]
Run Code Online (Sandbox Code Playgroud)
上面的代码行将导致程序perform-recurring-event在每次ticks计数器达到0或30的倍数时运行ticks.换句话说,它将每30运行一次.
| 归档时间: |
|
| 查看次数: |
3962 次 |
| 最近记录: |