编程学习网 > 编程语言 > Python > Python-Turtle海龟库-从入门到精通教程
2023
11-11

Python-Turtle海龟库-从入门到精通教程

python2.6版本中后引入的一个简单的绘图工具,叫做海龟绘图(Turtle Graphics),turtle库是python的内部库,使用导入即可 

1.import turtle
先说明一下turtle绘图的基础知识:
1. 画布(canvas)
画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置
1.1 设置画布大小
turtle.screensize(canvwidth=None, canvheight=None, bg=None)
参数分别为画布的宽(单位像素), 高, 背景颜色
如:
1turtle.screensize(800, 600, "green")
2turtle.screensize() #返回默认大小(400, 300)
turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
参数:
width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
(startx, starty): 这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心
如:
1turtle.setup(width=0.6, height=0.6)
2turtle.setup(width=800, height=800, startx=100, starty=100)
2. 画笔
2.1 画笔的状态
在画布上,默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝x轴正方向小乌龟. 这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中, 就是使用位置方向描述小乌龟(画笔)的状态
2.2 画笔的属性
画笔(画笔的属性,颜色、画线的宽度)
turtle.pensize():设置画笔的宽度;
turtle.pencolor(); 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组,
1  >>> pencolor('brown')
2    >>> tup = (0.2, 0.8, 0.55)
3    >>> pencolor(tup)
4    >>> pencolor()
5    '#33cc8c'
turtle.speed(speed): 设置画笔移动速度,画笔绘制的速度范围[0,10]整数, 数字越大越快
2.3 绘图命令
操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令
(1)画笔运动命令:
命令 说明
turtle.forward(distance) 向当前画笔方向移动distance像素长
turtle.backward(distance) 向当前画笔相反方向移动distance像素长度
turtle.right(degree) 顺时针移动degree°
turtle.left(degree) 逆时针移动degree°
turtle.pendown() 移动时绘制图形,缺省时也为绘制
turtle.goto(x,y) 将画笔移动到坐标为x,y的位置
turtle.penup() 移动时不绘制图形,提起笔,用于另起一个地方绘制时用
turtle.speed(speed) 画笔绘制的速度范围[0,10]整数
turtle.circle() 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
(2)画笔控制命令:
命令 说明
turtle.pensize(width) 绘制图形时的宽度
turtle.pencolor() 画笔颜色
turtle.fillcolor(colorstring) 绘制图形的填充颜色
turtle.color(color1, color2) 同时设置pencolor=color1, fillcolor=color2
turtle.filling() 返回当前是否在填充状态
turtle.begin_fill() 准备开始填充图形
turtle.end_fill() 填充完成;
turtle.hideturtle() 隐藏箭头显示;
turtle.showturtle() 与hideturtle()函数对应
(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的参数也是可选项
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) # 半圆
4. 绘图举例
4.1 太阳花
 1import turtle as t
 2import time
 3t.color("red", "yellow")
 4t.speed(10)
 5t.begin_fill()
 6for _ in range(50):
 7    t.forward(200)
 8    t.left(170)
 9t.end_fill()
10time.sleep(1)


4.2 绘制小蟒蛇
 1import turtle
 2
 3def drawSnake(rad, angle, len, neckrad):
 4    for _ in range(len):
 5        turtle.circle(rad, angle)
 6        turtle.circle(-rad, angle)
 7    turtle.circle(rad, angle/2)
 8    turtle.forward(rad/2)  # 直线前进
 9    turtle.circle(neckrad, 180)
10    turtle.forward(rad/4)
11
12if __name__ == "__main__":
13   turtle.setup(1500, 1400, 0, 0)
14   turtle.pensize(30)  # 画笔尺寸
15   turtle.pencolor("green")
16   turtle.seth(-40)    # 前进的方向
17   drawSnake(70, 80, 2, 15)



4.3 绘制五角星
 1import turtle
 2import time
 3
 4
 5turtle.pensize(5)
 6turtle.pencolor("yellow")
 7turtle.fillcolor("red")
 8
 9turtle.begin_fill()
10
11for _ in range(5):
12    turtle.forward(200)
13    turtle.right(144)
14turtle.end_fill()
15time.sleep(2)
16
17turtle.penup()
18turtle.goto(-150,-120)
19turtle.color("violet")
20turtle.write("Done", font=('Arial', 40, 'normal'))
21time.sleep(1)


4.4 小猪佩奇
  1# coding:utf-8
  2import turtle as t
  3# 绘制小猪佩奇
  4# =======================================
  5
  6t.pensize(4)
  7t.hideturtle()
  8t.colormode(255)
  9t.color((255, 155, 192), "pink")
 10t.setup(840, 500)
 11t.speed(10)
 12
 13# 鼻子
 14t.pu()
 15t.goto(-100,100)
 16t.pd()
 17t.seth(-30)
 18t.begin_fill()
 19a = 0.4
 20for i in range(120):
 21    if 0 <= i < 30 or 60 <= i < 90:
 22        a = a+0.08
 23        t.lt(3)  # 向左转3度
 24        t.fd(a)  # 向前走a的步长
 25    else:
 26        a = a-0.08
 27        t.lt(3)
 28        t.fd(a)
 29        t.end_fill()
 30
 31t.pu()
 32t.seth(90)
 33t.fd(25)
 34t.seth(0)
 35t.fd(10)
 36t.pd()
 37t.pencolor(255, 155, 192)
 38t.seth(10)
 39t.begin_fill()
 40t.circle(5)
 41t.color(160, 82, 45)
 42t.end_fill()
 43
 44t.pu()
 45t.seth(0)
 46t.fd(20)
 47t.pd()
 48t.pencolor(255, 155, 192)
 49t.seth(10)
 50t.begin_fill()
 51t.circle(5)
 52t.color(160, 82, 45)
 53t.end_fill()
 54
 55# 头
 56t.color((255, 155, 192), "pink")
 57t.pu()
 58t.seth(90)
 59t.fd(41)
 60t.seth(0)
 61t.fd(0)
 62t.pd()
 63t.begin_fill()
 64t.seth(180)
 65t.circle(300, -30)
 66t.circle(100, -60)
 67t.circle(80, -100)
 68t.circle(150, -20)
 69t.circle(60, -95)
 70t.seth(161)
 71t.circle(-300, 15)
 72t.pu()
 73t.goto(-100, 100)
 74t.pd()
 75t.seth(-30)
 76a = 0.4
 77for i in range(60):
 78    if 0 <= i < 30 or 60 <= i <90:
 79        a = a+0.08
 80        t.lt(3)  # 向左转3度
 81        t.fd(a)  # 向前走a的步长
 82    else:
 83        a = a-0.08
 84        t.lt(3)
 85        t.fd(a)
 86        t.end_fill()
 87
 88# 耳朵
 89t.color((255, 155, 192), "pink")
 90t.pu()
 91t.seth(90)
 92t.fd(-7)
 93t.seth(0)
 94t.fd(70)
 95t.pd()
 96t.begin_fill()
 97t.seth(100)
 98t.circle(-50, 50)
 99t.circle(-10, 120)
100t.circle(-50, 54)
101t.end_fill()
102
103t.pu()
104t.seth(90)
105t.fd(-12)
106t.seth(0)
107t.fd(30)
108t.pd()
109t.begin_fill()
110t.seth(100)
111t.circle(-50, 50)
112t.circle(-10, 120)
113t.circle(-50, 56)
114t.end_fill()
115
116#眼睛
117t.color((255, 155, 192), "white")
118t.pu()
119t.seth(90)
120t.fd(-20)
121t.seth(0)
122t.fd(-95)
123t.pd()
124t.begin_fill()
125t.circle(15)
126t.end_fill()
127
128t.color("black")
129t.pu()
130t.seth(90)
131t.fd(12)
132t.seth(0)
133t.fd(-3)
134t.pd()
135t.begin_fill()
136t.circle(3)
137t.end_fill()
138
139t.color((255, 155, 192), "white")
140t.pu()
141t.seth(90)
142t.fd(-25)
143t.seth(0)
144t.fd(40)
145t.pd()
146t.begin_fill()
147t.circle(15)
148t.end_fill()
149
150t.color("black")
151t.pu()
152t.seth(90)
153t.fd(12)
154t.seth(0)
155t.fd(-3)
156t.pd()
157t.begin_fill()
158t.circle(3)
159t.end_fill()
160
161# 腮
162t.color((255, 155, 192))
163t.pu()
164t.seth(90)
165t.fd(-95)
166t.seth(0)
167t.fd(65)
168t.pd()
169t.begin_fill()
170t.circle(30)
171t.end_fill()
172
173# 嘴
174t.color(239, 69, 19)
175t.pu()
176t.seth(90)
177t.fd(15)
178t.seth(0)
179t.fd(-100)
180t.pd()
181t.seth(-80)
182t.circle(30, 40)
183t.circle(40, 80)
184
185# 身体
186t.color("red", (255, 99, 71))
187t.pu()
188t.seth(90)
189t.fd(-20)
190t.seth(0)
191t.fd(-78)
192t.pd()
193t.begin_fill()
194t.seth(-130)
195t.circle(100,10)
196t.circle(300,30)
197t.seth(0)
198t.fd(230)
199t.seth(90)
200t.circle(300,30)
201t.circle(100,3)
202t.color((255,155,192),(255,100,100))
203t.seth(-135)
204t.circle(-80,63)
205t.circle(-150,24)
206t.end_fill()
207
208# 手
209t.color((255,155,192))
210t.pu()
211t.seth(90)
212t.fd(-40)
213t.seth(0)
214t.fd(-27)
215t.pd()
216t.seth(-160)
217t.circle(300,15)
218t.pu()
219t.seth(90)
220t.fd(15)
221t.seth(0)
222t.fd(0)
223t.pd()
224t.seth(-10)
225t.circle(-20,90)
226
227t.pu()
228t.seth(90)
229t.fd(30)
230t.seth(0)
231t.fd(237)
232t.pd()
233t.seth(-20)
234t.circle(-300,15)
235t.pu()
236t.seth(90)
237t.fd(20)
238t.seth(0)
239t.fd(0)
240t.pd()
241t.seth(-170)
242t.circle(20,90)
243
244# 脚
245t.pensize(10)
246t.color((240,128,128))
247t.pu()
248t.seth(90)
249t.fd(-75)
250t.seth(0)
251t.fd(-180)
252t.pd()
253t.seth(-90)
254t.fd(40)
255t.seth(-180)
256t.color("black")
257t.pensize(15)
258t.fd(20)
259
260t.pensize(10)
261t.color((240, 128, 128))
262t.pu()
263t.seth(90)
264t.fd(40)
265t.seth(0)
266t.fd(90)
267t.pd()
268t.seth(-90)
269t.fd(40)
270t.seth(-180)
271t.color("black")
272t.pensize(15)
273t.fd(20)
274
275# 尾巴
276t.pensize(4)
277t.color((255, 155, 192))
278t.pu()
279t.seth(90)
280t.fd(70)
281t.seth(0)
282t.fd(95)
283t.pd()
284t.seth(0)
285t.circle(70, 20)
286t.circle(10, 330)
287t.circle(70, 30)
288t.done()

以上就是Python-Turtle海龟库-从入门到精通教程的详细内容,想要了解更多Python教程欢迎持续关注编程学习网。

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取