netlogo - 在同一个补丁中消灭两只海龟
问题描述
大家下午好:我目前在一个 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
. 什么是枪?是坦克,是子弹,还是没有?
无论如何,您尝试的第一件事(您发布的代码中的前两行)不起作用,因为您要求某个代理执行检查(例如if any? enemy-here
),如果该条件为真,则您正在询问同一个代理到die
. 另外,请注意有两个语法错误:末尾没有右括号,并且您正在使用品种的单数版本来检查<breeds>-here
(例如,它应该是enemies-here
(或您称它们的任何名称)而不是enemy-here
)。
一般来说,你应该这样做:
ask bullets [
if (any? enemies-here) [
ask one-of enemies-here [die]
die
]
]
请注意,这使得一颗子弹只能杀死一个敌人,即使该补丁上有更多敌人。
推荐阅读
- javascript - 在窗口调整大小时重新计算元素大小
- typescript - 如何检查对象是否仅包含 Typescript 中的指定键?
- function - 如何用另一个功能停止(或类似的东西)一个功能?
- powershell - 需要查找当前在 IIS 中使用的日志文件的名称
- excel - 从excel中的不同工作簿中提取数据
- python-3.x - 在 tr 标签中抓取特定属性
- python - 熊猫用计数创建新列
- asp.net-core-mvc - 覆盖默认 Identity Server 4 客户端
- c++ - glTexSubImage3D() 上的 INVALID_OPERATION
- node.js - StorageSharedKeyCredential - 无法设置未定义的“accountName”属性