【问题标题】:Python: Use Multiple Colors at once in turtlePython:在海龟中一次使用多种颜色
【发布时间】:2014-06-20 12:05:46
【问题描述】:

有人做过吗?在保存时使用多种颜色。

我有一个颜色列表:

colors = ["#880000",\
                "#884400",\
                "#888800",\
                "#008800",\
                "#008888",\
                "#000088",\
                "#440088",\
                "#880088"]

我的目标是获取颜色列表并将其传递给海龟,以便它可以在一条线中画出一个彩色圆圈。

我的功能如下:

def drawImage(colorList, radius):
    for color in colorList:
        turtle.color(color)
    turtle.penup()
    turtle.setpos(0, -radius)
    xpos=turtle.xcor()
    ypos=turtle.ycor()
    turtle.begin_fill()
    turtle.pendown()
    turtle.home()
    turtle.setpos(xpos,ypos)
    turtle.circle(radius)
    turtle.end_fill()
    turtle.color('black')
    turtle.width(2)
    turtle.circle(radius)    
    return

上述函数的问题在于它只使用一种颜色而不是列表中不同颜色的小弧线进行绘制。谁能帮我解决这个问题或指出我做错了什么?

该函数被称为drawImage(colors,200),这将绘制一个半径为200的彩色圆圈

【问题讨论】:

    标签: python python-2.7 python-3.x colors


    【解决方案1】:

    你是说这个圈子吗?

    import turtle
    
    colors = [
        "#880000",
        "#884400",
        "#888800",
        "#008800",
        "#008888",
        "#000088",
        "#440088",
        "#880088"
    ]
    
    #--------------------
    
    #turtle.reset()
    angle = 360/len(colors)
    turtle.width(10)
    
    for color in colors:
        turtle.color(color)
        turtle.circle(100, angle)
    

    实心圆会有更多的工作,因为你必须一个一个地绘制实心的“三角形”(弧线)。


    编辑:

    import turtle
    
    colors = [
        "#880000",
        "#884400",
        "#888800",
        "#008800",
        "#008888",
        "#000088",
        "#440088",
        "#880088"
    ]
    
    #--------------------
    
    def filled_arc(radius, angle, color):
    
        turtle.color(color)
    
        turtle.begin_fill()
        turtle.forward(radius)
        turtle.left(90)
        turtle.circle(radius, angle)
        turtle.left(90)
        turtle.forward(radius)
        turtle.end_fill()
    
        turtle.left(180-angle)
    
    #--------------------
    
    angle = 360/len(colors)
    
    for color in colors:
        filled_arc(100, angle, color)
        turtle.left(angle)
    

    【讨论】:

    • 我不知道如何感谢你,但你应该非常感谢你,你真的让我省了很多麻烦。我一直在尝试一切,但在你来救援之前似乎没有任何效果。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多