Python:在turtle中同时使用多种颜色
有没有人做过这个?就是同时使用多种颜色。
我有一份颜色列表:
colors = ["#880000",\
"#884400",\
"#888800",\
"#008800",\
"#008888",\
"#000088",\
"#440088",\
"#880088"]
我的目标是把这个颜色列表传给turtle,这样它就可以在一次转动中画出一个五彩缤纷的圆。
我的函数如下:
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
你是指这个圆圈吗?
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)