前言

简介

💻 Python已经越来越来流行了,下面将通过这个文章详细介绍Python中的Turtle库(海龟库)

turtle绘图体系:1969年诞生,主要用于程序设计入门; turtle库是python的标准库之一;属于入门级的图形绘制函数库; 标准库:是随解释器直接安装到操作系统中的功能模块; 第三方库:需要经过安装才能使用的功能模块; 库:library、包package、模块module统称为模块; turtle库绘制原理:有一只海龟在窗体正中心,在画布上游走,走过的轨迹形成了绘制的图形,海龟由程序控制,可以自由改变颜色、方向宽度等;

Turtle 如何使用呢

💦 当然,我们想使用这个库需要先学习这个每个方法的意思是什么?

没有理论知识为实践做铺垫,是很难完成下面的题目

画笔控制函数

Control
Control

控制画笔运动函数

PenSport
PenSport

全局控制函数

QuanJu
QuanJu

如何调用函数

1
2
import 库名 as 别名
import turtle as t

实战

前言

🔫 所有的语言都是废话,只有实战才能检验自己收获多少!!!

实战训练

1、turtle库完成正方形螺旋线的绘制

🌤️代码

1
2
3
4
5
6
7
import turtle
turtle.speed("fastest") #画笔速度(”快:fastest 慢的:slow”)
turtle.pensize(2) #画笔像素/宽度
for i in range(100): #绘画100次
turtle.fd(2*i) #前进;等价于forward(2*i)
turtle.left(90) #绘画矩形的一条边,逆时针移动
turtle.done() #结束绘制

结果

Combat

2、turtle库完成绘制斜螺旋线

🌤️代码

1
2
3
4
5
6
7
import turtle
turtle.speed("fastest") #画笔速度(”快:fastest 慢的:slow”)
turtle.pensize(2) #画笔像素/宽度
for i in range(100): #绘画100次
turtle.fd(2*i) #前进;等价于forward(2*i)
turtle.left(90.5) #绘画矩形的一条边,逆时针移动
turtle.done() #结束绘制

结果

Combat

3、turtle库完成绘制彩色斜螺旋线

🌤️代码

1
2
3
4
5
6
7
8
9
import turtle
turtle.speed("fastest")
turtle.pensize(2) #画笔像素
color = ["purple", "blue","red","yellow"] #颜色"green","pink","orange","black"
for i in range(100): #绘画100次
turtle.pencolor(color[i%4]) #更换画笔颜色
turtle.fd(2*i) #前进;等价于forward(2*i)
turtle.left(90.5) #绘画矩形的一条边
turtle.done() #结束绘制

结果

Combat

4、turtle库完成绘制直角三角形

🌤️代码

1
2
3
4
5
6
7
8
9
10
11
import turtle
turtle.up()
turtle.goto(100, 100)
turtle.down()
turtle.fillcolor('red')
turtle.begin_fill()
turtle.goto(200, 100)
turtle.goto(100, 200)
turtle.goto(100, 100)
turtle.end_fill()
turtle.mainloop()

结果

Combat

5、turtle库完成绘制梯形

🌤️代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import turtle
turtle.up()
turtle.goto(100, 100)
turtle.down()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.goto(200, 100)
turtle.right(60)
turtle.forward(100)
turtle.right(120)
turtle.forward(200)
turtle.right(120)
turtle.forward(100)
turtle.end_fill()
turtle.done()

结果

Combat

6、turtle库完成绘制两个五角星

🌤️代码

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
import turtle
turtle.title('绘制两个五角星')
turtle.pensize(5)
turtle.bgcolor("black")
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(2):
if i == 0:
for j in range(5):
turtle.forward(50)
turtle.left(72)
turtle.forward(50)
turtle.right(144)
elif i == 1:
turtle.up()
turtle.goto(180, 0)
turtle.down()
for j in range(5):
turtle.forward(50)
turtle.left(72)
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
turtle.done()

结果

Combat

7、turtle库完成绘制八一五角星

🌤️代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import turtle
turtle.shape('triangle')
turtle.shapesize(1)
turtle.pensize(10)
turtle.speed(5)
turtle.pencolor('yellow')
turtle.color("yellow", 'red')
turtle.begin_fill()
for i in range(5):
turtle.forward(150)
turtle.left(72)
turtle.forward(150)
turtle.right(144)
turtle.end_fill()
turtle.penup() # 画笔抬起
turtle.goto(195, -90) # 画笔移动
turtle.pendown() # 画笔落下
turtle.write("八", align='center', font=('楷体', 68, 'bold'))
turtle.penup() # 画笔抬起
turtle.goto(195, -140) # 画笔移动
turtle.pendown() # 画笔落下
turtle.write("一", align='center', font=('楷体', 68, 'bold'))
turtle.done()

结果

Combat

7、turtle库完成绘制冬奥会吉祥物冰墩墩

🌤️代码

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
    import turtle   #导入Python的turtle库
turtle.title('PythonBingDwenDwen')
turtle.speed(0) # 速度
# 左手
turtle.penup()
turtle.goto(177, 112)
turtle.pencolor("lightgray")
turtle.pensize(3)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(80)
turtle.circle(-45, 200)
turtle.circle(-300, 23)
turtle.end_fill()
# 左手内
turtle.penup()
turtle.goto(182, 95)
turtle.pencolor("black")
turtle.pensize(1)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.setheading(95)
turtle.pendown()
turtle.circle(-37, 160)
turtle.circle(-20, 50)
turtle.circle(-200, 30)
turtle.end_fill()
# 轮廓
# 头顶
turtle.penup()
turtle.goto(-73, 230)
turtle.pencolor("lightgray")
turtle.pensize(3)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(20)
turtle.circle(-250, 35)
# 左耳
turtle.setheading(50)
turtle.circle(-42, 180)
# 左侧
turtle.setheading(-50)
turtle.circle(-190, 30)
turtle.circle(-320, 45)
# 左腿
turtle.circle(120, 30)
turtle.circle(200, 12)
turtle.circle(-18, 85)
turtle.circle(-180, 23)
turtle.circle(-20, 110)
turtle.circle(15, 115)
turtle.circle(100, 12)
# 右腿
turtle.circle(15, 120)
turtle.circle(-15, 110)
turtle.circle(-150, 30)
turtle.circle(-15, 70)
turtle.circle(-150, 10)
turtle.circle(200, 35)
turtle.circle(-150, 20)
# 右手
turtle.setheading(-120)
turtle.circle(50, 30)
turtle.circle(-35, 200)
turtle.circle(-300, 23)
# 右侧
turtle.setheading(86)
turtle.circle(-300, 26)
# 右耳
turtle.setheading(122)
turtle.circle(-53, 160)
turtle.end_fill()
# 右耳内
turtle.penup()
turtle.goto(-130, 180)
turtle.pencolor("black")
turtle.pensize(1)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(120)
turtle.circle(-28, 160)
turtle.setheading(210)
turtle.circle(150, 20)
turtle.end_fill()
# 左耳内
turtle.penup()
turtle.goto(90, 230)
turtle.setheading(40)
turtle.begin_fill()
turtle.pendown()
turtle.circle(-30, 170)
turtle.setheading(125)
turtle.circle(150, 23)
turtle.end_fill()
# 右手内
turtle.penup()
turtle.goto(-180, -55)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.setheading(-120)
turtle.pendown()
turtle.circle(50, 30)
turtle.circle(-27, 200)
turtle.circle(-300, 20)
turtle.setheading(-90)
turtle.circle(300, 14)
turtle.end_fill()
# 左腿内
turtle.penup()
turtle.goto(108, -168)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(-115)
turtle.circle(110, 15)
turtle.circle(200, 10)
turtle.circle(-18, 80)
turtle.circle(-180, 13)
turtle.circle(-20, 90)
turtle.circle(15, 60)
turtle.setheading(42)
turtle.circle(-200, 29)
turtle.end_fill()
# 右腿内
turtle.penup()
turtle.goto(-38, -210)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(-155)
turtle.circle(15, 100)
turtle.circle(-10, 110)
turtle.circle(-100, 30)
turtle.circle(-15, 65)
turtle.circle(-100, 10)
turtle.circle(200, 15)
turtle.setheading(-14)
turtle.circle(-200, 27)
turtle.end_fill()
# 右眼
# 眼圈
turtle.penup()
turtle.goto(-64, 120)
turtle.begin_fill()
turtle.pendown()
turtle.setheading(40)
turtle.circle(-35, 152)
turtle.circle(-100, 50)
turtle.circle(-35, 130)
turtle.circle(-100, 50)
turtle.end_fill()
# 眼珠
turtle.penup()
turtle.goto(-47, 55)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(25, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(-45, 62)
turtle.pencolor("darkslategray")
turtle.fillcolor("darkslategray")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(19, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(-45, 68)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(10, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(-47, 86)
turtle.pencolor("white")
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(5, 360)
turtle.end_fill()
# 左眼
# 眼圈
turtle.penup()
turtle.goto(51, 82)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(120)
turtle.circle(-32, 152)
turtle.circle(-100, 55)
turtle.circle(-25, 120)
turtle.circle(-120, 45)
turtle.end_fill()
# 眼珠
turtle.penup()
turtle.goto(79, 60)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(24, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(79, 64)
turtle.pencolor("darkslategray")
turtle.fillcolor("darkslategray")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(19, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(79, 70)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(10, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(79, 88)
turtle.pencolor("white")
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(5, 360)
turtle.end_fill()
# 鼻子
turtle.penup()
turtle.goto(37, 80)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.circle(-8, 130)
turtle.circle(-22, 100)
turtle.circle(-8, 130)
turtle.end_fill()
# 嘴
turtle.penup()
turtle.goto(-15, 48)
turtle.setheading(-36)
turtle.begin_fill()
turtle.pendown()
turtle.circle(60, 70)
turtle.setheading(-132)
turtle.circle(-45, 100)
turtle.end_fill()
# 彩虹圈
turtle.penup()
turtle.goto(-135, 120)
turtle.pensize(5)
turtle.pencolor("cyan")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-165, 150)
turtle.circle(-130, 78)
turtle.circle(-250, 30)
turtle.circle(-138, 105)
turtle.penup()
turtle.goto(-131, 116)
turtle.pencolor("slateblue")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-160, 144)
turtle.circle(-120, 78)
turtle.circle(-242, 30)
turtle.circle(-135, 105)
turtle.penup()
turtle.goto(-127, 112)
turtle.pencolor("orangered")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-155, 136)
turtle.circle(-116, 86)
turtle.circle(-220, 30)
turtle.circle(-134, 103)
turtle.penup()
turtle.goto(-123, 108)
turtle.pencolor("gold")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-150, 136)
turtle.circle(-104, 86)
turtle.circle(-220, 30)
turtle.circle(-126, 102)
turtle.penup()
turtle.goto(-120, 104)
turtle.pencolor("greenyellow")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-145, 136)
turtle.circle(-90, 83)
turtle.circle(-220, 30)
turtle.circle(-120, 100)
turtle.penup()
# 爱心
turtle.penup()
turtle.goto(220, 115)
turtle.pencolor("brown")
turtle.pensize(1)
turtle.fillcolor("brown")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(36)
turtle.circle(-8, 180)
turtle.circle(-60, 24)
turtle.setheading(110)
turtle.circle(-60, 24)
turtle.circle(-8, 180)
turtle.end_fill()
# 五环
turtle.penup()
turtle.goto(-5, -170)
turtle.pendown()
turtle.pencolor("blue")
turtle.circle(6)
turtle.penup()
turtle.goto(10, -170)
turtle.pendown()
turtle.pencolor("black")
turtle.circle(6)
turtle.penup()
turtle.goto(25, -170)
turtle.pendown()
turtle.pencolor("brown")
turtle.circle(6)
turtle.penup()
turtle.goto(2, -175)
turtle.pendown()
turtle.pencolor("lightgoldenrod")
turtle.circle(6)
turtle.penup()
turtle.goto(16, -175)
turtle.pendown()
turtle.pencolor("green")
turtle.circle(6)
turtle.penup()
turtle.pencolor("black")
turtle.goto(-16, -160)
turtle.write("BEIJING 2022", font=('Arial', 10, 'bold italic'))
turtle.hideturtle()
turtle.done()

结果

Combat

总结

看了上述的文章如果对你来说有一定的帮助, 请别忘了给小猿一点鼓励!

如果上面的代码你都会了,那莫你的时间就没有白费哦!