NetLogo - 品种和特征
NetLogo - Breed and Characteristics
我有一个僵尸和人类互相战斗的小模拟。下面的代码是创建我的僵尸的代码。
create-zombies 5 [
setxy random-xcor random-ycor
set color black
set size 2
set shape "person"
set zombies_speed 0.5
]
这段代码是用来把人变成僵尸的代码。 convert-h
变量是一个全局变量,我将它设置为 =
到 convert-h-2
这是一个滑块,可用于确定人类变成僵尸的概率。
to infect
set convert-h convert-h-2
if any? humans in-radius 1 [
ask humans in-radius 1 [
if random 10 < convert-h [
hatch-zombies 1 [
set heading random 360]
die]]]
end
但是当人变成丧尸的时候,并没有带走丧尸的所有特征(size 2
shape "person"
)。它只需要 speed
、breed
和 color
。有没有一种方法可以添加僵尸的这两个特征而无需在第二个代码片段中手动编写?我希望我描述的一切都有意义
您可以将“僵尸设置”逻辑统一到一个过程中,然后在创建它们时调用它 infect/hatch:
to setup-zombie
set color black
set size 2
set shape "person"
set zombies_speed 0.5
end
...
create-zombies 5 [
setup-zombie
setxy random-xcor random-ycor
]
...
to infect
set convert-h convert-h-2
if any? humans in-radius 1 [
ask humans in-radius 1 [
if random 10 < convert-h [
hatch-zombies 1 [
setup-zombie
set heading random 360]
die]]]
end
我有一个僵尸和人类互相战斗的小模拟。下面的代码是创建我的僵尸的代码。
create-zombies 5 [
setxy random-xcor random-ycor
set color black
set size 2
set shape "person"
set zombies_speed 0.5
]
这段代码是用来把人变成僵尸的代码。 convert-h
变量是一个全局变量,我将它设置为 =
到 convert-h-2
这是一个滑块,可用于确定人类变成僵尸的概率。
to infect
set convert-h convert-h-2
if any? humans in-radius 1 [
ask humans in-radius 1 [
if random 10 < convert-h [
hatch-zombies 1 [
set heading random 360]
die]]]
end
但是当人变成丧尸的时候,并没有带走丧尸的所有特征(size 2
shape "person"
)。它只需要 speed
、breed
和 color
。有没有一种方法可以添加僵尸的这两个特征而无需在第二个代码片段中手动编写?我希望我描述的一切都有意义
您可以将“僵尸设置”逻辑统一到一个过程中,然后在创建它们时调用它 infect/hatch:
to setup-zombie
set color black
set size 2
set shape "person"
set zombies_speed 0.5
end
...
create-zombies 5 [
setup-zombie
setxy random-xcor random-ycor
]
...
to infect
set convert-h convert-h-2
if any? humans in-radius 1 [
ask humans in-radius 1 [
if random 10 < convert-h [
hatch-zombies 1 [
setup-zombie
set heading random 360]
die]]]
end