【问题标题】:Using a slider to get user input for turtles' color使用滑块获取海龟颜色的用户输入
【发布时间】:2014-03-26 10:18:59
【问题描述】:

我想根据我可以在界面中更改的变量设置海龟的颜色。我正在考虑使用一个值从 1 到 5 的滑块,该值将决定将使用多少种颜色来为海龟着色。

例如,如果值为 2,那么海龟将用两种颜色着色。如果我可以控制要选择的颜色,我将不胜感激,但随机颜色也可以。

我曾经有一个静态的双色设置:

set color one-of [ red blue green brown orange ]

就这么简单。但是对于动态设置,我是这样做的。有没有更有效的方法来做到这一点?

 if groups = 1 [ set color red ]
    if groups = 2 [ set color one-of [ red blue ] ]
    if groups = 3 [ set color one-of [ red blue green ] ]
    if groups = 4 [ set color one-of [ red blue green orange ] ]
    if groups = 5 [ set color one-of [ red blue green orange brown ] ]

【问题讨论】:

    标签: simulation netlogo


    【解决方案1】:

    您可以使用sublist 从所需颜色列表中提取您想要的颜色数量:

    let colors sublist [ red blue green orange brown ] 0 groups
    ask turtles [ set color one-of colors ]
    

    如果您不想总是以相同的顺序获得相同的颜色,您可以使用n-ofbase-colors

    let colors n-of groups base-colors
    

    【讨论】:

    • 这简直太完美了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2021-02-05
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多