https://www.cnblogs.com/bravestarrhu/p/8287261.html
https://blog.csdn.net/zengxiantao1994/article/details/76588580
Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
turtle绘图的基础知识:
1. 画布(canvas)
画布就是turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置。
设置画布大小
turtle.screensize(canvwidth=None, canvheight=None, bg=None),参数分别为画布的宽(单位像素), 高, 背景颜色。
如:turtle.screensize(800,600, "green")
turtle.screensize() #返回默认大小(400, 300)
turtle.setup(width=0.5, height=0.75, startx=None, starty=None),参数:width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例,(startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。
如:turtle.setup(width=0.6,height=0.6)
turtle.setup(width=800,height=800, startx=100, starty=100)
2. 画笔
2.1 画笔的状态
在画布上,默认有一个坐标原点为画布中心的坐标轴,坐标原点上有一只面朝x轴正方向小乌龟。这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中,就是使用位置方向描述小乌龟(画笔)的状态。
2.2 画笔的属性
画笔(画笔的属性,颜色、画线的宽度等)
1) turtle.pensize():设置画笔的宽度;
2) turtle.pencolor():没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组。
3) turtle.speed(speed):设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。
2.3 绘图命令
操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令。
(1) 画笔运动命令
命令
说明
turtle.forward(distance)
| turtle.fd(distance)
向当前画笔方向移动distance像素长度
turtle.backward(distance)
| turtle.back(distance) | turtle.bk(distance)
向当前画笔相反方向移动distance像素长度
turtle.right(degree)
| turtle.rt(angle)
顺时针移动degree°
turtle.left(degree)
| turtle.lt(angle)
逆时针移动degree°
turtle.pendown()
移动时绘制图形,缺省时也为绘制
turtle.goto(x,y)
将画笔移动到坐标为x,y的位置
turtle.penup()
提起笔移动,不绘制图形,用于另起一个地方绘制
turtle.circle()
画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
setx( )
将当前x轴移动到指定位置
sety( )
将当前y轴移动到指定位置
setheading(angle)
turtle.seth(to_angle)
设置当前朝向为angle角度
home()
设置当前画笔位置为原点,朝向东。
dot(r)
绘制一个指定直径和颜色的圆点
(2) 画笔控制命令
命令
说明
turtle.fillcolor(colorstring)
绘制图形的填充颜色
turtle.color(color1, color2)
同时设置pencolor=color1, fillcolor=color2
turtle.filling()
返回当前是否在填充状态
turtle.begin_fill()
准备开始填充图形
turtle.end_fill()
填充完成
turtle.hideturtle()
隐藏画笔的turtle形状
turtle.showturtle()
显示画笔的turtle形状
(3) 全局控制命令
命令
说明
turtle.clear()
清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset()
清空窗口,重置turtle状态为起始状态
turtle.undo()
撤销上一个turtle动作
turtle.isvisible()
返回当前turtle是否可见
stamp()
复制当前图形
turtle.write(s [,font=("font-name",font_size,"font_type")])
写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项
(4) 其他命令
命令
说明
turtle.mainloop()或turtle.done()
启动事件循环 -调用Tkinter的mainloop函数。
必须是乌龟图形程序中的最后一个语句。
turtle.mode(mode=None)
设置乌龟模式(“standard”,“logo”或“world”)并执行重置。如果没有给出模式,则返回当前模式。
模式
初始龟标题
正角度
standard
向右(东)
逆时针
logo
向上(北)
顺时针
turtle.delay(delay=None)
设置或返回以毫秒为单位的绘图延迟。
turtle.begin_poly()
开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
turtle.end_poly()
停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
turtle.get_poly()
返回最后记录的多边形。
3. 命令详解
3.1 turtle.circle(radius, extent=None, steps=None)
描述:以给定半径画圆
参数:
radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆;
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)。
举例:
circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆
实例:
1 #coding=utf-8
2 importturtle3 importtime4
5 #同时设置pencolor=color1, fillcolor=color2
6 turtle.color("red", "yellow")7
8 turtle.begin_fill()9 for _ in range(50):10 turtle.forward(200)11 turtle.left(170)12 turtle.end_fill()13
14 turtle.mainloop()
太阳花
1 #coding=utf-8
2 importturtle3 importtime4
5 turtle.pensize(5)6 turtle.pencolor("yellow")7 turtle.fillcolor("red")8
9 turtle.begin_fill()10 for _ in range(5):11 turtle.forward(200)12 turtle.right(144)13 turtle.end_fill()14 time.sleep(2)15
16 turtle.penup()17 turtle.goto(-150,-120)18 turtle.color("violet")19 turtle.write("Done", font=('Arial', 40, 'normal'))20
21 turtle.mainloop()
五角星
1 #coding=utf-8
2
3 importturtle4 from datetime import *
5
6 #抬起画笔,向前运动一段距离放下
7 defSkip(step):8 turtle.penup()9 turtle.forward(step)10 turtle.pendown()11
12 defmkHand(name, length):13 #注册Turtle形状,建立表针Turtle
14 turtle.reset()15 Skip(-length * 0.1)16 #开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
17 turtle.begin_poly()18 turtle.forward(length * 1.1)19 #停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
20 turtle.end_poly()21 #返回最后记录的多边形。
22 handForm =turtle.get_poly()23 turtle.register_shape(name, handForm)24
25 defInit():26 globalsecHand, minHand, hurHand, printer27 #重置Turtle指向北
28 turtle.mode("logo")29 #建立三个表针Turtle并初始化
30 mkHand("secHand", 135)31 mkHand("minHand", 125)32 mkHand("hurHand", 90)33 secHand =turtle.Turtle()34 secHand.shape("secHand")35 minHand =turtle.Turtle()36 minHand.shape("minHand")37 hurHand =turtle.Turtle()38 hurHand.shape("hurHand")39
40 for hand insecHand, minHand, hurHand:41 hand.shapesize(1, 1, 3)42 hand.speed(0)43
44 #建立输出文字Turtle
45 printer =turtle.Turtle()46 #隐藏画笔的turtle形状
47 printer.hideturtle()48 printer.penup()49
50 defSetupClock(radius):51 #建立表的外框
52 turtle.reset()53 turtle.pensize(7)54 for i in range(60):55 Skip(radius)56 if i % 5 ==0:57 turtle.forward(20)58 Skip(-radius - 20)59
60 Skip(radius + 20)61 if i ==0:62 turtle.write(int(12), align="center", font=("Courier", 14, "bold"))63 elif i == 30:64 Skip(25)65 turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))66 Skip(-25)67 elif (i == 25 or i == 35):68 Skip(20)69 turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))70 Skip(-20)71 else:72 turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))73 Skip(-radius - 20)74 else:75 turtle.dot(5)76 Skip(-radius)77 turtle.right(6)78
79 defWeek(t):80 week = ["星期一", "星期二", "星期三",81 "星期四", "星期五", "星期六", "星期日"]82 returnweek[t.weekday()]83
84 defDate(t):85 y =t.year86 m =t.month87 d =t.day88 return "%s %d%d" %(y, m, d)89
90 defTick():91 #绘制表针的动态显示
92 t =datetime.today()93 second = t.second + t.microsecond * 0.000001
94 minute = t.minute + second / 60.0
95 hour = t.hour + minute / 60.0
96 secHand.setheading(6 *second)97 minHand.setheading(6 *minute)98 hurHand.setheading(30 *hour)99
100 turtle.tracer(False)101 printer.forward(65)102 printer.write(Week(t), align="center",103 font=("Courier", 14, "bold"))104 printer.back(130)105 printer.write(Date(t), align="center",106 font=("Courier", 14, "bold"))107 printer.home()108 turtle.tracer(True)109
110 #100ms后继续调用tick
111 turtle.ontimer(Tick, 100)112
113 defmain():114 #打开/关闭龟动画,并为更新图纸设置延迟。
115 turtle.tracer(False)116 Init()117 SetupClock(160)118 turtle.tracer(True)119 Tick()120 turtle.mainloop()121
122 if __name__ == "__main__":123 main()
时钟
1 importturtle as t2
3 t.pensize(4) #设置画笔的宽度
4 t.hideturtle()#隐藏画笔
5 t.colormode(255)6 t.color((255,155,192),"pink")7 t.setup(840,500)#起始位置
8 t.speed(10) #设置画笔移动速度
9
10 #鼻子
11 t.pu()#画笔抬起 penup
12 t.goto(-100,100)13 t.pd()#画笔按下 pendown
14 t.seth(-30)#设置方向在逆时针30度
15 t.begin_fill()16 a = 0.4
17 for i in range(120):18 if 0<=i<30 or 60<=i<90:19 a = a+0.08
20 t.lt(3) #left 绘制方向向左旋转angle度
21 t.fd(a) #forward 画笔向绘制方向的当前方向移动的pixels距离
22 else:23 a = a-0.08
24 t.lt(3)25 t.fd(a)26 t.end_fill()27
28 t.pu()29 t.seth(90)30 t.fd(25)31 t.seth(0)32 t.fd(10)33 t.pd()34 t.pencolor(255,155,192)35 t.seth(10)36 t.begin_fill()37 t.circle(5)38 t.color(160,82,45)39 t.end_fill()40
41 t.pu()42 t.seth(0)43 t.fd(20)44 t.pd()45 t.pencolor(255,155,192)46 t.seth(10)47 t.begin_fill()48 t.circle(5)49 t.color(160,82,45)50 t.end_fill()51
52 #头
53 t.color((255,155,192),"pink")54 t.pu()55 t.seth(90)56 t.fd(41)57 t.seth(0)58 t.fd(0)59 t.pd()60 t.begin_fill()61 t.seth(180)62 t.circle(300,-30)63 t.circle(100,-60)64 t.circle(80,-100)65 t.circle(150,-20)66 t.circle(60,-95)67 t.seth(161)68 t.circle(-300,15)69 t.pu()70 t.goto(-100,100)71 t.pd()72 t.seth(-30)73 a = 0.4
74 for i in range(60):75 if 0<=i<30 or 60<=i<90:76 a = a+0.08
77 t.lt(3)78 t.fd(a)79 else:80 a = a-0.08
81 t.lt(3)82 t.fd(a)83 t.end_fill()84
85 #耳朵
86 t.color((255,155,192),"pink")87 t.pu()88 t.seth(90)89 t.fd(-7)90 t.seth(0)91 t.fd(70)92 t.pd()93 t.begin_fill()94 t.seth(100)95 t.circle(-50,50)96 t.circle(-10,120)97 t.circle(-50,54)98 t.end_fill()99
100 t.pu()101 t.seth(90)102 t.fd(-12)103 t.seth(0)104 t.fd(30)105 t.pd()106 t.begin_fill()107 t.seth(100)108 t.circle(-50,50)109 t.circle(-10,120)110 t.circle(-50,56)111 t.end_fill()112
113 #眼睛
114 t.color((255,155,192),"white")115 t.pu()116 t.seth(90)117 t.fd(-20)118 t.seth(0)119 t.fd(-95)120 t.pd()121 t.begin_fill()122 t.circle(15)123 t.end_fill()124
125 t.color("black")126 t.pu()127 t.seth(90)128 t.fd(12)129 t.seth(0)130 t.fd(-3)131 t.pd()132 t.begin_fill()133 t.circle(3)134 t.end_fill()135
136 t.color((255,155,192),"white")137 t.pu()138 t.seth(90)139 t.fd(-25)140 t.seth(0)141 t.fd(40)142 t.pd()143 t.begin_fill()144 t.circle(15)145 t.end_fill()146
147 t.color("black")148 t.pu()149 t.seth(90)150 t.fd(12)151 t.seth(0)152 t.fd(-3)153 t.pd()154 t.begin_fill()155 t.circle(3)156 t.end_fill()157
158 #腮
159 t.color((255,155,192))160 t.pu()161 t.seth(90)162 t.fd(-95)163 t.seth(0)164 t.fd(65)165 t.pd()166 t.begin_fill()167 t.circle(30)168 t.end_fill()169
170 #嘴
171 t.color(239,69,19)172 t.pu()173 t.seth(90)174 t.fd(15)175 t.seth(0)176 t.fd(-100)177 t.pd()178 t.seth(-80)179 t.circle(30,40)180 t.circle(40,80)181
182 #身体
183 t.color((255,99,71),"red")184 t.pu()185 t.seth(90)186 t.fd(-20)187 t.seth(0)188 t.fd(-78)189 t.pd()190 t.begin_fill()191 t.seth(-130)192 t.circle(100,10)193 t.circle(300,30)194 t.seth(0)195 t.fd(230)196 t.seth(90)197 t.circle(300,30)198 t.circle(100,3)199 t.color((255,155,192),(255,100,100))200 t.seth(-135)201 t.circle(-80,63)202 t.circle(-150,24)203 t.end_fill()204
205 #手
206 t.color((255,155,192))207 t.pu()208 t.seth(90)209 t.fd(-40)210 t.seth(0)211 t.fd(-27)212 t.pd()213 t.seth(-160)214 t.circle(300,15)215 t.pu()216 t.seth(90)217 t.fd(15)218 t.seth(0)219 t.fd(0)220 t.pd()221 t.seth(-10)222 t.circle(-20,90)223
224 t.pu()225 t.seth(90)226 t.fd(30)227 t.seth(0)228 t.fd(237)229 t.pd()230 t.seth(-20)231 t.circle(-300,15)232 t.pu()233 t.seth(90)234 t.fd(20)235 t.seth(0)236 t.fd(0)237 t.pd()238 t.seth(-170)239 t.circle(20,90)240
241 #脚
242 t.pensize(10)243 t.color((240,128,128))244 t.pu()245 t.seth(90)246 t.fd(-75)247 t.seth(0)248 t.fd(-180)249 t.pd()250 t.seth(-90)251 t.fd(40)252 t.seth(-180)253 t.color("black")254 t.pensize(15)255 t.fd(20)256
257 t.pensize(10)258 t.color((240,128,128))259 t.pu()260 t.seth(90)261 t.fd(40)262 t.seth(0)263 t.fd(90)264 t.pd()265 t.seth(-90)266 t.fd(40)267 t.seth(-180)268 t.color("black")269 t.pensize(15)270 t.fd(20)271
272
273
274 #尾巴
275 t.pensize(4)276 t.color((255,155,192))277 t.pu()278 t.seth(90)279 t.fd(70)280 t.seth(0)281 t.fd(95)282 t.pd()283 t.seth(0)284 t.circle(70,20)285 t.circle(10,330)286 t.circle(70,30)287 t.done()
小猪佩奇
1 #!/usr/bin/env python3
2
3 #-*- coding: utf-8 -*-
4
5
6
7 #@Env: python 3.6
8
9
10
11 from turtle import *
12
13
14
15 #无轨迹跳跃
16
17 defmy_goto(x, y):18
19 penup()20
21 goto(x, y)22
23 pendown()24
25
26 #眼睛
27
28 defeyes():29
30 tracer(False)31
32 a = 2.5
33
34 for i in range(120):35
36 if 0 <= i < 30 or 60 <= i < 90:37
38 a -= 0.05
39
40 lt(3)41
42 fd(a)43
44 else:45
46 a += 0.05
47
48 lt(3)49
50 fd(a)51
52 tracer(True)53
54
55 #胡须
56
57 defbeard():58
59 my_goto(-37, 135)60
61 seth(165)62
63 fd(60)64
65
66 my_goto(-37, 125)67
68 seth(180)69
70 fd(60)71
72
73 my_goto(-37, 115)74
75 seth(193)76
77 fd(60)78
79
80 my_goto(37, 135)81
82 seth(15)83
84 fd(60)85
86
87 my_goto(37, 125)88
89 seth(0)90
91 fd(60)92
93
94 my_goto(37, 115)95
96 seth(-13)97
98 fd(60)99
100
101 #嘴巴
102
103 defmouth():104
105 my_goto(5, 148)106
107 seth(270)108
109 fd(100)110
111 seth(0)112
113 circle(120, 50)114
115 seth(230)116
117 circle(-120, 100)118
119
120 #围巾
121
122 defscarf():123
124 fillcolor('#e70010')125
126 begin_fill()127
128 seth(0)129
130 fd(200)131
132 circle(-5, 90)133
134 fd(10)135
136 circle(-5, 90)137
138 fd(207)139
140 circle(-5, 90)141
142 fd(10)143
144 circle(-5, 90)145
146 end_fill()147
148
149 #鼻子
150
151 defnose():152
153 my_goto(-10, 158)154
155 fillcolor('#e70010')156
157 begin_fill()158
159 circle(20)160
161 end_fill()162
163
164 #黑眼睛
165
166 defblack_eyes():167
168 seth(0)169
170 my_goto(-20, 195)171
172 fillcolor('#000000')173
174 begin_fill()175
176 circle(13)177
178 end_fill()179
180
181 pensize(6)182
183 my_goto(20, 205)184
185 seth(75)186
187 circle(-10, 150)188
189 pensize(3)190
191
192 my_goto(-17, 200)193
194 seth(0)195
196 fillcolor('#ffffff')197
198 begin_fill()199
200 circle(5)201
202 end_fill()203
204 my_goto(0, 0)205
206
207
208
209 #脸
210
211 defface():212
213 fd(183)214
215 fillcolor('#ffffff')216
217 begin_fill()218
219 lt(45)220
221 circle(120, 100)222
223
224 seth(90)225
226 eyes()227
228 seth(180)229
230 penup()231
232 fd(60)233
234 pendown()235
236 seth(90)237
238 eyes()239
240 penup()241
242 seth(180)243
244 fd(64)245
246 pendown()247
248 seth(215)249
250 circle(120, 100)251
252 end_fill()253
254
255 #头型
256
257 defhead():258
259 penup()260
261 circle(150, 40)262
263 pendown()264
265 fillcolor('#00a0de')266
267 begin_fill()268
269 circle(150, 280)270
271 end_fill()272
273
274 #画哆啦A梦
275
276 defDoraemon():277
278 #头部
279
280 head()281
282
283 #围脖
284
285 scarf()286
287
288 #脸
289
290 face()291
292
293 #红鼻子
294
295 nose()296
297
298 #嘴巴
299
300 mouth()301
302
303 #胡须
304
305 beard()306
307
308 #身体
309
310 my_goto(0, 0)311
312 seth(0)313
314 penup()315
316 circle(150, 50)317
318 pendown()319
320 seth(30)321
322 fd(40)323
324 seth(70)325
326 circle(-30, 270)327
328
329
330 fillcolor('#00a0de')331
332 begin_fill()333
334
335 seth(230)336
337 fd(80)338
339 seth(90)340
341 circle(1000, 1)342
343 seth(-89)344
345 circle(-1000, 10)346
347
348 #print(pos())
349
350
351 seth(180)352
353 fd(70)354
355 seth(90)356
357 circle(30, 180)358
359 seth(180)360
361 fd(70)362
363
364 #print(pos())
365
366 seth(100)367
368 circle(-1000, 9)369
370
371 seth(-86)372
373 circle(1000, 2)374
375 seth(230)376
377 fd(40)378
379
380 #print(pos())
381
382
383
384 circle(-30, 230)385
386 seth(45)387
388 fd(81)389
390 seth(0)391
392 fd(203)393
394 circle(5, 90)395
396 fd(10)397
398 circle(5, 90)399
400 fd(7)401
402 seth(40)403
404 circle(150, 10)405
406 seth(30)407
408 fd(40)409
410 end_fill()411
412
413 #左手
414
415 seth(70)416
417 fillcolor('#ffffff')418
419 begin_fill()420
421 circle(-30)422
423 end_fill()424
425
426 #脚
427
428 my_goto(103.74, -182.59)429
430 seth(0)431
432 fillcolor('#ffffff')433
434 begin_fill()435
436 fd(15)437
438 circle(-15, 180)439
440 fd(90)441
442 circle(-15, 180)443
444 fd(10)445
446 end_fill()447
448
449 my_goto(-96.26, -182.59)450
451 seth(180)452
453 fillcolor('#ffffff')454
455 begin_fill()456
457 fd(15)458
459 circle(15, 180)460
461 fd(90)462
463 circle(15, 180)464
465 fd(10)466
467 end_fill()468
469
470 #右手
471
472 my_goto(-133.97, -91.81)473
474 seth(50)475
476 fillcolor('#ffffff')477
478 begin_fill()479
480 circle(30)481
482 end_fill()483
484
485 #口袋
486
487 my_goto(-103.42, 15.09)488
489 seth(0)490
491 fd(38)492
493 seth(230)494
495 begin_fill()496
497 circle(90, 260)498
499 end_fill()500
501
502 my_goto(5, -40)503
504 seth(0)505
506 fd(70)507
508 seth(-90)509
510 circle(-70, 180)511
512 seth(0)513
514 fd(70)515
516
517 #铃铛
518
519 my_goto(-103.42, 15.09)520
521 fd(90)522
523 seth(70)524
525 fillcolor('#ffd200')526
527 #print(pos())
528
529 begin_fill()530
531 circle(-20)532
533 end_fill()534
535 seth(170)536
537 fillcolor('#ffd200')538
539 begin_fill()540
541 circle(-2, 180)542
543 seth(10)544
545 circle(-100, 22)546
547 circle(-2, 180)548
549 seth(180-10)550
551 circle(100, 22)552
553 end_fill()554
555 goto(-13.42, 15.09)556
557 seth(250)558
559 circle(20, 110)560
561 seth(90)562
563 fd(15)564
565 dot(10)566
567 my_goto(0, -150)568
569
570 #画眼睛
571
572 black_eyes()573
574
575 if __name__ == '__main__':576
577 screensize(800,600, "#f0f0f0")578
579 pensize(3) #画笔宽度
580
581 speed(9) #画笔速度
582
583 Doraemon()584
585 my_goto(100, -300)586
587 #write('by dongdong', font=("Bradley Hand ITC", 30, "bold"))
588
589 mainloop()
机器猫