Skip to main content
 首页 » 编程设计

python之如何在 python 中使用 turtle 模块绘制颜色环

2025年05月04日12dudu

我有以下代码,它应该在一个圆圈周围绘制一圈颜色,​​但如果打印并在移动到下一个之前更改 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) 

我怎样才能使围绕圆的每个圆弧都是不同的颜色。 here is an example

请您参考如下方法:

你需要这样的东西

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