2014-01-12 34 views
1

我正在Netlogo上創建一個迷宮的項目,我想對它編碼,以便移動的龜碰到一個不動的烏龜(代表一個障礙物) )在另一個地方,然後它打開了一個「迷你遊戲」,我將創建。然而,當我包含更多的海龜並在我的按鈕中使用問龜0以使其他海龜不移動並保持障礙時,一切都開始滯後。我將如何能夠使用全局變量來解決問題?如何通過Netlogo上的全局變量控制一隻海龜

+0

這可能是更有益的,如果你可以包括與此問題相關的代碼 – Marzy

+0

第一個建議可能是使用補丁,而不是使用海龜的障礙,因爲他們不動! – Marzy

+0

這是一個好主意,但我正在考慮爲烏龜使用不同的形狀... – user3188146

回答

2

這是使用兩個品種同一型號:

breed [Walls wall] 
patches-own [is-wall] 
breed [Humans person] 
Humans-own [target] 

to setup 
    clear-all 
    set-default-shape Walls "tile brick" 
    set-default-shape Humans "person" 
    set-patch-size 25 


    create-humans 1 [ 
    set heading 90 
    set color white 
    move-to patch -5 -5 
    set target one-of patches with [not any? walls-here] 
    ask target [set pcolor green]] 
    set-walls 
    reset-ticks 
end 

to set-walls 
    ask n-of 10 patches with [not any? humans-here] 
    [ 
    set pcolor red 
    sprout-walls 1 
    [ 
     set color brown 
    ] 
    ] 
end 

to go 
    ask humans [ 

    ifelse pcolor != green 
     [ 
     ifelse [pcolor] of patch-ahead 1 != red 
     [ 
      Your-Move-Function 
     ] 
     [ 
      Your-Bounce-Function 
     ] 

     leave-a-trail 
     ] 
     [ 
     stop 

     ] 


    ] 


    tick 
end 


to Your-Move-Function 
    let t target 
    face min-one-of all-possible-moves [distance t] 
    fd 1 
end 

to Your-Bounce-Function 
    let t target 
    face min-one-of all-possible-moves [distance t] 
end 

to-report all-possible-moves 
    report patches in-radius 1 with [not any? walls-here and distance myself <= 1 and distance myself > 0 and plabel = "" ] 
end 

to leave-a-trail 
    ask patch-here [set plabel ticks] 
end 

enter image description here

enter image description here