赞
踩
在海龟作图中,我们可以编写指令让一个虚拟的(想象中的)海龟在屏幕上来回移动。这个海龟带着一只钢笔,我们可以让海龟无论移动到哪都使用这只钢笔来绘制线条。通过编写代码,以各种很酷的模式移动海龟,我们可以绘制出令人惊奇的图片。使用海龟作图,我们不仅能够只用几行代码就创建出令人印象深刻的视觉效果,而且还可以跟随海龟看看每行代码如何影响到它的移动。这能够帮助我们理解代码的逻辑。所以海龟作图也常被用作新手学习 Python 的一种方式。
“小海龟”turtle是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
海龟绘图(Turtle Graphics)是python2.6版本中后引入的一个简单的绘图工具,出现在1966年的Logo计算机语言。
海龟绘图(turtle库)是python的内部模块,使用前导入即可 import turtle
海龟有3个关键属性:方向、位置和画笔(笔的属性有色彩、宽度和开/关状态)
小北安装的Anaconda3,使用的是Jupyter Notebook,建议友友们安装社区版,完全免费。
画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置(None,自己设置数值)
turtle.screensize(canvwidth=None,canvheight=None,bg=None)
- turtle.screensize(800,600,"green")
- turtle.screensize() #默认返回(400,300,"white")
turtle.setup(width=0.5,height=0.75,startx=None,starty=None)
参数:
width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
(startx, starty): 这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心
eg:
- turtle.setup(width=800,height=0.8)
- turtle.setup(width=800,height=600,startx=100,starty=200)
效果是一样的
使用 turtle.Pen() 来创建画笔对象
- import turtle
- turtle.Pen()
- turtle.mainloop()#画面保持
代码执行后就建立了画布,同时屏幕中间可以看见箭头(arrow),即所谓的海龟
在海龟绘图中,海龟的起点即画布中央为 (0,0),移动单位是像素 (pixel)
描述海龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中, 就是使用位置方向描述海龟(画笔)的状态。
绘图命令主要分为三种:
操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令。
(1)画笔运动命令:
(2)画笔控制命令:
(3) 全局控制命令:
部分命令解释和补充:
- turtle.circle(radius,extent=None,steps=None)#radius=半径,正负有别,extent代表弧度,steps内切正多边形的边数
- turtle.setheading(angle)#转变方向,angle指绝对角度,只改变方向不行进
- turtle.done() #停止画笔绘制,但是不关闭窗口
在实践中学习!(“时间是检验真理的唯一标准”)
- import turtle
-
- turtle.setup(800, 400) #调整画板大小
- turtle.up() #提起笔
- turtle.goto(-50, 50) #调整画板起始点
- turtle.down() #放下笔
- turtle.color("red") #画笔颜色
- turtle.speed(1) #控制绘画速度
-
- # 画图过程
- turtle.forward(100)
- turtle.right(90)
- turtle.forward(100)
- turtle.right(90)
- turtle.forward(100)
- turtle.right(90)
- turtle.forward(100)
- # 保持画面
- turtle.mainloop()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- import turtle as t
- import time
-
- t.color("red", "yellow")
- t.speed(10)
- t.begin_fill()
- for _ in range(25):
- t.forward(200)
- t.left(165)
-
- t.end_fill()
- time.sleep(5)
- import turtle
- from turtle import *
- # 相比于 import turtle as * 这样的区别
-
- # 绘制过程使用递归算法,后续解释
- def tree(branchLen, t):
- if branchLen > 5:
- t.forward(branchLen)
- t.right(20)
- tree(branchLen - 15, t)
- t.left(40)
- tree(branchLen - 10, t)
- t.right(20)
- t.backward(branchLen)
-
-
- t = Turtle()
- myWin = t.getscreen() # 获取屏幕
- t.speed(0)
- t.hideturtle() # 隐藏箭头
- t.left(90) # 掉转绘制方向朝上
- t.up() # 画笔离开
- t.backward(300) # 将画笔移动到屏幕下方
- t.down() # 画笔落下
- t.color("green") #调整画笔颜色
- tree(110, t) #调用绘制函数
- myWin.exitonclick() # 点击关闭屏幕
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
这种递归方法总体难度还是比较大,大家可以通过Debug的方法来初步进行,了解递归的运行方式。
- import turtle
- import turtle as t
-
- a = 200
- t.speed(0)
- t.color("green")
- for x in range(1, 37):
- t.forward(a)
- t.left(90)
- t.forward(a)
- t.left(90)
- t.forward(a)
- t.left(90)
- t.forward(a)
- t.left(100)
- t.mainloop()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- import turtle
-
- t = turtle.Pen()
- t.pensize(5)
- colorValue = 1
- colorStep = colorValue / 36
- t.speed(0)
- for x in range(1, 37):
- colorValue -= colorStep
- t.color(0.5, 1, colorValue)
- t.forward(200)
- t.left(90)
- t.forward(200)
- t.left(90)
- t.forward(200)
- t.left(90)
- t.forward(200)
- t.left(100)
- turtle.mainloop()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- import turtle
-
- t=turtle.Pen()
- t.hideturtle()
- t.pensize(5) #画笔粗细
- t.up()
- t.goto(-100,50)#画笔移动到画布较为中心的位置
- t.down()
- for i in range(1,6):
- t.forward(200)
- t.right(144)
-
- turtle.done() #类似Mainloop,一个循环函数,保证画布持续存在
- import turtle
- from turtle import *
-
- pen()
- pencolor("red")
- speed(0)
-
- for i in range(20):
- forward(i*20)
- right(144)
-
- done()
- import turtle
- from turtle import *
-
- pensize(5)
- color("yellow", "red")
- begin_fill() # 准备填充
- for i in range(5):
- forward(200)
- right(144)
-
- end_fill() # 填充完毕
-
- up()
- goto(-200, -100) # 将海龟移动到此位置
- pencolor("violet") # 颜色选用紫罗兰
- write("WQ", False, "left", font=('Arial', 40, 'normal')) # 在当前位置输入字符“WQ”,光标回到左下角,font中三项分别指定字型、字号和形式
- done()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
命令详解
turtle.circle(radius, extent=None, steps=None)
描述: 以给定半径画圆
参数:
radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)
举例:
circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆
- #PythonDraw.py
- import turtle #海龟库是turtle绘图的pyhon实现
- turtle.setup(650,350,200,200)#turtle.setup(with,height,startx,starty) 设置窗体的大小及位置
- turtle.penup()
- turtle.fd(-250)
- turtle.pendown()
- turtle.pensize(25)
- turtle.pencolor("purple")
- turtle.seth(-40)
- for i in range(4):
- turtle.circle(40,80)
- turtle.circle(-40,80)
- turtle.circle(40,80/2)
- turtle.fd(40)
- turtle.circle(16,180)
- turtle.fd(40*2/3)
- turtle.done()
-
- '''
- turtle.goto(x,y) 海龟从摸个位置去到达(x,y)的坐标位置
- turtle.fd(d) 向海龟的正前方向运行
- turtle.bk(d) 向海龟的反方向运行
- turtle.circle(r,angle) 以海龟的当前位置,左侧的某一个点为圆心,进行曲线运行
- turtle.seth(angle) 改变海龟的行径角度,只是改变行径的方向(绝对角度)
- turtle.left(angle) 当前的海龟向左或者向右改变运行的方向
- turtle.right(angle)
- turtle.colormode(mode) 颜色模块
- -1.0: RGB小数值模块
- -255:RGB整数值模块
- import <库名>
- <库名>.<函数>(<函数参数>)
- from <库名> import <函数名>
- from <库名> import*
- <函数名>(<函数参数>)
- import <库名> as <库别名>
- <库别名>.<函数> (<函数参数>)
- turtle.penup() 抬起画笔,海龟在飞行 别名 turtle.pu()
- turtle.pendown() 画笔落下,海龟爬行 turtle.pd()
- turtle.pensize(width) 画笔宽度,海龟的腰围 turtle.width(width)
- turtle.pencolot(colot) color为颜色字符串或r,g,b值
- for <变量> in range (<函数名>)
- <被循环执行的语句>
- -<变量>表示每次循环的计数,0到<次数>1
- range()函数
- -range(N)
- 产生0到N-1的整数序列,共N个
- -range(M,N)
- 产生M到N-1的整数序列,共N-M个
- '''
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- import turtle
-
-
- def koch(size, n):
- if n == 0:
- turtle.fd(size)
- else:
- for angle in [0, 60, -120, 60]:
- turtle.left(angle)
- koch(size / 3, n - 1)
-
-
- def main():
- turtle.setup(800, 400)
- turtle.up()
- turtle.goto(-300, -50)
- turtle.down()
- turtle.pensize(2)
- koch(600, 3)
- turtle.hideturtle()
- turtle.done()
-
-
- main()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
科赫雪花相当于画了三次科赫曲线
- #绘制科赫雪花
- import turtle
- def koch(size,n):
- if n == 0:
- turtle.fd(size)
- else:
- for angle in [0,60,-120,60]:
- turtle.left(angle)
- koch(size/3,n-1)
- def main():
- turtle.setup(600,600)
- turtle.penup()
- turtle.goto(-200,100)
- turtle.pendown()
- turtle.pensize(2)
- level = 3 #3阶科赫雪花,阶数
- koch(400,level)
- turtle.right(120)
- koch(400,level)
- turtle.right(120)
- koch(400,level)
- turtle.hideturtle()
- turtle.done()
- main()
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在本篇博客中,小北尝试使用海龟绘图模块(类)绘制一些基础图形,学习了一些Python编程的基础知识,当然还有很多都没涉及到,友友们可以自行学习,蟹蟹~
- import turtle as t
- # 绘制小猪佩奇
- # =======================================
-
- t.pensize(4)
- t.hideturtle()
- t.colormode(255)
- t.color((255, 155, 192), "pink")
- t.setup(840, 500)
- t.speed(10)
-
- # 鼻子
- t.pu()
- t.goto(-100,100)
- t.pd()
- t.seth(-30)
- t.begin_fill()
- a = 0.4
- for i in range(120):
- if 0 <= i < 30 or 60 <= i < 90:
- a = a+0.08
- t.lt(3) # 向左转3度
- t.fd(a) # 向前走a的步长
- else:
- a = a-0.08
- t.lt(3)
- t.fd(a)
- t.end_fill()
-
- t.pu()
- t.seth(90)
- t.fd(25)
- t.seth(0)
- t.fd(10)
- t.pd()
- t.pencolor(255, 155, 192)
- t.seth(10)
- t.begin_fill()
- t.circle(5)
- t.color(160, 82, 45)
- t.end_fill()
-
- t.pu()
- t.seth(0)
- t.fd(20)
- t.pd()
- t.pencolor(255, 155, 192)
- t.seth(10)
- t.begin_fill()
- t.circle(5)
- t.color(160, 82, 45)
- t.end_fill()
-
- # 头
- t.color((255, 155, 192), "pink")
- t.pu()
- t.seth(90)
- t.fd(41)
- t.seth(0)
- t.fd(0)
- t.pd()
- t.begin_fill()
- t.seth(180)
- t.circle(300, -30)
- t.circle(100, -60)
- t.circle(80, -100)
- t.circle(150, -20)
- t.circle(60, -95)
- t.seth(161)
- t.circle(-300, 15)
- t.pu()
- t.goto(-100, 100)
- t.pd()
- t.seth(-30)
- a = 0.4
- for i in range(60):
- if 0 <= i < 30 or 60 <= i <90:
- a = a+0.08
- t.lt(3) # 向左转3度
- t.fd(a) # 向前走a的步长
- else:
- a = a-0.08
- t.lt(3)
- t.fd(a)
- t.end_fill()
-
- # 耳朵
- t.color((255, 155, 192), "pink")
- t.pu()
- t.seth(90)
- t.fd(-7)
- t.seth(0)
- t.fd(70)
- t.pd()
- t.begin_fill()
- t.seth(100)
- t.circle(-50, 50)
- t.circle(-10, 120)
- t.circle(-50, 54)
- t.end_fill()
-
- t.pu()
- t.seth(90)
- t.fd(-12)
- t.seth(0)
- t.fd(30)
- t.pd()
- t.begin_fill()
- t.seth(100)
- t.circle(-50, 50)
- t.circle(-10, 120)
- t.circle(-50, 56)
- t.end_fill()
-
- #眼睛
- t.color((255, 155, 192), "white")
- t.pu()
- t.seth(90)
- t.fd(-20)
- t.seth(0)
- t.fd(-95)
- t.pd()
- t.begin_fill()
- t.circle(15)
- t.end_fill()
-
- t.color("black")
- t.pu()
- t.seth(90)
- t.fd(12)
- t.seth(0)
- t.fd(-3)
- t.pd()
- t.begin_fill()
- t.circle(3)
- t.end_fill()
-
- t.color((255, 155, 192), "white")
- t.pu()
- t.seth(90)
- t.fd(-25)
- t.seth(0)
- t.fd(40)
- t.pd()
- t.begin_fill()
- t.circle(15)
- t.end_fill()
-
- t.color("black")
- t.pu()
- t.seth(90)
- t.fd(12)
- t.seth(0)
- t.fd(-3)
- t.pd()
- t.begin_fill()
- t.circle(3)
- t.end_fill()
-
- # 腮
- t.color((255, 155, 192))
- t.pu()
- t.seth(90)
- t.fd(-95)
- t.seth(0)
- t.fd(65)
- t.pd()
- t.begin_fill()
- t.circle(30)
- t.end_fill()
-
- # 嘴
- t.color(239, 69, 19)
- t.pu()
- t.seth(90)
- t.fd(15)
- t.seth(0)
- t.fd(-100)
- t.pd()
- t.seth(-80)
- t.circle(30, 40)
- t.circle(40, 80)
-
- # 身体
- t.color("red", (255, 99, 71))
- t.pu()
- t.seth(90)
- t.fd(-20)
- t.seth(0)
- t.fd(-78)
- t.pd()
- t.begin_fill()
- t.seth(-130)
- t.circle(100,10)
- t.circle(300,30)
- t.seth(0)
- t.fd(230)
- t.seth(90)
- t.circle(300,30)
- t.circle(100,3)
- t.color((255,155,192),(255,100,100))
- t.seth(-135)
- t.circle(-80,63)
- t.circle(-150,24)
- t.end_fill()
-
- # 手
- t.color((255,155,192))
- t.pu()
- t.seth(90)
- t.fd(-40)
- t.seth(0)
- t.fd(-27)
- t.pd()
- t.seth(-160)
- t.circle(300,15)
- t.pu()
- t.seth(90)
- t.fd(15)
- t.seth(0)
- t.fd(0)
- t.pd()
- t.seth(-10)
- t.circle(-20,90)
-
- t.pu()
- t.seth(90)
- t.fd(30)
- t.seth(0)
- t.fd(237)
- t.pd()
- t.seth(-20)
- t.circle(-300,15)
- t.pu()
- t.seth(90)
- t.fd(20)
- t.seth(0)
- t.fd(0)
- t.pd()
- t.seth(-170)
- t.circle(20,90)
-
- # 脚
- t.pensize(10)
- t.color((240,128,128))
- t.pu()
- t.seth(90)
- t.fd(-75)
- t.seth(0)
- t.fd(-180)
- t.pd()
- t.seth(-90)
- t.fd(40)
- t.seth(-180)
- t.color("black")
- t.pensize(15)
- t.fd(20)
-
- t.pensize(10)
- t.color((240, 128, 128))
- t.pu()
- t.seth(90)
- t.fd(40)
- t.seth(0)
- t.fd(90)
- t.pd()
- t.seth(-90)
- t.fd(40)
- t.seth(-180)
- t.color("black")
- t.pensize(15)
- t.fd(20)
-
- # 尾巴
- t.pensize(4)
- t.color((255, 155, 192))
- t.pu()
- t.seth(90)
- t.fd(70)
- t.seth(0)
- t.fd(95)
- t.pd()
- t.seth(0)
- t.circle(70, 20)
- t.circle(10, 330)
- t.circle(70, 30)
- t.done()
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Python——turtle(海龟绘图)小猪佩奇
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。