如何在以下代码中完成以下操作:
补丁改变颜色反映他们与行“min-pycor”的距离
例如,颜色从黄色到红色再变为黑色(表示死亡)。
但是这应该考虑到黄色斑块的产生>红色>黑色。
turtles-own
[
stem? ;; true for stem cells, false for transitory cells
age ;; age of cell. changes color with age
metastatic? ;; false for progeny of stem cell 0, true for progeny of stem cell 1
]
globals
[
cell-count
]
to setup
clear-all
set-default-shape turtles "square"
ask patches[
if pycor = min-pycor [
ifelse random 10 <= 2
[set pcolor white]
[sprout 1 [set shape "square" set color blue] ]
]
]
evaluate-params
reset-ticks
end
to go
ask patches with [pcolor = yellow]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
ask patches with [pcolor = white]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
tick
end
;;transitional cells move and hatch more. Turtle proc.
to move-transitional-cells
if (not stem?)
[
set color ( red + 0.25 * age )
fd 1
if (age < 6)
[
hatch 1
[ ;amplification
rt random-float 360
fd 1
]
]
]
end
to mitosis ;; turtle proc. - stem cells only
if stem?
[
hatch 1
[
fd 1
set color red
set stem? false
ifelse (who = 1)
[ set age 16 ]
[ set age 0 ]
]
]
end
to death ;; turtle proc.
if (not stem?) and (not metastatic?) and (age > 20)
[ die ]
if (not stem?) and metastatic? and (age > 4)
[ die ]
end
to evaluate-params
set cell-count count turtles ;cell count
if (cell-count <= 0)
[ stop ]
end
to kill-original-stem-cell
ask turtle 0
[ die ]
end
to kill-moving-stem-cell
ask turtle 1
[ die ]
end
to kill-transitory-cells
ask turtles with [ age < 10 and not stem? ]
[ die ]
end
答案 0 :(得分:2)
您似乎有两个相互矛盾的要求,基于代码中的邻近度的颜色变化,以及基于您询问的PYCOR的颜色变化。
暂时忽略代码,我们可以通过多种方式根据PYCOR设置颜色。例如,我们可以创建颜色带,我们可以创建颜色的抖动混合,或者我们可以在颜色之间创建“平滑”过渡。
第一个很容易。我们可以使用IFELSE结构。此示例创建偶数波段,但您可以更改“除法”变量以创建任意高度的波段。
let color1 red
let color2 yellow
let color3 black
let divide1 (min-pycor + world-height * 0.33)
let divide2 (min-pycor + world-height * 0.66)
ask patches
[ ifelse pycor < divide1 [ set pcolor color1 ][
ifelse pycor < divide2 [ set pcolor color2 ][
set pcolor color3
]]
]
我们也可以用肮脏的方式做到这一点。此示例创建偶数波段。
let colors [ red yellow black ]
let bands length colors
let band-height floor ( world-height / bands + .5 )
;; pycor - min-pycor shifts the range from 0 to world-height
ask patches [ set pcolor item ( floor ( ( pycor - min-pycor ) / band-height ) ) colors ]
我们可以通过向pycor引入随机元素来创建抖动波段。我们还必须添加一些测试来保持随机数在范围内。
let colors [ red yellow black ]
let bands length colors
let band-height floor (world-height / bands + .5)
let dither band-height * .5 ;; adjust this to change dithery-ness
ask patches
[ let py pycor - min-pycor + dither - random-float ( dither * 2 )
;; you might want to really study the above line to fully grok what's happening there
if py < 0 [ set py 0 ]
if py > world-height [ set py world-height ]
set pcolor item ( floor ( py / band-height ) ) colors
]
毕业(渐变)乐队更难,因为NetLogo色彩空间不会逐渐变换色调,只有色调和阴影,所以真的没办法从红色变成黄色。所以我们必须使用RGB(三值列表)颜色,而不是NetLogo(单值)颜色。而且这超出了我现在愿意输入的内容,所以你去了 - 离开时作为一个exersize。