一、基础概念

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

turtle在python中什么意思_python中关于turtle库的学习笔记相关推荐

  1. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  2. python中定义数据结构_Python中的数据结构—简介

    python中定义数据结构 You have multiples algorithms, the steps of which require fetching the smallest value ...

  3. 第86讲:Scala中For表达式的生成器、定义和过滤器学习笔记

    第86讲:Scala中For表达式的生成器.定义和过滤器学习笔记 for表达式的生成器定义和过滤和角度讲解for的内容 for表达式中生成器定义和过滤是非常重要的内容. object ForInact ...

  4. Python第三方库pygame学习笔记(一)

    Pygame Python最经典的2D游戏开发第三方库,也支持3D游戏开发 Pygame适合用于游戏逻辑验证.游戏入门及系统演示验证 Pygame是一种游戏开发引擎,基本逻辑具有参考价值 pygame ...

  5. python海龟绘图颜色_python中利用turtle(海龟)绘图制作龟兔赛跑动画——仅供学习...

    python中利用turtle(海龟)绘图制作龟兔赛跑动画--仅供学习哦 首先,欢迎大家来我的博客当中浏览,由于我和我的可爱现在还都个初学者,所作的东西还不够完善,之前在学习计算机的各种语言的过程中, ...

  6. python海龟绘图颜色_Python中的高级turtle(海龟)作图(续)

    四.填色 color 函数有三个参数.第一个参数指定有多少红色,第二个指定有多少绿色,第三个指定有多少蓝色.比如,要得到车子的亮红色,我们用 color(1,0,0),也就是让海龟用百分之百的红色画笔 ...

  7. python决策树 多分类_Python中的决策树分类:您需要了解的一切

    python决策树 多分类 什么是决策树? (What is Decision Tree?) A decision tree is a decision support tool that uses ...

  8. python基础知识测试题_Python中的单元测试—基础知识

    python基础知识测试题 Unit testing is the number one skill which separates people who just finished their de ...

  9. python数据库模糊查询_python中数据库like模糊查询方式

    python中数据库like模糊查询方式 在Python中%是一个格式化字符,所以如果需要使用%则需要写成%%. 将在Python中执行的sql语句改为: sql = "SELECT * F ...

最新文章

  1. liunx系统mysql全量备份和增量备份
  2. 3w最简单led灯电路图_行业内幕揭秘:LED灯没有你想的那么简单!
  3. matlab去除坏点,图像处理之坏点校正及源码实现
  4. OpenMP入门教程(一)hello world
  5. Thymeleaf文档
  6. hdu-1166敌兵布阵(树状数组)
  7. 长沙理工大学第十二届ACM大赛-重现赛C 安卓图案解锁 (模拟)
  8. Windows遗产之RPC/DCOM:还在用吗,内部又有什么区别?
  9. 面试题3二维数组中的查找
  10. matlab 数据白化,数据白化
  11. [转]Android 上百实例源码分析以及开源分析
  12. learning and evaluating representations for deep one-class classification
  13. 初始C语言——成绩等级划分
  14. java 向word插入图片 调整图片位置
  15. go官网打不开,下载地址https://golang.google.cn/
  16. 【php图片上传在网页显示】
  17. 【新知实验室】——腾讯云TRTC实时音视频体验
  18. 20145204张亚军——web安全基础实践
  19. 基于labview的示波器自动控制软件
  20. 阿里P9架构师终于把毕生心血而成的分布式高可用算法笔记开源了

热门文章

  1. 李宏毅ML lecture-14 unsupervised Learning Word Embeddng
  2. Authing 客户故事|仙工智能
  3. Windows 的服务概述和网络端口要求
  4. 基于LSTM、RNN及滑动窗口CNN模型的股票价格预测
  5. 2018年高二上12月月考总结暨2018年度总结、2019年规划
  6. 使用Python进行图像缩放处理
  7. 在域控制器上(AD)搭建组织架构
  8. 根据根节点和子节点构造树形结构
  9. 一款基于VUE的动态化弹出层插件
  10. 【STM32】烧录器与程序下载