大家好,小编来为大家解答以下问题,用python画彩色六边形,如何用python画一朵花,现在让我们一起来看看吧!
1 importturtle2 importmath3
4
5 defp_line(t, n, length, angle):6 """Draws n line segments."""
7 for i inrange(n):8 t.fd(length)9 t.lt(angle)10
11
12 defpolygon(t, n, length):13 """Draws a polygon with n sides."""
14 angle = 360 /n15 p_line(t, n, length, angle)16
17
18 defarc(t, r, angle):19 """Draws an arc with the given radius and angle."""
20 arc_length = 2 * math.pi * r * abs(angle) / 360
21 n = int(arc_length / 4) + 1
22 step_length = arc_length /n23 step_angle = float(angle) /n24
25 #Before starting reduces, making a slight left turn.
26 t.lt(step_angle / 2)27 p_line(t, n, step_length, step_angle)28 t.rt(step_angle / 2)29
30
31 defpetal(t, r, angle):32 """Draws a 花瓣 using two arcs."""
33 for i in range(2):34 arc(t, r, angle)35 t.lt(180 -angle)36
37
38 defflower(t, n, r, angle, p):39 """Draws a flower with n petals."""
40 for i inrange(n):41 petal(t, r, angle)42 t.lt(p /n)43
44
45 defleaf(t, r, angle, p):46 """Draws a 叶子 and fill it."""
47 t.begin_fill() #Begin the fill process.
48 t.down()49 flower(t, 1, r, angle, p)50 t.end_fill()51
52
53 defmain():54 window = turtle.Screen() #creat a screen
55 window.bgcolor("white")56 window.title("draw a flower")57 lucy =turtle.Turtle()58 lucy.shape("turtle")59 lucy.color("red")60 lucy.width(3)61 #lucy.speed(10)
62
63 #Drawing flower
64 flower(lucy, 7, 60, 100, 360)65
66 #Drawing pedicel
67 lucy.color("brown")68 lucy.rt(90)69 lucy.fd(200)70
71 #Drawing leaf 1
72 lucy.width(1)73 lucy.rt(270)74 lucy.color("green")75 leaf(lucy, 40, 80, 180)76 lucy.rt(140)77 lucy.color("black")78 lucy.fd(30)79 lucy.lt(180)80 lucy.fd(30)81
82 #Drawing leaf 2
83 lucy.rt(120)84 lucy.color("green")85 leaf(lucy, 40, 80, 180)86 lucy.color("black")87 lucy.rt(140)88 lucy.fd(30)89 lucy.ht() #hideturtle
90 window.exitonclick()91
92
93 main()