Python:如何在turtle中同时使用多种颜色

3

有人曾经这样做过吗?同时使用多种颜色。

我有一列颜色:

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的彩色圆形。
1个回答

3
你是指这个圆圈吗?

enter image description here

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)

因为你需要一个个地画填充的“三角形”(弧),所以使用填充圆会更加繁琐。


编辑:

enter image description here

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)

我不知道该如何感谢你,但你应该得到一个大大的感谢。你真的帮我解决了很多麻烦。我一直在尝试各种方法,但都没有成功,直到你出现才解救了我。非常感谢! - DamnGood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接