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 海龟绘图的更多相关文章

  1. 小白学 Python 数据分析(20):pyecharts 概述

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...

  2. 小白学 Python(20):迭代器基础

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  3. 小白学 Python 爬虫(20):Xpath 进阶

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  4. 小白学 Python(24):Excel 基础操作(下)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  5. 小白学 Python(5):基础运算符(上)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  6. 小白学 Python(6):基础运算符(下)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  7. 小白学 Python(9):基础数据结构(列表)(上)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  8. 小白学 Python(21):生成器基础

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  9. 小白学 Python(22):time 和 calendar 模块简单使用

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  10. 小白学 Python(23):Excel 基础操作(上)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

随机推荐

  1. 6423. 【NOIP2019模拟11.11】画

    题目描述 Description Input Output Sample Input 3 2 3 3 6 5 1 2 1 3 Sample Output 15 Data Constraint 题解 迫 ...

  2. Swiper 的引入

    1. 从官网下载必要资源 https://www.swiper.com.cn/download/index.html#file1 2. 在项目中<head>中引入swiper.min.cs ...

  3. java中System类

    System作为系统类,在JDK的java.lang包中,可见它也是一种java的核心语言特性.System类的构造器由private修饰,不允许被实例化.因此,类中的方法也都是static修饰的静态 ...

  4. sql 查询 某字段是否重复

    select count(*) from ( select * from 客户 )C GROUP BY 客户编码 select * from ( select count(*)num from ( s ...

  5. Linux入门培训教程 linux下拷贝cp删除rm移动mv命令参数以及说明

    拷贝移动删除在windows中看起来这么简单,但linux经常使用的文字界面,所以对于linux系统 下拷贝cp删除 rm 移动mv命令参数就不得不需要了解和学习了 cp 该命令的功能是将给出的文件或 ...

  6. .NET面试题系列(十九)Socket网络异常类型

    序言 资料 异常测试之Socket网络异常

  7. box-shadow inset

    安卓支持,ios不支持:box-shadow: 0px 0px 2px inset rgba(0,0,0,0.08); 安卓,ios都支持:box-shadow: inset 0px 0px 2px ...

  8. http中post 和 get 请求方法区别

    前言 做Web开发就一定会涉及到浏览器和服务器的交互,所以了解浏览器和服务器交互的方式就尤为重要.从接触B/S开始就已经接触到了get和post,但是对它们的了解确实不深入.在后来不断的做项目过程中, ...

  9. 实验报告三&&第五周总结

    1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) ① 统计该字符串中字母s出现的次数. ② 统计该字符串中子串“is ...

  10. 一个Qt线程的例子,用于说明QWaitCondition的作用

      描述可能比较麻烦,还是直接上代码吧! main.cpp #include <QApplication> #include "mainpage.h" int main ...