How can one create a countdown timer in NetLogo?
I am tryin开发者_如何学编程g to make a Frogger-like game in NetLogo and I need to create a timer that counts down. However, I looked in Frogger and used the same procedures that create the timer but it does not work. Please advise.
How to create a count-down timer in NetLogo
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 ]
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
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.
To use the count-down timer in your program you must:
Initialise the count-down timer where appropriate (such as when the game, or a round of the game, begins).
Change and test the timer.
This might be once per "tick", or a calulation based on real time.
- Decrement the count-down on whatever shedule is appropriate.
- Update the display of the timer, if desired.
- Test the timer to see if it has expired.
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
Recurring Events
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 ]
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
.
精彩评论