本文将会为您介绍关于python--turtle库函数,学会这个库函数,会有很多让你意想不到的事情发生哦!

我也也会为你们,简单的编写几个代码,让你们看一下turtle函数的魅力

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

turtle绘图的基础知识:

1. 画布(canvas)

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

设置画布大小

turtle.screensize(canvwidth=None, canvheight=None, bg=None),参数分别为画布的宽(单位像素), 高, 背景颜色。

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

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

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)

2. 画笔

2.1 画笔的状态

在画布上,默认有一个坐标原点为画布中心的坐标轴,坐标原点上有一只面朝x轴正方向小乌龟。这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中,就是使用位置方向描述小乌龟(画笔)的状态。

2.2 画笔的属性

画笔(画笔的属性,颜色、画线的宽度等)

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

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

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

2.3 绘图命令

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

(1)    画笔运动命令

命令

说明

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)

绘制一个指定直径和颜色的圆点

(2)     画笔控制命令

命令

说明

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形状

(3)    全局控制命令

命令

说明

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参数也是可选项

(4)    其他命令

命令

说明

turtle.mainloop()或turtle.done()

启动事件循环 -调用Tkinter的mainloop函数。

必须是乌龟图形程序中的最后一个语句。

turtle.mode(mode=None)

设置乌龟模式(“standard”,“logo”或“world”)并执行重置。如果没有给出模式,则返回当前模式。

模式

初始龟标题

正角度

standard

向右(东)

逆时针

logo

向上(北)

顺时针

turtle.delay(delay=None)

设置或返回以毫秒为单位的绘图延迟。

turtle.begin_poly()

开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。

turtle.end_poly()

停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。

turtle.get_poly()

返回最后记录的多边形。

3. 命令详解

3.1 turtle.circle(radius, extent=None, steps=None)

描述:以给定半径画圆

参数:

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

extent(弧度) (optional);

steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)。

////////////////////////////////////////////////////////////////////////////////////////////

将会写几个代码:

1:爱心树:

# drawtree.py
 
from turtle import Turtle, mainloop
import turtle 
 
def tree(plist, l, a, f):
    """ plist is list of pens
    l is length of branch
    a is half of the angle between 2 branches
    f is factor by which branch is shortened
    from level to level."""
    if l > 5: #
        lst = []
        for p in plist:
            p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
            q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
            p.left(a) #Turn turtle left by angle units
            q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
            lst.append(p)#将元素增加到列表的最后
            lst.append(q)
        tree(lst, l*f, a, f)
   
            
 
def main():
    p = Turtle()
    p.color("red")
    p.pensize(4)
    #p.setundobuffer(None)
    p.hideturtle() #Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing,
    #because hiding the turtle speeds up the drawing observably.
    #p.speed(10)
   # p.getscreen().tracer(1,0)#Returtle.write("You are the secret of my heart")
    p.speed(4)
    #TurtleScreen methods can then be called for that object.
    p.left(90)# Turn turtle left by angle units. direction 调整画笔
 
    p.penup() #Pull the pen up – no drawing when moving.
    p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
    p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
    #否则turtle一移动就会自动的把线画出来
 
    #t = tree([p], 200, 65, 0.6375)
    t = tree([p], 200, 65, 0.6375)
    turtle.down()
    turtle.goto(-80,70)

    turtle.write("You are the secret of my heart")
    turtle. done ()

main()

2:表白专用:

import turtle
import time
def liujia():
    for i in range (200):
        turtle.right(1)
        turtle.forward(1)
turtle.color('red','pink')
turtle.pensize(2)
turtle.speed(10)
turtle.goto(0,0)
turtle.begin_fill()
turtle.left(140)
turtle.forward(112)
liujia()
turtle.left(120)
liujia()
turtle.forward(112)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.goto(-80,122.7)
turtle.left(70)
turtle.down()
turtle.forward(50)
turtle.left(140)
turtle.forward(50)
turtle.right(140)
turtle.forward(50)
turtle.left(140)
turtle.forward(50)
turtle.up()
turtle.goto(40,122.7)
turtle.right(160)
turtle.down()
turtle.forward(50)
turtle.up()
turtle.left(90)
turtle.down()
turtle.forward(25)
turtle.up()
for i in range (20):
    turtle.right(7.8)
    turtle.forward(0.3)
turtle.forward(8)
turtle.up()
turtle.goto(100,-10)
turtle.write("You are the secret of my heart")

turtle.done()

3:给你一朵小花花:

import turtle  
  
# 设置初始位置  
turtle.penup()  
turtle.left(90)  
turtle.fd(200)  
turtle.pendown()  
turtle.right(90)  
  
# 花蕊  
turtle.fillcolor("red")  
turtle.begin_fill()  
turtle.circle(10,180)  
turtle.circle(25,110)  
turtle.left(50)  
turtle.circle(60,45)  
turtle.circle(20,170)  
turtle.right(24)  
turtle.fd(30)  
turtle.left(10)  
turtle.circle(30,110)  
turtle.fd(20)  
turtle.left(40)  
turtle.circle(90,70)  
turtle.circle(30,150)  
turtle.right(30)  
turtle.fd(15)  
turtle.circle(80,90)  
turtle.left(15)  
turtle.fd(45)  
turtle.right(165)  
turtle.fd(20)  
turtle.left(155)  
turtle.circle(150,80)  
turtle.left(50)  
turtle.circle(150,90)  
turtle.end_fill()  
   
# 花瓣1  
turtle.left(150)  
turtle.circle(-90,70)  
turtle.left(20)  
turtle.circle(75,105)  
turtle.setheading(60)  
turtle.circle(80,98)  
turtle.circle(-90,40)  
  
# 花瓣2  
turtle.left(180)  
turtle.circle(90,40)  
turtle.circle(-80,98)  
turtle.setheading(-83)  
  
# 叶子1  
turtle.fd(30)  
turtle.left(90)  
turtle.fd(25)  
turtle.left(45)  
turtle.fillcolor("green")  
turtle.begin_fill()  
turtle.circle(-80,90)  
turtle.right(90)  
turtle.circle(-80,90)  
turtle.end_fill()  
   
turtle.right(135)  
turtle.fd(60)  
turtle.left(180)  
turtle.fd(85)  
turtle.left(90)  
turtle.fd(80)  
   
# 叶子2  
turtle.right(90)  
turtle.right(45)  
turtle.fillcolor("green")  
turtle.begin_fill()  
turtle.circle(80,90)  
turtle.left(90)  
turtle.circle(80,90)  
turtle.end_fill()  
   
turtle.left(135)  
turtle.fd(60)  
turtle.left(180)  
turtle.fd(60)  
turtle.right(90)

turtle.circle(200,60)

我只是写了这几个,以后有时间我还会陆续更新的!

努力是因为不想让在乎的人失望! 
博主整理不易,如果喜欢推荐关注一下博主哦!

博主喜欢广交好友下面是我的联系方式: 
QQ:1263030049 
加好友前请注明原因谢谢!

最新文章

  1. javascript中的闭包
  2. Delphi DateUtils时间单元
  3. 修改linux文件权限命令:chmod(转)
  4. Android studio下载依赖包很慢
  5. Shell数组例子
  6. 完美卸载SQL Server 2008的方案
  7. My Vim配置
  8. java类初始化,使用构造方法
  9. Hadoop技巧(04):简易处理solr date 时区问题
  10. WCF 内置跟踪日志
  11. 微信小程序开发笔记
  12. mysql 索引中的USING BTREE 的意义
  13. QMap迭代器
  14. 利用spring boot+vue做的一个博客项目
  15. 关于table动态添加数据 单元格合并 数组合并
  16. Android studio导入framework编译的classes.jar包
  17. javaScript高级教程(八)-----正则表达式温故知新
  18. 在函数内部定义的函数 this 指向 undefined
  19. jqgrid单元格合并
  20. Hive学习之路 (十二)Hive SQL练习之影评案例

热门文章

  1. JTA 分布式事务
  2. LOVO学习之思维导图和文档编辑器
  3. python 定时服务模块
  4. Mybatis 系列10
  5. ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?
  6. Awesome Big Data List
  7. python3学习笔记4---引用http://python3-cookbook.readthedocs.io/zh_CN/latest/
  8. 如何优雅的关闭Java线程池
  9. 深入理解SpringAOP之代理对象
  10. DevOps之二 Maven的安装与配置