1基础概念

1.1 画布(canvas)

画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置。

常用的画布方法有两个:screensize()和setup()。

(1)turtle.screensize(canvwidth=None, canvheight=None, bg=None)

参数分别为画布的宽(单位像素), 高, 背景颜色

如:

turtle.screensize(800, 600, "green")

turtle.screensize() #返回默认大小(400, 300)

(2)turtle.setup(width=0.5, height=0.75, startx=None, starty=None)

参数:

  • width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
  • (startx, starty): 这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心

如:

turtle.setup(width=0.6, height=0.6)

turtle.setup(width=800, height=800, startx=100, starty=100)

1.2 画笔

在画布上,默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝x轴正方向小乌龟。

这里我们描述小乌龟时使用了两个词语:标原点(位置),面朝x轴正方向(方向),turtle绘图中, 就是使用位置方向描述小乌龟(画笔)的状态

(1)画笔的属性

画笔有颜色、画线的宽度等属性。

1)  turtle.pensize() :设置画笔的宽度;

2)  turtle.pencolor() :没有参数传入返回当前画笔颜色;传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组。

>>> pencolor('brown')

>>> tup = (0.2, 0.8, 0.55)

>>> pencolor(tup)

>>> pencolor()

'#33cc8c'

3)  turtle.speed(speed) :设置画笔移动速度,画笔绘制的速度范围[0,10]整数, 数字越大越快

(2)绘图命令

操纵海龟绘图有着许多的命令,这些命令可以划分为3种:运动命令,画笔控制命令和全局控制命令

画笔运动命令:

命令

说明

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.speed(speed)

画笔绘制的速度范围[0,10]整数

turtle.circle()

画圆,半径为正(),表示圆心在画笔的左边(右边)画圆

画笔控制命令:

命令

说明

turtle.pensize(width)

绘制图形时的宽度

turtle.pencolor()

画笔颜色

turtle.fillcolor(colorstring)

绘制图形的填充颜色

turtle.color(color1, color2)

同时设置pencolor=color1, fillcolor=color2

turtle.filling()

返回当前是否在填充状态

turtle.begin_fill()

准备开始填充图形

turtle.end_fill()

填充完成;

turtle.hideturtle()

隐藏箭头显示;

turtle.showturtle()

hideturtle()函数对应

全局控制命令

命令

说明

turtle.clear()

清空turtle窗口,但是turtle的位置和状态不会改变

turtle.reset()

清空窗口,重置turtle状态为起始状态

turtle.undo()

撤销上一个turtle动作

turtle.isvisible()

返回当前turtle是否可见

stamp()

复制当前图形

turtle.write(s[,font=("font-name",font_size,"font_type")])

写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项, font的参数也是可选项

2、绘图展示

2.1猴子的头(未完成)

代码如下:

import turtle

turtle.screensize(800, 600, "green")

turtle.pencolor("brown")

turtle.pensize(5)

turtle.fillcolor("brown")

turtle.begin_fill()

turtle.circle(90)

turtle.circle(90,105)

turtle.right(90)

turtle.circle(40,-360)

turtle.end_fill()

turtle.left(90)

turtle.begin_fill()

turtle.circle(90,145)

turtle.right(90)

turtle.left(10)

turtle.circle(40,-360)

turtle.right(150)

turtle.forward(75)

turtle.right(90)

turtle.forward(50)

turtle.right(90)

turtle.forward(10)

turtle.end_fill()

turtle.fillcolor("red")

turtle.begin_fill()

turtle.pencolor("black")

turtle.penup

turtle.left(180)

turtle.pencolor("red")

turtle.forward(20)

turtle.right(180)

turtle.pencolor("black")

turtle.forward(10)

turtle.pendown

turtle.circle(10)

turtle.end_fill()

turtle.right(-90)

turtle.forward(50)

turtle.circle(20,105)

turtle.left(40)

turtle.circle(20,55)

turtle.circle(20,-55)

turtle.right(40)

turtle.circle(20,-105)

turtle.circle(-20,105)

turtle.right(40)

turtle.circle(-20,55)

turtle.pencolor("brown")

turtle.forward(40)

turtle.right(-20)

turtle.forward(20)

turtle.left(90)

turtle.forward(20)

turtle.pencolor("black")

turtle.fillcolor("white")

turtle.begin_fill()

turtle.circle(-20)

turtle.end_fill()

turtle.fillcolor("black")

turtle.begin_fill()

turtle.circle(-15)

turtle.end_fill()

turtle.right(180)

turtle.pencolor("brown")

turtle.forward(80)

turtle.pencolor("black")

turtle.fillcolor("white")

turtle.begin_fill()

turtle.circle(20)

turtle.end_fill()

turtle.fillcolor("black")

turtle.begin_fill()

turtle.circle(15)

turtle.end_fill()

(以后会将这个猴子完善,探索中)

2.2太阳花

代码如下:

from turtle import *

color('red','yellow')

begin_fill()

while True:

forward(200)

left(170)

if abs(pos()) <1:

break

end_fill()

done()

2.3五角星

代码如下:

import turtle

turtle.pensize(10)

turtle.pencolor("red")

turtle.fillcolor("yellow")

turtle.begin_fill()

for i in range(5):

turtle.forward(120)

turtle.right(144)

turtle.forward(120)

turtle.left(72)

turtle.end_fill()

turtle.hideturtle()

turtle.done()

Python——我所学习的turtle函数库的更多相关文章

  1. 从Theano到Lasagne:基于Python的深度学习的框架和库

    从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...

  2. 重温JSP学习笔记--El函数库

    EL函数库(由JSTL提供的) * 导入标签库:<%@ tablib prefix="fn" uri="http://java.sun.com/jsp/jstl/f ...

  3. shell学习——关于shell函数库的使用

    shell函数库的理解: 个人理解,shell函数库实质为一个脚本,脚本内包含了多个函数(函数具有普遍适用性). shell函数库的调用: 通过  . /path/lib/file.lib 或者 so ...

  4. python菜鸟教程学习9:函数

    函数的定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.python提供了很多内建函数,但我们依然可以自己创建函数,叫做用户自定义函数. 自定义函数 你可以定义一个由自己想要功能 ...

  5. python 操作exls学习之路1-openpyxl库学习

    这篇要讲到的就是如何利用Python与openpyxl结合来处理xlsx表格数据.Python处理表格的库有很多,这里的openpyxl就是其中之一,但是它是处理excel2007/2010的格式,也 ...

  6. python装饰器学习详解-函数部分

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 最近阅读<流畅的python>看见其用函数写装饰器部分写的很好,想写一些自己的读书笔记. ...

  7. Python全栈学习:匿名函数使用规范

    匿名函数,当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x) ...

  8. python核心编程学习记录之函数与函数式编程

    @func function 意思是func(function) @func(a) function 意思是func(a)这是个函数对象,在去调用function函数 如果要传额外的值,只传值用*tu ...

  9. 菜鸟Python学习笔记第一天:关于一些函数库的使用

    2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...

随机推荐

  1. Java学习--泛型

    个人理解,所谓的泛型就是将数据类型像参数(称为类型参数或者泛型参数)一样传入类,接口或者方法中,这个类型参数可以当作普通的数据类型,进行变量的声明(成员变量,局部变量(包括方法参数)),指明返回值类型 ...

  2. Oracle使用学习笔记(二)_Sql语句

    一.Sql语句的分类 数据操作语言,简称DML(data manipulation language),如增加,删除,修改,查询数据等 数据定义语言,简称DDL(data defination lan ...

  3. c# WinForm 文本输入对话框

    这种功能为什么不向MessageBox一样自带,还得自己写~~ 代码: public InputBox(string label) { InitializeComponent(); label1.Te ...

  4. Web开发敏捷之道应用Rails 进行Web开发(原书第4版)遇到的问题

    第11章:建立一个基于Ajax的购物车 原书是这样的: 问题:create.js.rjs rails4算是彻底抛弃rjs了,所以按照书上使用以下代码,是一点作用没有用的. 这里介绍一种方法: 1.在a ...

  5. 【C++】纯C++实现http打开网页下载内容的功能

    #include "stdafx.h" #include <windows.h> #include <iostream> #include "Wi ...

  6. Maven下Spring + SpringMvc + Hibernate4 配置实例

    1. 开发环境 IDEA 2. 在pom.xml中配置引用相关的包. <properties> <junit.version>4.10</junit.version> ...

  7. js,jquery备忘录

    1.var s = str.charCodeAt();转ASCII码 2.String.fromCharCode(65);转字母 3.es6 ... (扩展运算符),将一个数组转化成由逗号分割的队列. ...

  8. Java框架spring Boot学习笔记(三):Controller的使用

    Controller注解介绍 @Controller:处理http请求 @RestController: Spirng4之后新加的注解,其实是一个组合注解等同于@ResponseBody和@Contr ...

  9. Jmeter之集合点(Synchronizing timer 同步定时器)

    1.集合点介绍 LR中集合点可以设置多个虚拟用户等待到一个点,同时触发一个事务,以达到模拟真实环境下多个用户同时操作,实现性能测试的最终目的.jmeter中使用Synchronizing Timer实 ...

  10. 【python路飞】编码 ascii码(256位 =1个字节)美国;unicode(万国码)中文 一共9万个 用4个字节表示这9万个子 17位就能表示

    8位一个字节  1024字节 1KB   1024KB 1MB ASCII码不能包含中文.创建了unicode,一个中文4个字节.UTF-8一个中文3个.GBK中国人用的只包含中文2个字节 升级 Un ...