如何在 NetLogo 中创建倒数计时器? [英] How can one create a countdown timer in NetLogo?

查看:29
本文介绍了如何在 NetLogo 中创建倒数计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 NetLogo 中制作一个类似 Frogger 的游戏,我需要创建一个倒计时的计时器.但是,我查看了 Frogger 并使用了创建计时器的相同程序,但它不起作用.请指教.

I am trying 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.

推荐答案

如何在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.

  • 该变量通常是一个全局变量,除非每个代理必须有自己的倒计时.那么该变量将是代理的 -own'd 变量.

  • 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的倍数时运行.换句话说,它将每 30ticks 运行一次.

    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.

    这篇关于如何在 NetLogo 中创建倒数计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆