且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在NetLogo中创建倒数计时器?

更新时间:2022-06-25 06:32:30

如何在NetLogo中创建倒数计时器

这是如何实现倒数计时器的概述.这既适用于实时倒数,也适用于模拟时间倒数.

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.

  1. 实施变量以包含剩余时间或经过的时间.

  1. 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.

    要在程序中使用倒数计时器,您必须:

    1. 在适当的地方初始化倒数计时器(例如游戏或一轮游戏开始时).

    1. 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.

    1. 减少倒计时的递减计数.
    2. 根据需要更新计时器的显示.
    3. 测试计时器以查看其是否已过期.
    4. 对到期的计时器执行操作.

    1. Decrement the count-down on whatever shedule is appropriate.
    2. Update the display of the timer, if desired.
    3. Test the timer to see if it has expired.
    4. 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程序中:

    Recurring Events

    If what you need is a way to trigger recurring events every N ticks, you may be able to simply use themodoperator with thetickscounter in yourgoprocedeure:

    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-eventto run every time thetickscounter reached 0 or a multiple of 30. In other words, it would run every 30ticks.