我正在使用Netlogo为我的大学工作,我真的被困了。 我刚刚开始使用Netlogo,我正在尝试与一些朝圣者一起重建Mekka。
我一直在尝试很多不同的代码,添加新的代码,试一试,删除一些代码,但这就是我想到的:
turtles-own
[direction ;; 1 follows right-hand wall, -1 follows left-hand wall
way-is-clear? ;; reporter - true if no wall ahead
checked-following-wall?]
globals [halfedge]
breed [agents agent ]
agents-own [ around visible ]
to setup
create-agents 500 [
set color green
set size 2
; distribute agents randomly
setxy pxcor = halfedge pycor = halfedge
set heading random 360
; ensure that each is on its own patch
while [any? other agents-here] [ fd 1 ]
]
end
to bounce
if [pcolor] of patch-at dx 0 = blue [
set heading (- heading)
]
if [pcolor] of patch-at 0 dy = blue [
set heading (180 - heading)
]
end
to go
ask agents [ count-those-around ]
ask agents [ move ]
end
; store the number of agents surrounding me within
; local-radius units
; and the agents that I can see within visible-radius
to count-those-around
set around count agents with [self != myself] in-radius
local-radius
set visible agents with [self != myself] in-radius
visible-radius
end
to move
;; turn right if necessary
if not wall? (90 * direction) and wall? (135 * direction) [ rt 90 * direction ]
;; turn left if necessary (sometimes more than once)
while [wall? 0] [ lt 90 * direction ]
;; move forward
fd 1
end
; face towards the most popular local spot
to face-towards
face max-one-of visible [around]
end
; face away from the most popular local spot
to face-away
set heading towards max-one-of visible [around] - 180
end
to setup-center
clear-all
set halfedge int (edge / 2)
ask patches[
if (pxcor = (- halfedge) and pycor >= (- halfedge) and pycor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws left edge in blue
if ( pxcor = (0 + halfedge) and pycor >= (- halfedge) and pycor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws right edge in blue
if ( pycor = (- halfedge) and pxcor >= (- halfedge) and pxcor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws bottom edge in blue
if ( pycor = (0 + halfedge) and pxcor >= (- halfedge) and pxcor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws upper edge in blue
]
end
现在成功绘制了kaaba,唯一的问题是海龟不应该在那里产卵或遇到它(因此是凹凸代码)。 此外,他们随机四处走动,我不知道如何让他们按照一个不同颜色的领导者的反向点击形式移动。
你们中的任何人能帮助我吗?我永远感激不尽!
答案 0 :(得分:2)
你可能会一次写一个大程序,试图一次性学习太多。
首先写一个非常小的程序;让它运作;试图对它做一个非常小的改进,并使其工作;等等。如果在任何时候你遇到困难,来这里,显示你的代码最多只有一个的东西被打破了,并询问一个关于一个问题的问题,特别是你“目前仍然坚持下去。这是获得帮助的最有效方式。
一些随机编码提示:
这不是有效的代码:
setxy pxcor = halfedge pycor = halfedge
setxy
需要两个数字,但是你传递了两个布尔值:pxcor = halfedge
是真还是假,pycor = halfedge
也是真或假。我想你可能只是setxy halfedge halfedge
。
agents with [self != myself]
可以简单地替换为other agents
。