更新时间:2022-06-25 06:32:30
这是如何实现倒数计时器的概述.这既适用于实时倒数,也适用于模拟时间倒数.
This is a general outline of how to implement a count-down timer. This applies both to a real-time count-down, or a simulation-time count-down.
实施变量以包含剩余时间或经过的时间.
Implement a variable to contain the remaining time or elapsed time.
该变量通常是全局变量,除非每个代理必须具有自己的递减计数.然后,该变量将是代理的自有变量.
The variable is usually a global variable, unless each agent must have its own count-down. Then the variable will be a -own'd variable of the agent.
globals [ count-down ]
;; or
turtles-own [ count-down ]
我认为通常***跟踪剩余时间.递减计数变量使用递减计数的持续时间初始化.通过简单地增加或减去剩余时间,可以(在游戏中)轻松实现延长倒数计时的奖金和减少倒数的罚款.跟踪倒计时到期的实际"时间(使用timer + duration
或类似方法)通常没有什么用,特别是在您的游戏可以暂停的情况下.可能会产生不良后果,因此您必须编写代码.
I think it's generally best to track time-remaining. The count-down variable is initialized with the duration of the count-down. This makes it easy (in a game) to implement bonuses that extend the count-down, and penalties that reduce it, by simply adding to or subrtacting from the time remaining. Tracking the "actual" time that the count-down expires (using timer + duration
or something similar) is generally less useful, especially if your game can be paused. Undesired effects could occur that you would have to code around.
执行一个程序来初始化倒数计时.
Implement a procedure to initialize the count-down.
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
执行一个减少剩余时间的过程.
Implement a procedure to decrement the remaining time.
to decrement-timer
set count-down count-down - 1
end
执行一个程序来测试倒计时是否已过期.
Implement a procedure to test whether the count-down has expired.
to-report timer-expired?
report ( count-down <= 0 )
end
实现一种显示剩余时间或经过时间的方法.例如:
Implement a way to display the time remaining or elapsed time. For example:
使用补丁标签显示时间:
Use a patch label to show the time:
to update-timer-display
ask patch max-pxcor max-pycor [ set plabel count-down ]
end
使用带有时钟形状的特殊定义的乌龟来显示时间流逝. NetLogo模型库中存在这样的示例
use a specially defined turtle with a clock shape to show the time elapsing. Examples of this exist in the NetLogo Models Library
实施计时器到期时发生的操作.
Implement the action that occurs when the timer expires.
这完全取决于您.
This is entirely up to you.
这可能涉及重置计时器以进行另一次倒计时.
This may involve resetting the timer for another count-down.
在适当的地方初始化倒数计时器(例如游戏或一轮游戏开始时).
Initialise the count-down timer where appropriate (such as when the game, or a round of the game, begins).
更改并测试计时器.
这可能是打勾"一次,或者是基于实时的计算.
This might be once per "tick", or a calulation based on real time.
对到期的计时器执行操作.
Act on the expired timer.
;; 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
如果您需要的是每N个滴答触发一次重复事件的方法,则可以简单地将mod
运算符与ticks
计数器一起使用在您的go
程序中:
If what you need is a way to trigger recurring events every N ticks, you may be able to simply use themod
operator with theticks
counter in yourgo
procedeure:
if ticks mod 30 = 0 [ perform-recurring-event ]
以上代码行将导致过程perform-recurring-event
每当ticks
计数器达到0或30的倍数时运行.换句话说,它将每30 ticks
运行一次.
The above code line would cause the procedureperform-recurring-event
to run every time theticks
counter reached 0 or a multiple of 30. In other words, it would run every 30ticks
.