有人做过吗?在节省时间使用多种颜色.
我有一个颜色列表:
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的彩色圆圈
解决方法:
你是说这个圈子吗?
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)