小白学Python(20)—— Turtle 海龟绘图
Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
在1966年,Seymour Papert和Wally Feurzig发明了一种专门给儿童学习编程的语言——LOGO语言,它的特色就是通过编程指挥一个小海龟(turtle)在屏幕上绘图。海龟绘图(Turtle Graphics)后来被移植到各种高级语言中,Python内置了turtle库,基本上100%复制了原始的Turtle Graphics的所有功能。
使用之前需要导入库:
from turtle import *
画笔运动命令
|
命令 |
说明 |
|
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.circle() |
画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆 |
|
setx( ) |
将当前x轴移动到指定位置 |
|
sety( ) |
将当前y轴移动到指定位置 |
|
setheading(angle) |
设置当前朝向为angle角度 |
|
home() |
设置当前画笔位置为原点,朝向东。 |
|
dot(r) |
绘制一个指定直径和颜色的圆点 |
画笔控制命令
|
命令 |
说明 |
|
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形状 |
如,画一个长方形:
# 导入turtle包的所有内容:
from turtle import * # 设置笔刷宽度:
width(4) # 前进:
forward(200)
# 右转90度:
right(90) # 笔刷颜色:
pencolor('red')
forward(100)
right(90) pencolor('green')
forward(200)
right(90) pencolor('blue')
forward(100)
right(90) # 调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()

更多操作请参考turtle库的说明。https://docs.python.org/3.6/library/frameworks.html
五角星:
from turtle import *
for i in range(5):
fd(200)
rt(144)
done()

填充颜色
from turtle import *
width(4)
pencolor("yellow")
fillcolor("red") begin_fill()
for i in range(5):
fd(200)
rt(144)
end_fill()
done()

太阳花:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()

太极图:
from turtle import * circle(100,180)
circle(200,180)
circle(100,-180) fillcolor('black') begin_fill() circle(100,180)
circle(200,180)
circle(100,-180) end_fill() penup() goto(0,100)
dot(50)
goto(0,-100)
pencolor('white')
dot(50) hideturtle() done()

利用循环嵌套方法画图:
from turtle import * for i in range(6):
pendown()
fd(150) for j in range(10):
circle(40)
lt(36)
lt(60)
penup()
goto(0,0)
done()

画一个笑脸
from turtle import *
def go(x,y):
penup()
goto(x,y)
pendown()
def arc(radius):
circle(radius,90)
reset()
speed(0)
go(0,-150)
circle(200)
go(50,100)
seth(225)
arc(10)
arc(50)
arc(10)
arc(50)
go(-50,100)
seth(-45)
arc(-10)
arc(-50)
arc(-10)
arc(-50)
go(-70,-50)
arc(100)
hideturtle()

绘制一棵分型树:
from turtle import * # 设置色彩模式是RGB:
colormode(255) lt(90)
lv = 14
l = 120
s = 45 width(lv) # 初始化RGB颜色:
r = 0
g = 0
b = 0
pencolor(r, g, b) penup()
bk(l)
pendown()
fd(l) def draw_tree(l, level):
global r, g, b
# save the current pen width
w = width() # narrow the pen width
width(w * 3.0 / 4.0)
# set color:
r = r + 1
g = g + 2
b = b + 3
pencolor(r % 200, g % 200, b % 200) l = 3.0 / 4.0 * l lt(s)
fd(l) if level < lv:
draw_tree(l, level + 1)
bk(l)
rt(2 * s)
fd(l) if level < lv:
draw_tree(l, level + 1)
bk(l)
lt(s) # restore the previous pen width
width(w) speed("fastest") draw_tree(l, 4) done()

螺旋图:

from turtle import *
for i in range(100):
fd(2*i)
lt(90)
done()

from turtle import * speed(10) for i in range(200):
fd(2*i)
lt(92)
done()

小白学Python(20)—— Turtle 海龟绘图的更多相关文章
- 小白学 Python 数据分析(20):pyecharts 概述
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- 小白学 Python(20):迭代器基础
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python 爬虫(20):Xpath 进阶
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python(24):Excel 基础操作(下)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(5):基础运算符(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(6):基础运算符(下)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(9):基础数据结构(列表)(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(21):生成器基础
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(22):time 和 calendar 模块简单使用
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(23):Excel 基础操作(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
随机推荐
- Winserver-禁止程序启动
注册表限制程序启动 经测试,可以阻止手动启动,但在job中还是会有启动的进程,这个待确定. run→regedit 添加程序只写exe名就行 手动不能运行了
- springboot结合jsp页面详解
第一次写博客,其实就是为了约束我自己,写的不一定对,互相借鉴吧!有不对的地方请多多指正,谢谢! 今天我们来看一下springboot结合jsp页面的具体操作: 1.首先我们先看一下目录结构 由上面我们 ...
- sqlserver 中批量删除\r\n 换行符
从Excel中向sqlserver 中批量粘贴数据时 可能会粘贴进去换行符 \r\n 这时候在查询时候是看不见的 只有把该字段赋值到‘’中才能发现换行. 批量替换语句: update [表名]set ...
- 配置中心Apollo多环境部署
- 2019hdu多校 K-th Closest Distance
题目链接:Click here 大致题意:q次询问,每次询问你区间[L,R]中|p-ai|的值第k小的是多少 Solution: 直接找是很困难的,我们考虑二分答案,那么本题就十分简单了 我们对权值维 ...
- nuget push 程序包到nuget服务器时报错 406 (Not Acceptable)
1.在window服务器上部署nuget服务器时,发布包时出现请求报错 406 (Not Acceptable) 验证用户名.密码正确的情况下,还是出现上面错误.后面跟踪服务器日志,发现window\ ...
- 详解cocos2dx 3.0的release版本在android平台的签名过程
当您的游戏准备发布前,需要编译成为release版本,命令中需要增加 -m release,编译命令如下: cocos compile -p android -m release 在编译结束后,生成x ...
- (62)通信协议之一protobuf
Protobuf协议特点分析 KingKa.吴永聪 1.protobuf是什么? protobuf(Google Protocol Buffers)是Google提供的一个具有高效的协议数据交换格式 ...
- BZOJ 3168 Luogu P4100 [HEOI2013]钙铁锌硒维生素 (矩阵求逆、二分图匹配)
线性代数+图论好题. 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3168 (luogu) https://www.lu ...
- 基于数组阻塞队列 ArrayBlockingQueue 的一个队列工具类
java语言基于ArrayBlockingQueue 开发的一个根据特定前缀和后缀的队列.每天自动循环生成. 1.定义队列基类 Cookie package com.bytter.util.queue ...