如何检查任意两只海龟之间的碰撞?
How do I check for collisions between any two turtles?
Netlogo 老实说对我来说很奇怪,但我一直在努力让 Space Invaders 成为一个项目。我需要能够检查同一品种的多只海龟之间的距离,以便 运行 进行碰撞检查。我该怎么做?
to shoot
ask turtle 0 [
hatch 1
[
set shape "dot"
set size 2
set color red
set ctrl "projectile"
set xcor [xcor] of turtle 0
set ycor [ycor] of turtle 0 + 2
repeat 40
[
ifelse ycor < 15 and distancexy [xcor] of turtle 1 [ycor] of turtle 1 > 0.5
[
set ycor ycor + 1
]
[
ifelse distancexy [xcor] of turtle 1 [ycor] of turtle 1 < 0.5
[
ask turtle 1 [die]die][die]
]
wait .01
]
]
]
end
所以你想要像 'hero' 乌龟这样的东西来射击一定距离内的所有乌龟。我稍微修改了您的代码,但基本思想是合理的。您实际上没有确定目标的步骤。我还通过使用 face
简化了移动并使用了 while
因为不能保证需要 40 步才能到达目标。
to shoot
let shooter turtle 0 ; or however the shooter is selected
ask shooter
[ let targets other turtles with [distance myself < 0.5 ; finds the targets
if any? targets
[ let this-target one-of targets
face this-target ; so shooter is facing the target so trail better
; do the shooting
[ hatch 1
[ set shape "dot"
set size 2
set color red
set ctrl "projectile"
let step-distance 0.02
while distance this-target > step-distance
[ face this-target ; you don't need to worry about coordinates
forward step-distance
wait .01
]
die ; once close enough, projectile dies
]
ask this-target [ die ] ; and kills the target
]
]
]
end
此代码完全未经测试,需要另一个循环来获取所有目标。通常你不会在 NetLogo 中使用太多循环,但是 ask
会将其更改为目标而不是射手的视角。
Netlogo 老实说对我来说很奇怪,但我一直在努力让 Space Invaders 成为一个项目。我需要能够检查同一品种的多只海龟之间的距离,以便 运行 进行碰撞检查。我该怎么做?
to shoot
ask turtle 0 [
hatch 1
[
set shape "dot"
set size 2
set color red
set ctrl "projectile"
set xcor [xcor] of turtle 0
set ycor [ycor] of turtle 0 + 2
repeat 40
[
ifelse ycor < 15 and distancexy [xcor] of turtle 1 [ycor] of turtle 1 > 0.5
[
set ycor ycor + 1
]
[
ifelse distancexy [xcor] of turtle 1 [ycor] of turtle 1 < 0.5
[
ask turtle 1 [die]die][die]
]
wait .01
]
]
]
end
所以你想要像 'hero' 乌龟这样的东西来射击一定距离内的所有乌龟。我稍微修改了您的代码,但基本思想是合理的。您实际上没有确定目标的步骤。我还通过使用 face
简化了移动并使用了 while
因为不能保证需要 40 步才能到达目标。
to shoot
let shooter turtle 0 ; or however the shooter is selected
ask shooter
[ let targets other turtles with [distance myself < 0.5 ; finds the targets
if any? targets
[ let this-target one-of targets
face this-target ; so shooter is facing the target so trail better
; do the shooting
[ hatch 1
[ set shape "dot"
set size 2
set color red
set ctrl "projectile"
let step-distance 0.02
while distance this-target > step-distance
[ face this-target ; you don't need to worry about coordinates
forward step-distance
wait .01
]
die ; once close enough, projectile dies
]
ask this-target [ die ] ; and kills the target
]
]
]
end
此代码完全未经测试,需要另一个循环来获取所有目标。通常你不会在 NetLogo 中使用太多循环,但是 ask
会将其更改为目标而不是射手的视角。