title: How to make turtles move around patches with specific rules?
body: <p>I am new to Netlogo, and have some questions. That would be great if you could help me.</p>
  
  <p>I would like to create some fruit flies moving around a tree that is made up by green patches. Fruit flies are attracted to the tree. They will turn back to the tree if fruit flies move away certain distance (such as 5 grids) from the tree.</p>
  
  <p>In the beginning, they will not stop on the green patches because they have enough energy. As time passed, they will loss their energy. They will find the closest green patch, and stay on it for certain time once their energy reaches 0. They gain energy after that, and they only can do short hops from the bottom branch to the top one (3 hops). Flies will move back to the bottom branch when they are on the top part of tree. I think I need to do a WHILE loop, but I have no idea how to do that. Please look at my codes.</p>
  
  <pre><code>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 &lt; min-pycor + 2]
      [ set pcolor 66 ]
  
    ;; Create trunk and branches
  
     ask patches with [ pxcor = -15 and pycor &lt;= 0 ] [ set pcolor brown ]
  
     ask patches with [ pxcor = -15 and pycor &lt; 8 and pycor &gt; 0] [ set pcolor lime ]
     ask patches with [ pxcor = pycor - 15 and pycor &lt;= 5 and pycor &gt; 0 ] [ set pcolor lime ]
     ask patches with [ pxcor = (- pycor) - 15 and pycor &lt;= 5 and pycor &gt; 0 ] [ set pcolor lime ]
     ask patches with [ pxcor = pycor - 8 and pycor &lt;= 2 and pxcor &gt; -15 ] [ set pcolor lime ]
     ask patches with [ pxcor = (- pycor) - 22 and pycor &lt;= 2 and pxcor &lt; -15 ] [ set pcolor lime ]
     ask patches with [ pxcor = pycor - 1 and pycor &lt;= -1 and pxcor &gt; -15 ] [ set pcolor lime ]
     ask patches with [ pxcor = (- pycor) - 29 and pycor &lt;= -1 and pxcor &lt; -15 ] [ set pcolor lime ]
  
     ask patches with [ pxcor = 15 and pycor &lt;= 0 ] [ set pcolor brown ]
     ask patches with [ pxcor = 15 and pycor &lt; 8 and pycor &gt; 0] [ set pcolor lime ]
     ask patches with [ pxcor = pycor + 15 and pycor &lt;= 5 and pycor &gt; 0 ] [ set pcolor lime ]
     ask patches with [ pxcor = (- pycor) + 15 and pycor &lt;= 5 and pycor &gt; 0 ] [ set pcolor lime ]
     ask patches with [ pxcor = pycor + 22 and pycor &lt;= 2 and pxcor &gt; 15 ] [ set pcolor lime ]
     ask patches with [ pxcor = (- pycor) + 8 and pycor &lt;= 2 and pxcor &lt; 15 ] [ set pcolor lime ]
     ask patches with [ pxcor = pycor + 29 and pycor &lt;= -1 and pxcor &gt; 15 ] [ set pcolor lime ]
     ask patches with [ pxcor = (- pycor) + 1 and pycor &lt;= -1 and pxcor &lt; 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 &gt; 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
  </code></pre>
  
  <p>I am sorry if you are confused by my codes. I appreciate your help. Thanks.</p>
  
  <p>Kind Regards,</p>
  
  <p>Ming</p>