如何用Python的turtle模块绘制彩色环

2 投票
1 回答
1967 浏览
提问于 2025-04-18 05:11

我有一段代码,目的是在一个圆圈周围画出一个彩色的环,但它只打印出一种颜色,并且在移动到下一个颜色之前只改变了8次。

import turtle

def drawCircle(colorList, radius):
    for color in colorList:
        turtle.color(color)
        for i in range(len(colorList)):
            turtle.penup()
            turtle.setpos(0, -radius)
            xpos=turtle.xcor()
            ypos=turtle.ycor()
            head=turtle.heading()
            turtle.begin_fill()
            turtle.pendown()
            turtle.home()
            turtle.setpos(xpos,ypos)
            turtle.setheading(head)
            turtle.circle(radius)
            turtle.end_fill()
            turtle.penup()
    return

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

drawCircle(colorList,200)

我该怎么做才能让圆圈周围的每一段弧都是不同的颜色呢? 这里有一个例子

1 个回答

1

你需要类似这样的东西

def drawSegment(color,x, y, r, angleStart, angleEnd, step=1):
    #More efficient to work in radians
    radianStart = angleStart*pi / 180
    radianEnd   = angleEnd*pi / 180
    radianStep=step *pi/180

    #Draw the segment
    turtle.penup()
    turtle.setpos(x,y)
    turtle.color(color)
    turtle.begin_fill()
    turtle.pendown()
    for theta in arange(radianStart,radianEnd,radianStep):
        turtle.setpos(x + r * cos(theta), y + r * sin(theta))

    turtle.setpos(x + r * cos(radianEnd), y + r * sin(radianEnd))
    turtle.setpos(x, y);
    turtle.end_fill()

def drawCircle(colorList,radius):
    #do something to draw an equal segment for each color advancing it around 360 degree's

撰写回答