Python 相互环绕的彩虹色圆圈
我试图改变屏幕上显示的圆圈的颜色和颜色。到目前为止,我已经知道了如何在递归模式中使所有颜色都不同,但我需要帮助了解如何添加更多颜色。附件是我所拥有的和我需要实现的 我的代码Python 相互环绕的彩虹色圆圈,python,recursion,turtle-graphics,Python,Recursion,Turtle Graphics,我试图改变屏幕上显示的圆圈的颜色和颜色。到目前为止,我已经知道了如何在递归模式中使所有颜色都不同,但我需要帮助了解如何添加更多颜色。附件是我所拥有的和我需要实现的 我的代码 import turtle import colorsys def draw_circle(x,y,r,color): turtle.seth(0) turtle.up() turtle.goto(x,y-r) turtle.down() turtle.fillcolor(color
import turtle
import colorsys
def draw_circle(x,y,r,color):
turtle.seth(0)
turtle.up()
turtle.goto(x,y-r)
turtle.down()
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(r)
turtle.end_fill()
def draw_recursive_circles(x,y,r,color,n):
if n == 0:
return
draw_circle(x,y,r,color)
colors = ['red','orange','yellow','green','blue','purple']
i = 0
for angle in range(30,360,60):
turtle.up()
turtle.goto(x,y)
turtle.seth(angle)
turtle.fd(r*2)
draw_recursive_circles(turtle.xcor(),turtle.ycor(),r/3,colors[i],n-1)
i += 1
turtle.tracer(0)
turtle.hideturtle()
turtle.speed(0)
draw_recursive_circles(0,0,100,'red',5)
turtle.update()
您导入colorsys但从不使用它——这是一条线索,您应该根据角度而不是固定的颜色列表生成颜色。导入的原因是基于turtle的RGB颜色的模型不适合我们的需要,因此我们需要一个更合适的模型,如HSV(我们只关心H/hue),并让它将这些值转换为RGB 卫星数量由您的
通话范围决定:
for angle in range(30,360,60):
此图形中的哪个更像:
for angle in range(0, 360, 30):
因为有12颗卫星,360/30
是12颗。最后,我们需要进行适当的计算,以便无论何时更改位置或标题,为了进行递归绘制,我们都需要在退出时恢复原始值。下面是我对该问题的简化示例解决方案:
from turtle import Screen, Turtle
from colorsys import hsv_to_rgb
def draw_circle(radius):
y = turtle.ycor() # save position & heading
heading = turtle.heading()
turtle.fillcolor(hsv_to_rgb(heading / 360, 1.0, 1.0))
turtle.sety(y - radius)
turtle.setheading(0)
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
turtle.sety(y) # restore position & heading
turtle.setheading(heading)
def draw_recursive_circles(radius, n):
if n == 0:
return
draw_circle(radius)
if n > 1:
heading = turtle.heading() # save heading
for angle in range(0, 360, 30):
turtle.setheading(angle)
turtle.forward(radius * 2)
draw_recursive_circles(radius / 5, n - 1)
turtle.backward(radius * 2)
turtle.setheading(heading) # restore heading
screen = Screen()
screen.tracer(False)
turtle = Turtle(visible=False)
turtle.penup()
draw_recursive_circles(150, 4)
screen.update()
screen.tracer(True)
screen.exitonclick()
我故意保持钢笔的高度,以简化我的示例,因此只显示圆圈的填充部分。把周围的轮廓放回去,作为练习留给你
中心圆的颜色不正确。修正这是一个简单的问题,在最初调用“代码”> DracyRealsiviyCulsCe()/<代码> 之前,我们应该接受这个答案:如果你觉得这个答案对你有帮助,你可以考虑。