python中关于turtle库的学习笔记
一、基础概念
1、画布:画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置。常用的画布方法有两个:screensize()和setup()。
(1)turtle.screensize(canvwidth, canvheight, bg):参数分别为画布的宽(单位像素), 高, 背景颜色
如:
turtle.screensize(500,1000,'green')
(2)turtle.setup(width, height, startx, starty):width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例。(startx, starty): 这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。
2、画笔:在画布上,默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝x轴正方向小乌龟。这里我们描述小乌龟时使用了两个词语:标原点(位置),面朝x轴正方向(方向),turtle绘图中, 就是使用位置方向描述小乌龟(画笔)的状态。
(1)画笔属性:
1) turtle.pensize():设置画笔的宽度;
2) turtle.pencolor():没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组。
3) turtle.speed(speed):设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。
(2)绘制命令:
1)turtle.forward(distance)(别名:turtle.fd):向当前画笔方向移动distance像素长度。
2)turtle.backward(distance):向当前画笔相反方向移动distance像素长度。
3)turtle.right(degree):顺时针移动degree°。
4)turtle.left(degree):逆时针移动degree°。
5)turtle.pendown()(别名:turtle.pd(),turtle.down()):移动时绘制图形,缺省时也为绘制。
6)turtle.goto(x,y):将画笔移动到坐标为x,y的位置。
7)turtle.penup()(别名:turtle.pu(),turtle.up()):提起笔移动,不绘制图形,用于另起一个地方绘制。
8)turtle.circle():画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆。
9)setx( ):将当前x轴移动到指定位置。
10)sety( ):将当前y轴移动到指定位置。
11)setheading(angle):设置当前朝向为angle角度。
12)home():设置当前画笔位置为原点,朝向东。
13)dot(r):绘制一个指定直径和颜色的圆点。
14)turtle.fillcolor(colorstring):绘制图形的填充颜色。
15)turtle.color(color1, color2):同时设置pencolor=color1, fillcolor=color2。
16)turtle.filling():返回当前是否在填充状态。
17)turtle.begin_fill():准备开始填充图形。
18)turtle.end_fill():填充完成。
19)turtle.hideturtle():隐藏画笔的turtle形状。
20)turtle.showturtle():显示画笔的turtle形状。
21)turtle.seth(to_angle)(别名:turtle.setheading(to_angle)):设置小海龟当前前进方向为to_angle,该角度是绝对方向的角度值。
3、实例:
(1)太阳花:

from turtle import *
begin_fill() #准备开始填充图形
pensize(2) #设置画笔的宽度
color('red','yellow') #设置画笔颜色为蓝色,填充颜色为绿色
while True:
forward(200) #画笔移动200个像素长度
left(170) #逆时针移动170°
if abs(pos())<1: #判断画笔是否回到起点
break
end_fill() #结束填充图形
done()
(2)玫瑰花:

from turtle import *
#global pen and speed
pencolor("black")
fillcolor("red")
speed(50)
s=0.15
#init poistion
penup()
goto(0,600*s)
pendown()
begin_fill()
circle(200*s,30)
for i in range(60):
lt(1)
circle(50*s,1)
circle(200*s,30)
for i in range(4):
lt(1)
circle(100*s,1)
circle(200*s,50)
for i in range(50):
lt(1)
circle(50*s,1)
circle(350*s,65)
for i in range(40):
lt(1)
circle(70*s,1)
circle(150*s,50)
for i in range(20):
rt(1)
circle(50*s,1)
circle(400*s,60)
for i in range(18):
lt(1)
circle(50*s,1)
fd(250*s)
rt(150)
circle(-500*s,12)
lt(140)
circle(550*s,110)
lt(27)
circle(650*s,100)
lt(130)
circle(-300*s,20)
rt(123)
circle(220*s,57)
end_fill()
lt(120)
fd(280*s)
lt(115)
circle(300*s,33)
lt(180)
circle(-300*s,33)
for i in range(70):
rt(1)
circle(225*s,1)
circle(350*s,104)
lt(90)
circle(200*s,105)
circle(-500*s,63)
penup()
goto(170*s,-330*s)
pendown()
lt(160)
for i in range(20):
lt(1)
circle(2500*s,1)
for i in range(220):
rt(1)
circle(250*s,1)
fillcolor('green')
penup()
goto(670*s,-480*s)
pendown()
rt(140)
begin_fill()
circle(300*s,120)
lt(60)
circle(300*s,120)
end_fill()
penup()
goto(180*s,-850*s)
pendown()
rt(85)
circle(600*s,40)
penup()
goto(-150*s,-1300*s)
pendown()
begin_fill()
rt(120)
circle(300*s,115)
lt(75)
circle(300*s,100)
end_fill()
penup()
goto(430*s,-1370*s)
pendown()
rt(30)
circle(-600*s,35)
done()
(3)正多边形渐变为圆:

import turtle
turtle.screensize(600,500,'white')
turtle.pensize(3) #设置画笔宽度为3
turtle.pencolor('blue') #设置画笔颜色为黑色
turtle.fillcolor('yellow') #设置填充颜色为黄色
turtle.begin_fill() #开始填充
turtle.forward(-300)
for i in range(3,8):
turtle.circle(20, steps=i)
turtle.forward(100)
turtle.circle(20)
turtle.end_fill()
turtle.hideturtle() #隐藏海龟
turtle.done()
4)五角星:

from turtle import *
pensize(2)
color('yellow','red')
begin_fill()
while True:
forward(220)
right(144)
if abs(pos()) < 1:
break
end_fill()
hideturtle()
done()
python中关于turtle库的学习笔记的更多相关文章
- [Python ]小波变化库——Pywalvets 学习笔记
[Python ]小波变化库——Pywalvets 学习笔记 2017年03月20日 14:04:35 SNII_629 阅读数:24776 标签: python库pywavelets小波变换 更多 ...
- Python 中的map和reduce学习笔记
map和reduce都是Python中的内置函数 map函数接受两个参数,第一个参数是函数,第二个参数是列表,将函数依次作用于列表中的元素,并返回一个元素 reduce同样以函数和列表作为参数,区别在 ...
- python中的迭代器和生成器学习笔记总结
生成器就是一个在行为上和迭代器非常类似的对象. 是个对象! 迭代,顾名思意就是不停的代换的意思,迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果.每一次对过程的重复称为一次“迭代”,而 ...
- python中的turtle库绘制图形
1. 前奏: 在用turtle绘制图形时,需要安装对应python的解释器以及IDE,我安装的是pycharm,在安装完pycharm后,在pycharm安装相应库的模块,绘图可以引入turtle模块 ...
- python中的turtle库(图形绘画库)
turtle绘图的基础知识:1. 画布(canvas) 画布就是turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置. 设置画布大小 turtle.screensize(canvwidt ...
- turtle库的学习笔记
(1)turtle使用pen来绘制图形 pendown() 放下画笔,移动到指定点后继续绘制 penup() 提起画笔,用于另起一个地方绘制时使用 pensize(width) 设置画笔线条 ...
- Python的dict字典结构操作方法学习笔记
Python的dict字典结构操作方法学习笔记 这篇文章主要介绍了Python的dict字典结构操作方法学习笔记本,字典的操作是Python入门学习中的基础知识,需要的朋友可以参考下 一.字典的基本方 ...
- Python中使用第三方库xlrd来写入Excel文件示例
Python中使用第三方库xlrd来写入Excel文件示例 这一篇文章就来介绍下,如何来写Excel,写Excel我们需要使用第三方库xlwt,和xlrd一样,xlrd表示read xls,xlwt表 ...
- 【归纳】正则表达式及Python中的正则库
正则表达式 正则表达式30分钟入门教程 runoob正则式教程 正则表达式练习题集(附答案) 元字符\b代表单词的分界处,在英文中指空格,标点符号或换行 例子:\bhi\b可以用来匹配hi这个单词,且 ...
随机推荐
- Re-enable extensions not coming from Chrome Web Store on Chrome v35+ (with enhanced security)
1. Add the --enable-easy-off-store-extension-install flag when you start chrome (create shortcut, ed ...
- 原子动作检测 A Better Baseline for AVA
本文将Faster-RCNN用在了I3D的feature map上,用于视频中多人多动作的检测 challege比赛第二名的整体方法是将Faster-RCNN作用在I3Dfeature上.训练时,以标 ...
- shaderlab UV动画所需的变量声明
优化资源.美术需要迫使自己的顶点shader能够进行TRANSFORM_TEX运算,进行该运算的前提是需要声明一个 _MainTex_ST 变量,类型为float4即可. 此时就可以使用unity c ...
- Codeforces 1110D. Jongmah 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1110D.html 题意 给定 n 个数,每一个数都是在 [1,m] 里的整数. 从中取出形如 {x,x,x ...
- ASP.NET Core 3.0预览版体验
目前.NET Core 3.0的版本为.NET Core 3.0 Preview 3,对应ASP.NET Core 3.0 Preview 3. ASP.NET Core 3.0 之后将不再支持.NE ...
- angularJs实现数据双向绑定的原理
angular1.x在指定的事件触发时(比如dom事件,xhr响应事件,浏览器定位变更事件,定时器事件),通过脏值检测的方式比对数据是否有变更,来决定是否更新视图. angular2使用了zone.j ...
- 洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)
LOJ 洛谷 考场上都拍上了,8:50才发现我读错了题=-= 两天都读错题...醉惹... \(Solution1\) 先求一遍前缀异或和. 假设左端点是\(i\),那么我们要在\([i,n]\)中找 ...
- python一些语法糖用法
@修饰符 '@'符号用作函数修饰符是python2.4新增加的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.也就是说@A def f(): 是非法的. 只可以在模块或类定义层内对函数 ...
- php+ajax文件上传
php+ajax文件上传 html: <input id="user_real_name" class="input_show" type="t ...
- CSS3 三次贝塞尔曲线(cubic-bezier)
例子:transition:all 1s cubic-bezier(.21,.2,.65,.1) 最近在看animation模块,其中animation-timing-function 和 trans ...