Python 如何用蟒蛇龟画一朵花

Python 如何用蟒蛇龟画一朵花,python,pycharm,turtle-graphics,Python,Pycharm,Turtle Graphics,我正在练习Python技能。我很好奇如何使用海龟和函数来绘制这朵花,而不使用turtle.circle(radius) 此外,我制作了一个多边形螺旋,如下所示: import turtle import math def draw_polygons(): """"Make t draws a polygon of side sd and length l""" sd = 20 area = 50000 while sd >= 3: si

我正在练习Python技能。我很好奇如何使用海龟和函数来绘制这朵花,而不使用
turtle.circle(radius)

此外,我制作了一个多边形螺旋,如下所示:

import turtle
import math


def draw_polygons():
    """"Make t draws a polygon of side sd and length l"""
    sd = 20
    area = 50000
    while sd >= 3:
        side_length = math.sqrt(area / sd * 4 * math.atan(math.pi / sd))
        for i in range(sd):
            for a_color in ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]:
                rest.fillcolor(a_color)
            rest.begin_fill()
            rest.forward(side_length)
            rest.left(360/sd)
            print("side length =", side_length)
        rest.penup()
        rest.forward(side_length / 2)
        rest.pendown()
        rest.right(30)
        sd -= 1

rest = turtle.Turtle()
wn = turtle.Screen()


draw_polygons()

wn.exitonclick()
我想用不同的颜色填充每个多边形,我做错了什么步骤?或者我下一步应该做什么

示例多边形螺旋如下所示:

import turtle
import math


def draw_polygons():
    """"Make t draws a polygon of side sd and length l"""
    sd = 20
    area = 50000
    while sd >= 3:
        side_length = math.sqrt(area / sd * 4 * math.atan(math.pi / sd))
        for i in range(sd):
            for a_color in ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]:
                rest.fillcolor(a_color)
            rest.begin_fill()
            rest.forward(side_length)
            rest.left(360/sd)
            print("side length =", side_length)
        rest.penup()
        rest.forward(side_length / 2)
        rest.pendown()
        rest.right(30)
        sd -= 1

rest = turtle.Turtle()
wn = turtle.Screen()


draw_polygons()

wn.exitonclick()

乍一看,我会说你的缩进是错误的

 for a_color in ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]:
     rest.fillcolor(a_color)
  rest.begin_fill()
在这里,您可以循环使用所有颜色,然后填充形状。 颜色将始终为紫色,因为这是您在rest.fillcolor中设置的最后一种颜色


作为解决问题的一种方法,您可以创建一个颜色列表,并通过调用
rest.fillcolor(colors[(i%len(colors)))
获得所需的颜色。如果您询问两个不同的程序,让我们先解决第二个问题。除了颜色问题,您的循环逻辑与
begin\u fill()不正确
内部循环,而不是外部循环。因此,让我们重新编写该程序:

from turtle import Turtle, Screen
import math

COLORS = ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]

def draw_polygons(sides, area):
    """ Draws a polygon with 'sides' sides and area 'area' """

    for i, sd in enumerate(range(sides, 2, -1)):
        side_length = math.sqrt(area / sd * 4 * math.atan(math.pi / sd))
        # print("side length =", side_length)

        a_color = COLORS[i % len(COLORS)]
        rest.fillcolor(a_color)

        rest.pendown()
        rest.begin_fill()

        for _ in range(sd):
            rest.forward(side_length)
            rest.left(360 / sd)

        rest.end_fill()
        rest.penup()

        rest.forward(side_length / 2)
        rest.right(30)

wn = Screen()

rest = Turtle()
rest.speed('fastest')

draw_polygons(20, 40_000)

rest.hideturtle()

wn.exitonclick()

回到你原来的问题,一种方法是在做下一种颜色的矩形之前,先把每种颜色的矩形都放下来。这里是对你的代码的一次修改,只画出图像的花瓣部分,因为我假设这就是你要问的。我只是看了下面的一些距离和角度,你需要做一些数学计算让他们做对:

from turtle import Turtle, Screen
import math

COLORS = ["green", "orange", "red", "yellow"]

def draw_polygons(area):
    """ Draws a circle using rectangles with area 'area' """

    side_length = math.sqrt(area * math.atan(math.pi / 4))

    for a_color in COLORS:

        for _ in range(12):

            rest.fillcolor(a_color)

            rest.forward(3 * side_length / 10)
            rest.right(15)

            rest.left(112)  # I eyeballed this angle, need to do math
            rest.pendown()
            rest.begin_fill()

            for _ in range(2):
                rest.forward(side_length * 2)
                rest.left(90)
                rest.forward(side_length / 2)
                rest.left(90)
            rest.end_fill()
            rest.penup()
            rest.right(112)  # undo rotation

            rest.forward(3 * side_length / 10)
            rest.right(15)

        # I eyeballed these opposing parameters -- need to do the math
        rest.forward(2 * side_length / 13)
        rest.right(7.5)

wn = Screen()

rest = Turtle()
rest.speed('fastest')
rest.penup()
rest.sety(120)

draw_polygons(20_000)

rest.hideturtle()

wn.exitonclick()

然而,我怎样才能用海龟图形和功能画出与图片一样的花呢?哦,我的上帝,你刚刚帮了我这么多,伙计。谢谢你,非常感谢。我已经设法解决了螺旋多边形的颜色问题,并且已经解决了,这与你的解决方案不同,但我很感激你的尝试。但我仍然在努力解决这个问题花的身体部位和龟的内部形状,龟也有不同的颜色。希望尽快收到你的来信。