【问题标题】:Ask turtles to change colour and stop when they meet other turtles Netlogo让乌龟在遇到其他乌龟时改变颜色并停下来 Netlogo
【发布时间】:2020-11-02 04:01:03
【问题描述】:

我有两只乌龟,颜色分别为“绿色”和“蓝色”。我想当绿海龟遇到蓝海龟时,蓝海龟变白停止移动,而绿海龟继续随机移动。这是我的代码,我这样做了,但它给出了 Expected a literal value 的错误,并且没有返回预期的结果。非常感谢任何帮助。

ask turtles with [color = green]
  [if any? other turtles-here with [color = blue]
    [set turtles with [color = blue] [color = white]
]
  ]

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    你忘了ask(指示)海龟。关键字 with 子集正确的海龟,但您需要 set 一个变量,而不是 set 一个海龟集。此外,'=' 用于评估(比较),而不是用于分配值。我想你想要这个:

    ask turtles with [color = green]
    [ if any? other turtles-here with [color = blue]
      [ ask turtles-here with [color = blue]
        [ set color white
        ]
      ]
    ]
    

    请注意,我必须再次写turtles-here,否则所有的蓝色乌龟都会变成白色。一种更易读的同时减少错误的方法是为相关的海龟设置一个临时变量(使用let)。

    ask turtles with [color = green]
    [ let changers other turtles-here with [color = blue]
      if any? changers
      [ ask changers
        [ set color white
        ]
      ]
    ]
    

    【讨论】:

    • 提出一个新问题。至少,你必须向我们展示你用来移动海龟的代码,以及你正在做什么来试图让它们停止移动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    相关资源
    最近更新 更多