本文实例为大家分享了用python画一朵花的具体代码,供大家参考,具体内容如下
第一种,画法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
from turtle import *
import time
setup( 600 , 800 , 0 , 0 )
speed( 0 )
penup()
seth( 90 )
fd( 340 )
seth( 0 )
pendown()
speed( 5 )
begin_fill()
fillcolor( 'red' )
circle( 50 , 30 )
for i in range ( 10 ):
fd( 1 )
left( 10 )
circle( 40 , 40 )
for i in range ( 6 ):
fd( 1 )
left( 3 )
circle( 80 , 40 )
for i in range ( 20 ):
fd( 0.5 )
left( 5 )
circle( 80 , 45 )
for i in range ( 10 ):
fd( 2 )
left( 1 )
circle( 80 , 25 )
for i in range ( 20 ):
fd( 1 )
left( 4 )
circle( 50 , 50 )
time.sleep( 0.1 )
circle( 120 , 55 )
speed( 0 )
seth( - 90 )
fd( 70 )
right( 150 )
fd( 20 )
left( 140 )
circle( 140 , 90 )
left( 30 )
circle( 160 , 100 )
left( 130 )
fd( 25 )
penup()
right( 150 )
circle( 40 , 80 )
pendown()
left( 115 )
fd( 60 )
penup()
left( 180 )
fd( 60 )
pendown()
end_fill()
right( 120 )
circle( - 50 , 50 )
circle( - 20 , 90 )
speed( 1 )
fd( 75 )
speed( 0 )
circle( 90 , 110 )
penup()
left( 162 )
fd( 185 )
left( 170 )
pendown()
circle( 200 , 10 )
circle( 100 , 40 )
circle( - 52 , 115 )
left( 20 )
circle( 100 , 20 )
circle( 300 , 20 )
speed( 1 )
fd( 250 )
penup()
speed( 0 )
left( 180 )
fd( 250 )
circle( - 300 , 7 )
right( 80 )
circle( 200 , 5 )
pendown()
left( 60 )
begin_fill()
fillcolor( 'green' )
circle( - 80 , 100 )
right( 90 )
fd( 10 )
left( 20 )
circle( - 63 , 127 )
end_fill()
penup()
left( 50 )
fd( 20 )
left( 180 )
pendown()
circle( 200 , 25 )
penup()
right( 150 )
fd( 180 )
right( 40 )
pendown()
begin_fill()
fillcolor( 'green' )
circle( - 100 , 80 )
right( 150 )
fd( 10 )
left( 60 )
circle( - 80 , 98 )
end_fill()
penup()
left( 60 )
fd( 13 )
left( 180 )
pendown()
speed( 1 )
circle( - 200 , 23 )
exitonclick()
|
第二种,画法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
import turtle
import math
def p_line(t, n, length, angle):
"""draws n line segments."""
for i in range (n):
t.fd(length)
t.lt(angle)
def polygon(t, n, length):
"""draws a polygon with n sides."""
angle = 360 / n
p_line(t, n, length, angle)
def arc(t, r, angle):
"""draws an arc with the given radius and angle."""
arc_length = 2 * math.pi * r * abs (angle) / 360
n = int (arc_length / 4 ) + 1
step_length = arc_length / n
step_angle = float (angle) / n
# before starting reduces, making a slight left turn.
t.lt(step_angle / 2 )
p_line(t, n, step_length, step_angle)
t.rt(step_angle / 2 )
def petal(t, r, angle):
"""draws a 花瓣 using two arcs."""
for i in range ( 2 ):
arc(t, r, angle)
t.lt( 180 - angle)
def flower(t, n, r, angle, p):
"""draws a flower with n petals."""
for i in range (n):
petal(t, r, angle)
t.lt(p / n)
def leaf(t, r, angle, p):
"""draws a 叶子 and fill it."""
t.begin_fill() # begin the fill process.
t.down()
flower(t, 1 , r, angle, p)
t.end_fill()
def main():
window = turtle.screen() # creat a screen
window.bgcolor( "white" )
window.title( "draw a flower" )
lucy = turtle.turtle()
lucy.shape( "turtle" )
lucy.color( "red" )
lucy.width( 3 )
# lucy.speed(10)
# drawing flower
flower(lucy, 7 , 60 , 100 , 360 )
# drawing pedicel
lucy.color( "brown" )
lucy.rt( 90 )
lucy.fd( 200 )
# drawing leaf 1
lucy.width( 1 )
lucy.rt( 270 )
lucy.color( "green" )
leaf(lucy, 40 , 80 , 180 )
lucy.rt( 140 )
lucy.color( "black" )
lucy.fd( 30 )
lucy.lt( 180 )
lucy.fd( 30 )
# drawing leaf 2
lucy.rt( 120 )
lucy.color( "green" )
leaf(lucy, 40 , 80 , 180 )
lucy.color( "black" )
lucy.rt( 140 )
lucy.fd( 30 )
lucy.ht() # hideturtle
window.exitonclick()
main()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/lyox/p/8668167.html