如何像 "cro" 命令但在海龟上下文中以有组织的方式制作海龟脸?
How do I make turtle face in an organized manner like the "cro" command but in turtle context?
我正在尝试制作一个可以向 8 个方向发射 8 发子弹的炮塔。在我的命令中,它们都以航向 0 生成,我如何让它们面向正确的方向。每只海龟都应该面对 45 的倍数。就像在观察者上下文中使用 cro 命令一样。
to fire-tacks
ask ttacks with [alive?] [
set attackSpeed attackSpeed + .5
if any? turtles with [is-bloon?] in-radius 5 and attackSpeed >= 12
[set attackSpeed 0
hatch-btacks 8 [set alive? false set is-turret? false
set size 1 set damage 1 set color black set is-dart? true set bullet-
speed 4
]]]
end
您可以使用 range
and foreach
来执行此操作(查看链接以了解有关其工作原理的更多详细信息)。 range
可以生成您想要的标题序列,并且 foreach
可以迭代该序列以在每个标题下生成新的海龟。看看这个简化的例子:
breed [ turrets turret ]
breed [ btacks btack ]
to setup
ca
create-turrets 1 [
setxy random-xcor random-ycor
]
reset-ticks
end
to go
ask turrets [
foreach ( range 0 360 45 ) [
new_heading ->
hatch-btacks 1 [
set heading new_heading
fd 1
]
]
]
end
我正在尝试制作一个可以向 8 个方向发射 8 发子弹的炮塔。在我的命令中,它们都以航向 0 生成,我如何让它们面向正确的方向。每只海龟都应该面对 45 的倍数。就像在观察者上下文中使用 cro 命令一样。
to fire-tacks
ask ttacks with [alive?] [
set attackSpeed attackSpeed + .5
if any? turtles with [is-bloon?] in-radius 5 and attackSpeed >= 12
[set attackSpeed 0
hatch-btacks 8 [set alive? false set is-turret? false
set size 1 set damage 1 set color black set is-dart? true set bullet-
speed 4
]]]
end
您可以使用 range
and foreach
来执行此操作(查看链接以了解有关其工作原理的更多详细信息)。 range
可以生成您想要的标题序列,并且 foreach
可以迭代该序列以在每个标题下生成新的海龟。看看这个简化的例子:
breed [ turrets turret ]
breed [ btacks btack ]
to setup
ca
create-turrets 1 [
setxy random-xcor random-ycor
]
reset-ticks
end
to go
ask turrets [
foreach ( range 0 360 45 ) [
new_heading ->
hatch-btacks 1 [
set heading new_heading
fd 1
]
]
]
end