让多只海龟同时移动

Make multiple turtles move all at the same time

节目的想法是什么:我们有看门狗,羊,狼。我的羊在浅滩里,狼从外面来了,一进入浅滩,看门狗就开始行动了。狼吃羊,羊吃草,看门狗要吓唬狼(暂未实现)

我的问题:所有的海龟一次移动一只。

我想要的:我希望它们在每个滴答声中同时移动。

代码:

    breed [sheeps sheep]
breed [wolves wolf]
breed [watchdogs watchdog]

turtles-own [attacked?]

globals [counter
  attack
  sheep-energy
  wolf-energy
  grass-counter
  lifespan
  age
  num-attacks
  death-countdown]

to set-variables-shapes
  set lifespan 365 * 10
    set-default-shape sheeps "sheep"
    set-default-shape watchdogs "dog"
    set-default-shape wolves "wolf"
     set sheep-energy 200
     set wolf-energy 400
     set death-countdown 60
end

to setup
  clear-all
 set-variables-shapes
 create-fold
 spawn-animals
  reset-ticks

end

to go

   ask turtles [
     move
   ]
    respawn-grass

  ask sheeps [
    if ( pcolor != green or pcolor != brown) [
      move-to one-of patches with [ pcolor = green or pcolor = brown] ;; If i change this with turn-around (procedure), all sheeps die at once.
    ]
    eat-grass
    reproduce
    sheep-status
    death
 show sheep-energy
  ]

  ask wolves [
    attack-sheep
    if (xcor < 5 and xcor > -14 and ycor < 15 and ycor > -5 ) [

      ask watchdogs [
    move
    if ( pcolor != green or pcolor != brown ) [
      move-to one-of patches with [ pcolor = green]
    ]
      ]
    ]
  ]

tick
end

to create-fold
  ask patches [
    if (pxcor < 6 and pxcor > -15 and pycor < 16 and pycor > -6)[
      set pcolor grey]
    if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5) [
      set pcolor green]
   ]

end


to sheep-status
  if (pcolor = brown) [
    set sheep-energy sheep-energy - (life-from-food / 2)]
  if attacked?
  [set color red]

 end


to attack-sheep
    ask other sheeps in-radius (size)[
      set attacked? true
        set color red
    ]

end
to reproduce
  if (age = lifespan / 10) [
    hatch-sheeps 40 [
      hatch-options
      ]
  ]
end

to eat-grass
  if (pcolor = green)[
    set pcolor brown
    set sheep-energy sheep-energy + life-from-food
  ]
end

to respawn-grass
  ask patches [
  if pcolor = brown [
    ifelse grass-counter = 0 [
      set pcolor green
      set grass-counter grass-respawn-timer
    ]
    [ set grass-counter grass-counter - 1]
  ]
  ]
  end
to death
  sheep-death-timer
  if (sheep-energy <= 0)
  [die]
  ;;if (age >= lifespan) [die]
  sheep-explode

end

to sheep-death-timer
  if (attacked?)[
  set death-countdown death-countdown - 1

  if (death-countdown  <= 30)
  [set color black]
  if (death-countdown <= 0)
  [die]
  ]
end

to sheep-explode
  if (sheep-energy >= 10000)[
  hatch-sheeps 20 [
    hatch-options]
  die
  ]
end

to hatch-options
  set sheep-energy 200
      set age 1
      set attacked? false
  end

to move
  fd 1
  rt random 90
  lt random 90
end

to spawn-animals
   create-wolves number-of-wolves [
      set color red
      setxy 10 -10
      set size 1.3
      ask patch 10 -10[
        Set plabel "Wolves"]
  ]
  create-sheeps number-of-sheeps [
     if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5 )[
        setxy -4.5 5]
     ask patch -4.5 15 [
       Set plabel "Sheeps"]
  set color white
  set attacked? false
   ]
 create-watchdogs number-of-dogs [
    if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5 )[
    setxy  -4.5 5
    set size 1.3]
  set color blue
  ]
end
to turn-around
  fd -1
  rt 180
end

您可以使用

ask-concurrent sheeps [move ]

让每个绵羊代理都同时移动,不过应该没关系,他们实际上不建议你使用它。

NetLogo中没有"at the same time"这样的东西。 NetLogo 是单线程的,不支持并行处理。

如果所有代理在同一个时间点内移动,您可以将其视为 "at the same time",因为这一切都发生在一个时间点内。

但是在一个滴答声中,除了一次移动一个以外,代理几乎没有其他办法。