Netlogo:如何在世界中央的特定补丁中停止乌龟的特定滴答声?
Netlogo: How to stop a turtle for a certain ticks with a specific patch in the middle of world?
我是初学者。我已经查过编程指导词典了。
我正在考虑双车道道路(例如,道路 1、道路 2)模型。
而且我还在考虑一个模型,其中由指定的补丁((10 0)和(20 2))指定的海龟停止 10 个刻度。
但是,我不知道如何为每条道路编写和指定 xcor 和 ycor 的具体参数(例如道路 1 上的 xcor 和 ycor,道路 2 上的 xcor 和 ycor)。
而且我也不知道如何在设置速度语法中编写和控制参数 "speed" 。
以下是示例小模型。为避免复杂化,此示例模型只有一条路。此示例模型失败,海龟不会停在 patch(10 0)。
也许我需要你的建议。谢谢。
globals [ count-tick ]
turtles-own [ speed flag-A ]
to setup
clear-all
resize-world 0 50 min-pycor max-pycor
ask patches [ setup-road ]
reset-ticks
end
to setup-road
if ( pycor < 1 ) and ( pycor > -1 ) [ set pcolor white ]
end
to create-car
crt 1 [
set color blue
setxy min-pxcor 0
set heading 90
set speed 1
]
end
这是模型主体。
to go
if (count turtles-on patch 0 0 = 0) [
create-car
ask (turtles-on patch 0 0) [
set flag-A FALSE
]
]
ask (turtles-on patch 10 0) [
set flag-A TRUE
set count-tick 10
]
if count-tick > 0 [
set count-tick count-tick - 1
ask (turtles-on patch 10 0) with [flag-A = TRUE]
[
set color red
set speed 0
]
]
if count-tick = 0 [
ask (turtles-on patch 10 0) with [flag-A = TRUE]
[
set speed 1
set flag-A FALSE
]
]
if (count turtles-on patch max-pxcor 0 > 0) [
ask min-one-of turtles [who][
die
]
]
set-speed
tick
end
这是控制速度的并行更新。
to set-speed
ask turtles with [ xcor < 10 ] [
let turtle-ahead one-of turtles-on patch-ahead 1
ifelse turtle-ahead = nobody
[ set speed 1
fd speed
]
[ set speed 0
]
]
ask turtles with [ 10 < max-pxcor ] [
let turtle-ahead one-of turtles-on patch-ahead 1
ifelse turtle-ahead = nobody
[ set speed 1
fd speed
]
[ set speed 0
]
]
end
好的,作为一般规则,一次向您的模型添加一个元素。测试该元素,然后在一切正常后才添加下一个元素。在您的情况下,您正在尝试做几件事而没有任何一个在工作-移动汽车,将它们暂停 10 个滴答声,使其中一个死在路的尽头,做一些未指定速度的事情,以及我可能没有做的其他事情'立即通知。
你在这里也有几个概念上的问题 - 最大的是 count-tick 是一个 turtle 变量,但你将它视为一个全局变量,因为 if count-tick...
应该在 ask turtles
块内.以这种方式考虑,如果您创建了 10 辆汽车,则变量 count-tick 有 10 个副本,因此您要使用 if
语句检查哪一个。
你也没有告诉你的海龟移动,但这可能在你没有显示的代码中。尽可能多地保留您的代码,这就是我认为您正在尝试做的事情。这将在左侧创建一辆汽车,让它向右移动,在正确的位置暂停 10 个刻度并变红,然后再次移动,到达终点时将其杀死。
globals [ count-tick ]
turtles-own [ speed flag-A ]
to setup
clear-all
resize-world 0 50 min-pycor max-pycor
ask patches [ setup-road ]
reset-ticks
end
to setup-road
if ( pycor < 1 ) and ( pycor > -1 ) [ set pcolor white ]
end
to create-car
crt 1 [
set color blue
setxy min-pxcor 0
set heading 90
set speed 1
set flag-A FALSE
]
end
to go
if (count turtles-on patch 0 0 = 0) [
create-car
]
ask (turtles-on patch 10 0) [
set flag-A TRUE
set count-tick 10
]
ask (turtles-on patch 10 0) with [flag-A = TRUE] [
set color red
set speed 0
set count-tick count-tick - 1
if count-tick = 0 [
set speed 1
set flag-A FALSE
]
]
if (count turtles-on patch max-pxcor 0 > 0) [
ask min-one-of turtles-on patch max-pxcor 0 [who][
die
]
]
ask turtles [ forward speed ]
tick
end
我是初学者。我已经查过编程指导词典了。 我正在考虑双车道道路(例如,道路 1、道路 2)模型。 而且我还在考虑一个模型,其中由指定的补丁((10 0)和(20 2))指定的海龟停止 10 个刻度。 但是,我不知道如何为每条道路编写和指定 xcor 和 ycor 的具体参数(例如道路 1 上的 xcor 和 ycor,道路 2 上的 xcor 和 ycor)。 而且我也不知道如何在设置速度语法中编写和控制参数 "speed" 。 以下是示例小模型。为避免复杂化,此示例模型只有一条路。此示例模型失败,海龟不会停在 patch(10 0)。 也许我需要你的建议。谢谢。
globals [ count-tick ]
turtles-own [ speed flag-A ]
to setup
clear-all
resize-world 0 50 min-pycor max-pycor
ask patches [ setup-road ]
reset-ticks
end
to setup-road
if ( pycor < 1 ) and ( pycor > -1 ) [ set pcolor white ]
end
to create-car
crt 1 [
set color blue
setxy min-pxcor 0
set heading 90
set speed 1
]
end
这是模型主体。
to go
if (count turtles-on patch 0 0 = 0) [
create-car
ask (turtles-on patch 0 0) [
set flag-A FALSE
]
]
ask (turtles-on patch 10 0) [
set flag-A TRUE
set count-tick 10
]
if count-tick > 0 [
set count-tick count-tick - 1
ask (turtles-on patch 10 0) with [flag-A = TRUE]
[
set color red
set speed 0
]
]
if count-tick = 0 [
ask (turtles-on patch 10 0) with [flag-A = TRUE]
[
set speed 1
set flag-A FALSE
]
]
if (count turtles-on patch max-pxcor 0 > 0) [
ask min-one-of turtles [who][
die
]
]
set-speed
tick
end
这是控制速度的并行更新。
to set-speed
ask turtles with [ xcor < 10 ] [
let turtle-ahead one-of turtles-on patch-ahead 1
ifelse turtle-ahead = nobody
[ set speed 1
fd speed
]
[ set speed 0
]
]
ask turtles with [ 10 < max-pxcor ] [
let turtle-ahead one-of turtles-on patch-ahead 1
ifelse turtle-ahead = nobody
[ set speed 1
fd speed
]
[ set speed 0
]
]
end
好的,作为一般规则,一次向您的模型添加一个元素。测试该元素,然后在一切正常后才添加下一个元素。在您的情况下,您正在尝试做几件事而没有任何一个在工作-移动汽车,将它们暂停 10 个滴答声,使其中一个死在路的尽头,做一些未指定速度的事情,以及我可能没有做的其他事情'立即通知。
你在这里也有几个概念上的问题 - 最大的是 count-tick 是一个 turtle 变量,但你将它视为一个全局变量,因为 if count-tick...
应该在 ask turtles
块内.以这种方式考虑,如果您创建了 10 辆汽车,则变量 count-tick 有 10 个副本,因此您要使用 if
语句检查哪一个。
你也没有告诉你的海龟移动,但这可能在你没有显示的代码中。尽可能多地保留您的代码,这就是我认为您正在尝试做的事情。这将在左侧创建一辆汽车,让它向右移动,在正确的位置暂停 10 个刻度并变红,然后再次移动,到达终点时将其杀死。
globals [ count-tick ]
turtles-own [ speed flag-A ]
to setup
clear-all
resize-world 0 50 min-pycor max-pycor
ask patches [ setup-road ]
reset-ticks
end
to setup-road
if ( pycor < 1 ) and ( pycor > -1 ) [ set pcolor white ]
end
to create-car
crt 1 [
set color blue
setxy min-pxcor 0
set heading 90
set speed 1
set flag-A FALSE
]
end
to go
if (count turtles-on patch 0 0 = 0) [
create-car
]
ask (turtles-on patch 10 0) [
set flag-A TRUE
set count-tick 10
]
ask (turtles-on patch 10 0) with [flag-A = TRUE] [
set color red
set speed 0
set count-tick count-tick - 1
if count-tick = 0 [
set speed 1
set flag-A FALSE
]
]
if (count turtles-on patch max-pxcor 0 > 0) [
ask min-one-of turtles-on patch max-pxcor 0 [who][
die
]
]
ask turtles [ forward speed ]
tick
end