【问题标题】:Making turtles wait x number of ticks让海龟等待 x 个滴答声
【发布时间】:2014-03-25 12:15:53
【问题描述】:

我正在尝试做的部分工作是让一种乌龟四处移动,但是当一个乌龟到达目的地时,它会等待一定数量的滴答声然后继续?也可以让海龟根据目的地(不同的补丁颜色)等待不同数量的滴答声。是制作海龟品种还是全局变量来计算蜱的数量?希望相关的代码如下。

【问题讨论】:

标签: wait netlogo


【解决方案1】:

你是对的,这可以通过让海龟计算它们在补丁上的蜱数来完成。这也必须是一个海龟变量,而不是一个全局变量,因为每个海龟都有不同的值

我使用的方法是这样的:

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

    breed [visitors 访客]

    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
    

【讨论】:

    猜你喜欢
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多