如何使用Python turtle画花


我正在练习我的Python技能。 我很好奇如何在不使用 turtle.circle(radius)的情况下使用turtles和函数绘制此花。

如何使用Python turtle画花

代码如下:

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()

https://www.linuxboy.net/topicnews.aspx?tid=17

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2020-01/162131.htm

相关内容