Netlogo 如何让海龟在有特定规则的补丁中移动?

Netlogo 如何让海龟在有特定规则的补丁中移动?,netlogo,Netlogo,我是Netlogo的新手,有一些问题。如果你能帮助我那就太好了 我想创造一些果蝇在由绿色斑块组成的树周围移动。果蝇被树吸引住了。如果果蝇离开树一定距离(如5格),它们会回到树上 一开始,它们不会停在绿色斑块上,因为它们有足够的能量。随着时间的推移,他们会失去精力。他们会找到最近的绿色区域,一旦能量达到0,就会在上面停留一段时间。之后它们获得能量,并且它们只能从底部分支跳到顶部分支(3跳)。当苍蝇在树的顶部时,它们会移回底部的树枝。我想我需要做一个WHILE循环,但我不知道怎么做。请看我的密码 b

我是Netlogo的新手,有一些问题。如果你能帮助我那就太好了

我想创造一些果蝇在由绿色斑块组成的树周围移动。果蝇被树吸引住了。如果果蝇离开树一定距离(如5格),它们会回到树上

一开始,它们不会停在绿色斑块上,因为它们有足够的能量。随着时间的推移,他们会失去精力。他们会找到最近的绿色区域,一旦能量达到0,就会在上面停留一段时间。之后它们获得能量,并且它们只能从底部分支跳到顶部分支(3跳)。当苍蝇在树的顶部时,它们会移回底部的树枝。我想我需要做一个WHILE循环,但我不知道怎么做。请看我的密码

breed [flies fly]
breed [suns sun]

turtles-own [energy]
flies-own [count-down]


to setup 

  clear-all 
  setup-suns
  setup-flies 
  setup-patches 

  reset-ticks 

end

to setup-suns
  ;; Create the sun

   set-default-shape suns "sun"

   create-suns 1 [
     setxy max-pxcor - 3
     max-pycor - 3
     set color yellow
     set size 7
   ]
end

to setup-flies

  set-default-shape flies "bee 2"
  create-flies number-fly [ set color white setxy random-xcor random-ycor set count-down 15]

end

to setup-patches

  ;; Create sky and grass

  ask patches
    [ set pcolor blue  ]
  ask patches with [pycor < min-pycor + 2]
    [ set pcolor 66 ]

  ;; Create trunk and branches

   ask patches with [ pxcor = -15 and pycor <= 0 ] [ set pcolor brown ]

   ask patches with [ pxcor = -15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
   ask patches with [ pxcor = pycor - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor - 8 and pycor <= 2 and pxcor > -15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) - 22 and pycor <= 2 and pxcor < -15 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor - 1 and pycor <= -1 and pxcor > -15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) - 29 and pycor <= -1 and pxcor < -15 ] [ set pcolor lime ]

   ask patches with [ pxcor = 15 and pycor <= 0 ] [ set pcolor brown ]
   ask patches with [ pxcor = 15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
   ask patches with [ pxcor = pycor + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor + 22 and pycor <= 2 and pxcor > 15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) + 8 and pycor <= 2 and pxcor < 15 ] [ set pcolor lime ]
   ask patches with [ pxcor = pycor + 29 and pycor <= -1 and pxcor > 15 ] [ set pcolor lime ]
   ask patches with [ pxcor = (- pycor) + 1 and pycor <= -1 and pxcor < 15 ] [ set pcolor lime ]   

   ask patches with [ pxcor = -26 and pycor = -3 ] [ set pcolor red ]  
   ask patches with [ pxcor = -10 and pycor = 5 ] [ set pcolor red ]            
   ask patches with [ pxcor = 21 and pycor = -1 ] [ set pcolor red ] 



end 

to go 

  move-flies 
  tick

end

to move-flies

  ask flies [ 

    set energy 6

    ifelse energy > 10 [

      ;rt random 50 lt random 50 jump random-float 1 ;

      let nearest-leaf min-one-of (patches with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 5 grids - long range search.

      if is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf. 
       face nearest-leaf 
       ;set heading 45
       ;fd distance nearest-leaf
       rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
       ;move-to nearest-leaf ;
     ]
    ]

    [
     ifelse pcolor != lime 
     [ rt random 50 lt random 50 jump random-float 1 ; Initialization - flies fly around to search their resources.
       continue]
     [ stay ] 

    ]
 ] 

end

to continue 

  let nearest-leaf min-one-of (patches in-radius 1 with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 2 grids - short hops.

  ifelse is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf. 
     face nearest-leaf 
     fd random distance nearest-leaf
     ;ask patches in-radius 1 [set pcolor red]
     ;rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
     ;move-to nearest-leaf ;
  ]
  [ 
    let turn-back min-one-of (patches with [pcolor = lime] ) [distance myself] ;
    ;set heading 180
    face turn-back
    jump random-float 5  ] 

    move-up ;Flies tend to move up through all branches by short hops. Need a while loop.

    ; Move down if they reach the top of tree
    let canopy patch-at-heading-and-distance 0 1
    let canopy-left patch-left-and-ahead 45 1
    let canopy-right patch-right-and-ahead 45 1

    if canopy != lime or canopy-left != lime or canopy-right != lime
       [
         move-down
       ] 

end

to move-up
  ask flies [
     set heading one-of [0 30 -30] 
  ]
end 

to move-down
  ask flies [
     set heading one-of [180 120 -120] 
  ]
end 

to stay

 set count-down count-down - 1 ;;decrement timer
 set label count-down
 if count-down = 0
  [
    rt random 50 lt random 50 jump random-float 1
    set label ""
    reset-count-down ;;it's another procedure   
  ]

end

to reset-count-down

  set count-down 30

end
繁殖[苍蝇飞]
繁殖[太阳]
海龟拥有[能量]
苍蝇自己的[倒计时]
设置
清除所有
设置太阳
设置苍蝇
安装补丁
重置滴答声
结束
设置太阳
;; 创造太阳
将默认形状太阳设置为“太阳”
创造太阳1[
setxy最大pxcor-3
马克斯·皮科尔-3
设置颜色为黄色
7号套餐
]
结束
设置苍蝇
将默认形状设置为“蜜蜂2”
创建苍蝇数苍蝇[设置颜色白色setxy random xcor random ycor设置倒计时15]
结束
设置修补程序
;; 创造天空和草地
询问补丁
[设置颜色为蓝色]
使用[pycor使用[pxcor=(-pycor)询问修补程序-22和pycor从您的描述中,我认为您几乎肯定不需要
while
循环。在NetLogo模型的
go
过程中,您只指定在一个勾号中发生的事情。因此,如果需要它来表示在一瞬间发生的事情,您只会使用
while
,而不是一个展开的过程在多次滴答声中


(同意Frank的观点,这是太多的代码,无法发布,因此很难获得帮助。阅读和研究这么多代码需要相当长的时间,更不用说尝试帮助您了。在这个回答中,我尝试提取并解决了一个方面。)

你能给出一个你遇到的问题的具体例子吗?更好的办法是将问题和代码浓缩起来,在尽可能少的空间内举例说明这个问题。