turtle库的使用

一、基本介绍

turtle(海龟)库是turtle绘图体系的Python实现
- turtle绘图体系:1969年诞生,主要用于程序设计入门

  • Python语言的标准库之一
    - 入门级的图形绘制函数库

标准库
Python计算生态 = 标准库 + 第三方库
- 标准库:随解释器直接安装到操作系统中的功能模块
- 第三方库:需要经过安装才能使用的功能模块

  • 库Library、包Package、模块Module,统称模块

turtle的原理
**有一只海龟,其实在窗体正中心,在画布上游走

  • 走过的轨迹形成了绘制的图形
  • 海龟由程序控制,可以变换颜色、改变宽度等**

turtle欣赏

python turtle模块 哈利波特 python-turtle模块的应用_Python


python turtle模块 哈利波特 python-turtle模块的应用_python_02

二、turtle绘图窗体布局

python turtle模块 哈利波特 python-turtle模块的应用_python_03


python turtle模块 哈利波特 python-turtle模块的应用_Python_04


python turtle模块 哈利波特 python-turtle模块的应用_ci_05

三、turtle空间坐标体系

python turtle模块 哈利波特 python-turtle模块的应用_函数参数_06


例子:

import turtle
turtle.goto( 100, 100)
turtle.goto( 100,-100)
turtle.goto(-100,-100)
turtle.goto(-100, 100)
turtle.goto(0,0)

结果如下图:

python turtle模块 哈利波特 python-turtle模块的应用_ci_07


操作方法:

python turtle模块 哈利波特 python-turtle模块的应用_函数参数_08

四、turtle角度坐标体系

python turtle模块 哈利波特 python-turtle模块的应用_函数参数_09


python turtle模块 哈利波特 python-turtle模块的应用_函数参数_10


例子:turtle.seth(45)

python turtle模块 哈利波特 python-turtle模块的应用_函数参数_11

import turtle
turtle.left(45)
turtle.fd(150)
turtle.right(135)
turtle.fd(300)
turtle.left(135)
turtle.fd(150)

python turtle模块 哈利波特 python-turtle模块的应用_函数参数_12

五、RGB色彩体系

由三种颜色构成的万物色

  • RGB指红蓝绿三个通道的颜色组合
  • 覆盖视力所能感知的所有颜色
  • RGB每色取值范围0-255整数或0-1小数

常用RGB色彩

python turtle模块 哈利波特 python-turtle模块的应用_Python_13


python turtle模块 哈利波特 python-turtle模块的应用_Python_14

六、库引用及import

扩充Python程序功能的方式

  • 使用import保留字完成,采用.()编码风格
    import <库名>
    <库名>.<函数名>(<函数参数>)
import turtle
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
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()

import更多方法:
使用from和import保留字共同完成
from <库名> import <函数名>
from <库名> import *
<函数名>(<函数参数>)

from turtle import *
setup(650, 350, 200, 200)
penup()
fd(-250)
pendown()
pensize(25)
pencolor("purple")
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()

这样的好处是减少了每行turtle的使用。

两种方法比较:

import <库名>
<库名>.<函数名>(<函数参数>)

from <库名> import <函数名>
from <库名> import *
<函数名>(<函数参数>)

第一种方法不会出现函数重名问题,第二种方法则会出现

使用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("purple")
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()

七、turtle画笔控制函数

penup(), pendown()
pensize(), pencolor()

画笔操作后一直有效,一般成对出现

  • turtle.penup() 别名 turtle.pu()
    抬起画笔,海龟在飞行
  • turtle.pendown() 别名 turtle.pd()
    落下画笔,海龟在爬行

画笔设置后一直有效,直至下次重新设置

  • turtle.pensize(width) 别名 turtle.width(width)
    画笔宽度,海龟的腰围
  • turtle.pencolor(color) color为颜色字符串或r,g,b值
    画笔颜色,海龟在涂装

pencolor(color)的color可以有三种形式

  • 颜色字符串 :turtle.pencolor(“purple”)
  • RGB的小数值:turtle.pencolor(0.63, 0.13, 0.94)
  • RGB的元组值:turtle.pencolor((0.63,0.13,0.94))

八、turtle运动控制函数

fd()
circle()

控制海龟行进:走直线 & 走曲线

  • turtle.forward(d) 别名 turtle.fd(d)
    向前行进,海龟走直线
  • d: 行进距离,可以为负数
  • turtle.circle(r, extent=None)
    根据半径r绘制extent角度的弧形
  • r: 默认圆心在海龟左侧r距离的位置
  • extent: 绘制角度,默认是360度整圆

python turtle模块 哈利波特 python-turtle模块的应用_函数参数_15


画笔设置后一直有效,直至下次重新设置

  • turtle.forward(d) 别名 turtle.fd(d)
    向前行进,海龟走直线
  • d: 行进距离,可以为负数

八、turtle方向控制函数

控制海龟面对方向: 绝对角度 & 海龟角度

  • turtle.setheading(angle) 别名 turtle.seth(angle)
    改变行进方向,海龟走角度
  • angle: 行进方向的绝对角度

python turtle模块 哈利波特 python-turtle模块的应用_函数参数_16


控制海龟面对方向: 绝对角度 & 海龟角度

  • turtle.left(angle) 海龟向左转
  • turtle.right(angle) 海龟向右转
  • angle: 在海龟当前行进方向上旋转的角度

九、循环语句与range()函数

for 和 in 保留字
range()

按照一定次数循环执行一组语句
for <变量> in range(<次数>):
<被循环执行的语句>
<变量>表示每次循环的计数,0到<次数>-1

>>> for i in range(5):
print(i)
0
1
2
3
4
>>> for i in range(5):
print("Hello:"
,i)
Hello: 0
Hello: 1
Hello: 2
Hello: 3
Hello: 4

range()函数
产生循环计数序列

  • range(N)
    产生 0 到 N-1的整数序列,共N个
  • range(M,N)
    例如: range(5)
    0, 1, 2, 3, 4
    产生 M 到 N-1的整数序列,共N-M个
    例如:range(2, 5)
    2, 3, 4

十、"Python蟒蛇绘制"代码分析

python turtle模块 哈利波特 python-turtle模块的应用_Python_17


python turtle模块 哈利波特 python-turtle模块的应用_python_18


python turtle模块 哈利波特 python-turtle模块的应用_Python_19


python turtle模块 哈利波特 python-turtle模块的应用_函数参数_20

总结

  • turtle库的海龟绘图法
  • turtle.setup()调整绘图窗体在电脑屏幕中的布局
  • 画布上以中心为原点的空间坐标系: 绝对坐标&海龟坐标
  • 画布上以空间x轴为0度的角度坐标系: 绝对角度&海龟角度
  • 常用RGB色彩体系的认识
  • turtle程序语法元素分析
  • 库引用: import、from…import、import…as…
  • penup()、pendown()、pensize()、pencolor()
  • fd()、circle()、seth()
  • 循环语句:for和in、range()函数