相信有不少人学习 python 都是听了老前辈的推荐
"学 python 好, python 有趣的代码多"
比如说画一只小狮子
这就是今天想要介绍的绘制图形库 - turtle
一, 什么是 turtle 库
turtle 库是 python 的基础绘图库
这个库被介绍为一个最常用的用来给孩子们介绍编程知识的方法库,
其主要是用于程序设计入门, 是标准库之一, 利用 turtle 可以制作很多复杂的绘图.
简单理解 turtle 库就是一个用于图形绘制的基础库.
可能刚刚就有人有疑问, 为什么绘画库不叫 painting, 而叫 turtle? 这就不得不说 turtle 库的思想.
在画布上, 默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝 x 轴正方向小乌龟.
二, 有哪些函数?
1. 画布
既然说 turtle 是一个绘图库, 那么怎么能不说画布呢?
画布函数有两个
第一个为 screensize()其中包含三个参数, 它们分别为
canvwidth=None, 画布的长度
canvheight=None, 画布的宽度
bg=None, 画布的背景色
画布是必不可少的, 为防止有些粗心鬼忘设定, 所以默认无设定时为一个 400*300 的白画布
第二个为 setup(), 有四个参数
width,height 两者表示为画布的长与宽, 当传参为整数时, 表示像素长度单位, 当传参为小数时, 表示与屏幕的比例
starX,starY 则表示一组坐标值, 坐标表示画布左上角的位置, 若为空则默认为屏幕中心
两个函数各有优劣, screensize()可以设定背景色, setup 可以设定位置, 如下图所比较
- import turtle
- turtle.screensize(400,300,"green")
- turtle.done()
- import turtle
- turtle.setup(600,400,100,100)
- turtle.done();
2. 画笔
turtle 绘图中, 就是使用位置方向描述小乌龟 (画笔) 的状态.
所以画笔一共有三个基础属性函数, 分别为
turtle.pensize(): 设置画笔的宽度;
turtle.pencolor(); 设置画笔的颜色, 颜色采用 RGB, 也可以直接输入对应颜色如 "red", 若无参数时, 采用当前颜色,
turtle.speed(); 设置画笔的速度, 从 1`10, 数字越大则速度越快.
代码如下设定
- import turtle
- turtle.pensize(5)
- turtle.pencolor("green")
- turtle.forward(100)
- turtle.done();
3. 操作
操作可就相对来说多了一些, 这里也只简单罗列几种常用的
(1)画笔运动命令
pendown()与 penup()这两个函数可是哥俩, 一目了然的意思, 一个负责抬起画笔, 一个负责落下画笔
表示画笔状态的转换, 自然也就不需要传参数进入了
forword()函数用来控制画笔向正方向移动(正方向默认为东)
有向正方向, 自然就有负方向函数 backward()
还有就是 goto 函数, 就像直译, 表示将画笔移动到坐标轴的某处, goto 传参数为坐标信息
就像刚刚介绍的那样, turtle 使用的为极坐标方式, 参数为方向与长度
所以 left 与 right 表示为顺时针与逆时针, 传入参数为角度
(2)画笔控制命令
画笔控制命令相对来说比较少, 主要操作便是填充
turtle.fillcolor(colorstring) 绘制图形的填充颜色
turtle.begin_fill()准备开始填充图形
turtle.end_fill()填充完成
(3) 全局控制命令
turtle.clear() 清除窗口绘制图形
turtle.undo() 撤销上一个 turtle 动作
- def hair():
- t.penup()
- t.goto(-50, 150)
- t.pendown()
- t.fillcolor('#a2774d')
- t.begin_fill()
- for j in range(10):
- t.setheading(60 - (j * 36))
- t.circle(-50, 120)
- t.end_fill()
- import turtle as t
- def hair():
- t.penup()
- t.goto(-50, 150)
- t.pendown()
- t.fillcolor('#a2774d')
- t.begin_fill()
- for j in range(10):
- t.setheading(60 - (j * 36))
- t.circle(-50, 120)
- t.end_fill()
- def face():
- t.penup()
- t.goto(0, 100)
- t.pendown()
- t.fillcolor('#f2ae20')
- t.begin_fill()
- t.setheading(180)
- t.circle(85)
- t.end_fill()
- #下巴
- t.circle(85, 120)
- t.fillcolor('white')
- t.begin_fill()
- t.circle(85, 120)
- t.setheading(135)
- t.circle(100, 95)
- t.end_fill()
- def ears(dir):
- t.penup()
- t.goto((0-dir)*30, 90)
- t.setheading(90)
- t.pendown()
- t.fillcolor('#f2ae20')
- t.begin_fill()
- t.circle(dir*30)
- t.end_fill()
- t.penup()
- t.goto((0-dir)*40, 85)
- t.setheading(90)
- t.pendown()
- t.fillcolor('white')
- t.begin_fill()
- t.circle(dir*17)
- t.end_fill()
- def nose():
- t.penup()
- t.goto(20, 0)
- t.setheading(90)
- t.pendown()
- t.fillcolor('#a2774d')
- t.begin_fill()
- t.circle(20)
- t.end_fill()
- def eye(dir):
- t.penup()
- t.goto((0-dir)*30, 20)
- t.setheading(0)
- t.pendown()
- t.fillcolor('black')
- t.begin_fill()
- t.circle(10)
- t.end_fill()
- def mouth():
- t.penup()
- t.goto(0, 0)
- t.setheading(-90)
- t.pendown()
- t.forward(50)
- t.setheading(0)
- t.circle(80, 30)
- t.penup()
- t.goto(0, -50)
- t.setheading(180)
- t.pendown()
- t.circle(-80, 30)
- hair()
- ears(1)
- ears(-1)
- face()
- eye(1)
- eye(-1)
- mouth()
- nose()
- t.done()
来源: http://www.bubuko.com/infodetail-3218504.html