在同一个补丁中消除两只乌龟
Eliminate two turtles in same patch
大家下午好:
我目前在一个 Netlogo 程序中工作,该程序具有三种不同类型的海龟(坦克、子弹、敌人)。正如你想象的那样,我希望当一颗子弹在同一敌人 patch
中时,子弹和敌人 die
。问题是我不知道该怎么做。我尝试使用以下代码,但它不起作用,因为它消除了两个敌人穿过他们的补丁的时间,而我只想消除子弹和敌人在同一补丁中的时间。我也试过这段代码,它也不起作用:
ask bullet [ if any? enemy-here [ die ] ]
ask enemy [ if any? bullet-here [ die ] ]
to shoot
if not mouse-down? or not mouse-inside? [ stop ]
create-bullets 1 [
setxy mouse-xcor mouse-ycor + 5
set shape "line half"
set color red
set heading 360
set size 4
]
end
to fire
ask bullets [
fd 0.006
if pycor = max-pycor [ die ]
**ask turtles with [count turtles-here >= 2] [ die]** ;this is the important part that why it is in strong
]
tick
end
to move-tank
setxy mouse-xcor mouse-ycor
set hidden? not mouse-inside?
end
根据您在评论中的回复进行更新
那么问题一定是别的原因,因为我给你的代码确实有效,你可以用我做的这个小玩具模型自己验证一下:
breed [bullets bullet]
breed [enemies enemy]
to setup
clear-all
set-default-shape bullets "circle"
create-enemies 30 [
move-to one-of patches with [not any? enemies-here]
]
end
to fire ; 'fire' is a button in the Interface.
create-bullets 1 [
move-to one-of patches with [not any? enemies-here]
set size 0.5
]
end
to go
; I suggest to NOT make 'go' a forever-button, so to clearly see
; the moment in which the bullet hits an enemy and they both disappear.
ask bullets [
forward 1
if (any? enemies-here) [
ask one-of enemies-here [die]
die
]
]
end
你可以看到当子弹击中敌人时,他们都会消失。因此,如果应用此方法在您的模型中不起作用,则问题必须是 (1) 模型中的其他地方,或者 (2) 您根据自己的情况调整方法的方式。
我倾向于认为这是第二种选择,因为代码明确地使用了两个 die
命令:一个用于目标敌人,一个用于子弹本身,我认为没有合理的方法可能会使模型的任何其他部分对此产生干扰。
初始回复
第一件事:检查您的品种名称是否正确。你说你有坦克、子弹和敌人,但在代码中我看到你使用 ask guns
。什么是枪?是坦克,是子弹,还是none?
无论如何,您尝试的第一件事(您发布的代码中的前两行)不起作用,因为您要求某个代理执行检查(例如 if any? enemy-here
),如果条件为真,您正在向 询问 到 die
的同一个代理人。另外,请注意有两个语法错误:末尾没有右括号,并且您正在使用品种的单数版本来检查 <breeds>-here
(例如,它应该是 enemies-here
(或您称之为的任何东西) ) 而不是 enemy-here
).
一般来说,你应该这样做:
ask bullets [
if (any? enemies-here) [
ask one-of enemies-here [die]
die
]
]
请注意,这使得一颗子弹只能杀死一个敌人,即使该补丁上有更多敌人也是如此。
大家下午好:
我目前在一个 Netlogo 程序中工作,该程序具有三种不同类型的海龟(坦克、子弹、敌人)。正如你想象的那样,我希望当一颗子弹在同一敌人 patch
中时,子弹和敌人 die
。问题是我不知道该怎么做。我尝试使用以下代码,但它不起作用,因为它消除了两个敌人穿过他们的补丁的时间,而我只想消除子弹和敌人在同一补丁中的时间。我也试过这段代码,它也不起作用:
ask bullet [ if any? enemy-here [ die ] ]
ask enemy [ if any? bullet-here [ die ] ]
to shoot
if not mouse-down? or not mouse-inside? [ stop ]
create-bullets 1 [
setxy mouse-xcor mouse-ycor + 5
set shape "line half"
set color red
set heading 360
set size 4
]
end
to fire
ask bullets [
fd 0.006
if pycor = max-pycor [ die ]
**ask turtles with [count turtles-here >= 2] [ die]** ;this is the important part that why it is in strong
]
tick
end
to move-tank
setxy mouse-xcor mouse-ycor
set hidden? not mouse-inside?
end
根据您在评论中的回复进行更新
那么问题一定是别的原因,因为我给你的代码确实有效,你可以用我做的这个小玩具模型自己验证一下:
breed [bullets bullet]
breed [enemies enemy]
to setup
clear-all
set-default-shape bullets "circle"
create-enemies 30 [
move-to one-of patches with [not any? enemies-here]
]
end
to fire ; 'fire' is a button in the Interface.
create-bullets 1 [
move-to one-of patches with [not any? enemies-here]
set size 0.5
]
end
to go
; I suggest to NOT make 'go' a forever-button, so to clearly see
; the moment in which the bullet hits an enemy and they both disappear.
ask bullets [
forward 1
if (any? enemies-here) [
ask one-of enemies-here [die]
die
]
]
end
你可以看到当子弹击中敌人时,他们都会消失。因此,如果应用此方法在您的模型中不起作用,则问题必须是 (1) 模型中的其他地方,或者 (2) 您根据自己的情况调整方法的方式。
我倾向于认为这是第二种选择,因为代码明确地使用了两个 die
命令:一个用于目标敌人,一个用于子弹本身,我认为没有合理的方法可能会使模型的任何其他部分对此产生干扰。
初始回复
第一件事:检查您的品种名称是否正确。你说你有坦克、子弹和敌人,但在代码中我看到你使用 ask guns
。什么是枪?是坦克,是子弹,还是none?
无论如何,您尝试的第一件事(您发布的代码中的前两行)不起作用,因为您要求某个代理执行检查(例如 if any? enemy-here
),如果条件为真,您正在向 询问 到 die
的同一个代理人。另外,请注意有两个语法错误:末尾没有右括号,并且您正在使用品种的单数版本来检查 <breeds>-here
(例如,它应该是 enemies-here
(或您称之为的任何东西) ) 而不是 enemy-here
).
一般来说,你应该这样做:
ask bullets [
if (any? enemies-here) [
ask one-of enemies-here [die]
die
]
]
请注意,这使得一颗子弹只能杀死一个敌人,即使该补丁上有更多敌人也是如此。