让海龟等待x滴答声 [英] Making turtles wait x number of ticks

查看:96
本文介绍了让海龟等待x滴答声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的部分工作是让一只乌龟到处走动,但是当一只乌龟到达目的地时,乌龟要等待一定的滴答声才能继续?也有可能使海龟根据其目的地(不同的斑块颜色)等待不同数量的of.是制作海龟品种或全局变量来计算of的数量吗?希望相关的代码如下.

Part of what I am trying to do is make a breed of turtles move around, but when one reaches its destination that turtle waits for a certain number of ticks before continuing ? Also is it possible to make turtles wait for different number of ticks depending upon their destination ( different patch colors). Is it a case of making a turtle breed or global variable to count the number of ticks? The hopefully relevant code is below.

推荐答案

您是对的,可以通过让海龟计算它们在补丁上的s虫数量来实现.另外,这必须是乌龟变量,而不是全局变量,因为每只乌龟对此都有不同的值

You are right, this can be done by making the turtles count the number of ticks they have been on a patch. Also this has to be a turtle variable and not a global variable since each turtle will have a different value for this

我使用的方法是:

  1. 乌龟到达目的地后,将ticks(记录到现在为止已经过去的滴答声数量的全局变量)记录到乌龟变量中,例如ticks-since-here.这就像时间戳一样.
  2. 在每个连续的刻度上,检查当前时间ticks全局变量和ticks-since-here turtle变量之间的差.如果该数量大于允许乌龟停留在补丁上的滴答声的数量,请让它选择并移动到新的目的地.

  1. Once the turtle arrives at its destination record the ticks (the global variable which records the number of ticks that have passed till now) into a turtle variable say ticks-since-here. This works like a time-stamp.
  2. On each successive tick check the difference between the current-time ticks global variable and the ticks-since-here turtle variable. If this becomes greater than the number of ticks the turtle is allowed to stay on the patch, let it choose and move to the new destination.

品种[访客来访者]

globals [ number-of-visitors ]

visitors-own [
  ; visitors own destination 
  destination
  ticks-since-here
]

to go
  ask visitors [
    move
  ]
  tick
end

to move
  ; Instructions to move the agents around the environment go here
  ; comparing patch standing on to dest, if at dest then  choose random new dest
  ; then more forward towards new dest
  ifelse ( patch-here = destination ) 
  [
    if ticks - ticks-since-here > ticks-to-stay-on-patch patch-here
    [
      set ticks-since-here 0
      set destination one-of patches with 
      [
        pcolor = 65 or pcolor = 95 or pcolor = 125 or pcolor = 25 or pcolor = 15 or pcolor = 5
      ]
    ]
  ]
  [
    face destination
    forward 1
    if ( patch-here = destination ) 
    [
      set ticks-since-here ticks
    ]
  ]
end

to-report ticks-to-stay-on-patch [p]  
  if [pcolor] of p = 65
    [
      report 6
    ]
  if [pcolor] of p = 95
    [
      report 5
    ]
  if [pcolor] of p = 125
    [
      report 4
    ]
  if [pcolor] of p = 25
    [
      report 3
    ]
  if [pcolor] of p = 15
    [
      report 2
    ]
  if [pcolor] of p = 5
    [
      report 1
    ] 
end

to setup-people 
  ;;;; added the following lines to facilitate world view creation
  ask patches
  [
    set pcolor one-of [65 95 125 25 15 5]
  ]
  set number-of-visitors 100
  ;;;;

  create-visitors number-of-visitors 
  [
    ask visitors 
    [
      ; set the shape of the visitor to "visitor"
      set shape "person"
      ; set the color of visitor to white
      set color white
      ; give person a random xy
      setxy (random 50) (random 50)
      ; set visitors destination variable
      set destination one-of patches with 
      [
        pcolor = 65 or pcolor = 95 or pcolor = 125 or pcolor = 25 or pcolor = 15 or pcolor = 5
      ]
    ]
  ]  
end

这篇关于让海龟等待x滴答声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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