turtle库的使用
1.基本
turtle库基本介绍
-
turtle库是turtle绘图体系的Python实现
1969年诞生
-
是标准库之一
-
Python计算生态=标准库+第三方库
- 标准库:随解释器直接安装到操作系统中的功能模块
- 第三方库:需要安装下能使用的功能模块
- 库Library,包Package,模块Module,统称模块
-
turtle是一种真实的存在
turtle绘图窗口布局
- setup()设置窗体大小及位置
turtle.setup(width,height,startx,staty)
-
四个参数中后两个可选
-
setup()不是必须的
-
<startx,staty>默认在窗口正中心
turtle空间坐标体系
- 绝对坐标
turtle.goto(x,y)
【eg.2】
import turtle
turtle.goto(100,100)
turtle.goto(150,-50)
turtle.goto(100,-100)
turtle.goto(0,-150)
turtle.goto(-100,-100)
turtle.goto(-150,-50)
turtle.goto(-100,100)
turtle.goto(0,0)
- 相对坐标
turtle.circle(r,angle)
turtle.bk(d) #向前
turtle.fd(d) #向后
turtle角度坐标体系
- 绝对角度
turtle.seth(angle)
- seth()改变海龟行进方向
- seth()只改变方向但不行进
- angle为绝对度数
RGB色彩体系
- 红绿蓝三原色
2.库引用和import
- 扩程序功能的方式
- 使用import保留字完成,采用.()编码风格
import <库名>
<库名>.<函数名>(<函数参数>)
- 使用from和import保留字共同完成
from <库名> import <函数名>
from <库名> import*
<函数名>(<函数参数>)
【eg.2】改编用程序绘制蟒蛇
import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("red")
turtle.seth(-40)
for i in range(4):
turtle.circle(40,80)
turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done()
from turtle import*
setup(650,350,200,200)
penup()
fd(-250)
pendown()
pensize(25)
pencolor("red")
seth(-40)
for i in range(4):
circle(40,80)
circle(-40,80)
circle(40,80/2)
fd(40)
circle(16,180)
fd(40*2/3)
done()
- 但可能会出现函数名重复的情况
- 使用import 和as保留字共同完成
import <库名> as <库别名>
<库别名>.<函数名>(<函数参数>)
-
- 给调用的外部关联库关联一个更短,更适合自己的名字
- 冗余量最小且不会出现函数名重复
import turtle as t
t.setup(650,350,200,200)
t.penup()
t.fd(-250)
t.pendown()
t.pensize(25)
t.pencolor("red")
t.seth(-40)
for i in range(4):
t.circle(40,80)
t.circle(-40,80)
t.circle(40,80/2)
t.fd(40)
t.circle(16,180)
t.fd(40*2/3)
t.done()
3. turtle 画笔控制函数
- 画笔操作一直有效,一般成对出现
#抬起画笔
turtle.penup() #别名 turtle.pu()
#落下画笔
turtle.pendown() #别名 turtle.pd()
- 画笔设置后一直有效,直至下次重新设置
#画笔宽度
turtle.pensize(widle) #别名 turtle.width(width)
#画笔颜色
turtle.pencolor(color) #color为颜色字符串或rgb值
pencolor(color)的color参数可以有三种形式
-
颜色字符串 :turtle.pencolor(“red”)
RGB的最小值 :turtle.pencolor(0.63,0.13,0.94)
RGB的元组值 : turtle.pencolor((0.63,0.13,0.94))
4. 运动控制函数
控制海龟行进:走直线和走曲线
turtle.forward(d) 别名:turtle.fd(d)
- 向前行进,海龟走直线
- d:行进距离,可以为负数(倒退行进)
turtle.circle(r,extent=None)
- 根据半径r绘制extent角度的弧形
- r:默认圆心在海龟左侧r距离的位置
- extent:绘制角度,默认是360度整圆
5. turtle方向控制函数
控制海龟面对方向:绝对角度和海龟角度
只改变运动方向,不动海龟
turtle.setheading(angle) #别名:turtle.seth(angle)
- angle:改变行进方向,海龟走角度
turtle.left(angle) #海龟向左转
turtle.right(angle) #海龟向右转
- angle:在海龟当前行进方向上旋转的角度
6. 循环语句与range()函数
6.1 循环语句
- 按照一定次序循环执行的一组语句
for<变量> in range (<参数>)
<被循环执行的语句>
- 变量 表示每次循环的次数,0到 次数-1
6.2 range()函数
- 产生循环计数序列
range(N)
- 产生 0 到 N-1的整数序列,共N个
range(M,N)
- 产生 M到 N-1的整数序列,共N-M个
【eg.1】用程序绘制蟒蛇
import turtle
turtle.setup(650,350,200,200) #设置窗体大小和位置
turtle.penup() #抬起画笔
turtle.fd(-250) #让海龟倒退行进250
turtle.pendown() #落下画笔
turtle.pensize(25) #设置画笔大小
turtle.pencolor("red")
turtle.seth(-40) #设置画笔方向
for i in range(4):
turtle.circle(40,80)
turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done() #程序不会自动退出
- Python蟒蛇颜色
- Python蟒蛇的长度
- Python蟒蛇的方向