前言
在1966年,Seymour Papert和Wally Feurzig发明了一种专门给儿童学习编程的语言——LOGO语言,它的特色就是通过编程指挥一个小海龟(turtle)在屏幕上绘图。海龟绘图(Turtle Graphics)后来被移植到各种高级语言中,Python内置了turtle库,基本上100%复制了原始的Turtle Graphics的所有功能操作以及相关案例。
1.1. 画布(canvas)
画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置
1.2 设置画布大小
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 画笔的使用
使用 turtle.Pen() 来创建画笔对象
import turtle
turtle.Pen()
turtle.mainloop()#画面保持
2.3 绘图命令
操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令
(1)画笔运动命令:
3. 命令详解
3.1 turtle.circle(radius, extent=None, steps=None)
描述: 以给定半径画圆
参数:
radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)
举例:
circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆
4. 绘图举例
4.1 党旗
import turtle
turtle.fillcolor("red")
turtle.pencolor("red")
turtle.up()
turtle.goto(-300, 300)
turtle.down()
turtle.begin_fill()
for i in range(2):
turtle.forward(600)
turtle.right(90)
turtle.forward(400)
turtle.right(90)
turtle.end_fill()
turtle.fillcolor("yellow")
turtle.pencolor("yellow")
turtle.up()
turtle.goto(10, 220)
turtle.seth(225)
turtle.down()
turtle.begin_fill()
turtle.fd(125)
turtle.right(90)
turtle.fd(50)
turtle.right(90)
turtle.fd(100)
turtle.right(90)
turtle.circle(25, 90)
turtle.end_fill()
turtle.up()
turtle.goto(-40, 190)
turtle.seth(-45)
turtle.down()
turtle.begin_fill()
for i in range(2):
turtle.forward(200)
turtle.right(90)
turtle.forward(30)
turtle.right(90)
turtle.end_fill()
turtle.up()
turtle.goto(-100, 100)
turtle.seth(-50)
turtle.down()
turtle.begin_fill()
turtle.circle(100, 180)
turtle.fd(20)
turtle.right(157)
turtle.circle(-115, 190)
turtle.left(90)
turtle.fd(20)
turtle.right(90)
turtle.fd(20)
turtle.right(90)
turtle.fd(20)
turtle.left(80)
turtle.fd(30)
turtle.end_fill()
turtle.up()
turtle.goto(-90, 50)
turtle.down()
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.hideturtle() # 隐藏小海龟
# 维持面板
turtle.done()
4.2 草莓熊
import turtle as t # 设置背景颜色,窗口位置以及大小 t.colormode(255) # 颜色模式 t.speed(0) t.screensize(850, 760, "white") # 画布大小背景颜色 t.setup(width=850, height=760, startx=None, starty=None) # 绘图窗口的大小和起始坐标 # t.bgpic("di_800.gif") t.title("逆境清醒草莓熊!") # 设置绘图窗口的标题 t.resizemode('noresize') # 大小调整模式:auto,user,noresize t.tracer(1) scolor = ["#E6005C", "#00BFFF", "#538a30", "#F28500"] # 深色列表 qcolor = ["#FF007F", "#87CEFA", "#7fbc2b", "#FFA500"] # 浅色列表 blsize = 80 # blsize值,blsize,是大等腰直角三角形的斜边风车等比例缩放 bs = 2 ** 0.5 / 2 * blsize # bs是直角边,2**0.5 表示数学中的“根号2” # zjsjxxb是小等腰直角三角形的斜边,zjb是直角边 zjb = blsize / 2 # zjb是小等腰直角三角形的直角边 zjsjxxb = 2 ** 0.5 * zjb # zjsjxxb是小等腰直角三角形的斜边 length = 1.7 * blsize # 风车杆长 width = 2 / 15 * blsize # 风车杆宽 def fongche(): # 风车 t.penup() t.goto(-205, -42) t.begin_fill() t.pensize(4) t.pencolor("#321320") t.fillcolor("#D2B48C") t.circle(15) t.end_fill() t.penup() t.goto(-220, 80) t.pendown() t.setheading(-90) t.pensize(width) t.pencolor("#5f4a1d") t.forward(length) t.pensize(2) t.backward(length) t.setheading(90) for i in range(4): # 小等腰直角三角形 t.color(scolor[i]) # 遍历深色列表scolor t.begin_fill() t.forward(zjb) t.left(90) t.forward(zjb) t.left(135) t.forward(zjsjxxb) t.end_fill() # t.pencolor(scolor[i]) # t.pensize(4) # 大等腰直角三角形 t.color(qcolor[i]) # 遍历浅色列表qcolor t.begin_fill() t.backward(zjsjxxb) t.right(90) t.forward(bs) t.left(135) t.forward(blsize) t.end_fill() # t.pencolor(scolor[i]) # t.pensize(4) # 旋转180度后,画下一片风车叶片 t.right(180) t.penup() mling_circle_list = iter([ # 每段弧线(半径,弧角度数) (18, 360), (14, 360), (10, 360), (6, 360), (18, 360), (14, 360), (10, 360), (6, 360), ]) def mling_draw_eyeball(zb1, zb2, zb3, zb4): for zb, color_ in zip([zb1, zb2, zb3, zb4], ['#ffffff', '#482d08', '#000000', '#ffffff']): t.penup() t.goto(*zb) t.pendown() t.begin_fill() t.setheading(0) t.color(color_) t.pencolor('#000000') t.pensize(2) t.circle(*next(mling_circle_list)) t.end_fill() t.penup() p = t.home() t.pencolor("#321320") t.fillcolor("#cb3263") t.pensize(4) t.goto(120, 110) t.pendown() t.begin_fill() t.goto(200, 0) t.left(-40) t.circle(-110, 105) t.left(75) t.goto(170, -110) t.left(-80) t.circle(30, 40) t.left(60) t.circle(-80, 70) t.left(83) t.circle(-35, 95) t.goto(60, -270) t.left(-80) t.circle(-65, 70) t.left(63) t.circle(35, 30) t.left(130) t.circle(-65, 70) t.goto(-120, -270) t.left(-110) t.circle(-35, 80) t.left(83) t.circle(-80, 50) t.left(60) t.circle(-80, 60) t.left(60) t.circle(30, 30) t.left(20) t.circle(80, 80) t.left(-105) t.circle(-70, 150) t.left(50) t.circle(-170, 50) t.goto(120, 110) # Author:Adversity Awake t.end_fill() t.penup() p = t.home() t.pencolor("#321320") t.fillcolor("#ffffff") t.pensize(4) t.goto(90, 60) t.pendown() t.begin_fill() t.right(30) t.circle(-130, 360) t.end_fill() t.penup() p = t.home() t.pencolor("#321320") t.fillcolor("#f3d2ad") t.pensize(4) t.goto(-250, -55) t.seth(0) t.pendown() t.begin_fill() t.right(-55) t.circle(-45, 270) t.goto(-220, -75) t.goto(-250, -55) t.end_fill() fongche() t.penup() p = t.home() t.pencolor("#321320") t.fillcolor("#f3d2ad") t.pensize(4) t.goto(185, -90) t.pendown() t.begin_fill() t.right(140) t.circle(43, 95) t.goto(185, -90) t.end_fill() t.penup() t.seth(0) t.pencolor('#321320') t.fillcolor('#cb3263') t.pensize(4) t.begin_fill() t.goto(21, 0) t.pendown() t.circle(123, 134) t.left(-90) t.circle(40, 185) t.left(-60) t.circle(120, 60) t.left(-90) t.circle(50, 200) t.left(-90) t.circle(100, 100) t.left(-12) t.circle(100, 40) t.goto(21, 0) t.penup() # Author:Adversity Awake t.end_fill() t.penup() t.goto(0, 0) t.seth(0) t.pencolor('#321320') t.fillcolor('#ffffff') t.pensize(4) t.begin_fill() t.goto(-70, 210) t.left(140) t.pendown() t.circle(30, 200) t.goto(-70, 210) t.penup() t.end_fill() t.penup() t.goto(0, 0) t.seth(0) t.pencolor('#321320') t.fillcolor('#ffffff') t.pensize(4) t.begin_fill() t.goto(90, 220) t.left(45) t.pendown() t.circle(22, 200) t.goto(90, 220) t.penup() t.end_fill() t.penup() t.goto(0, 0) t.seth(0) t.pencolor('#321320') t.fillcolor('#ffffff') t.pensize(4) t.begin_fill() t.left(-98) t.left(90) t.goto(18, 10) t.pendown() t.circle(100, 134) t.left(10) t.circle(110, 30) t.left(10) t.circle(160, 40) t.circle(85, 40) t.left(2) t.circle(95, 40) t.left(5) t.circle(95, 60) t.goto(18, 10) t.penup() t.end_fill() t.penup() p = t.home() t.pencolor("#321320") t.fillcolor("#8f3a52") t.pensize(2) t.goto(25, 240) t.pendown() t.begin_fill() t.goto(60, 235) t.left(30) t.fd(8) t.left(90) t.fd(22) t.circle(90, 8) t.left(20) t.circle(90, 8) t.left(20) t.circle(90, 20) t.left(40) t.circle(50, 20) t.end_fill() t.penup() t.pensize(12) t.goto(-2, 250) t.pencolor("#4D1F00") t.fillcolor("#4D1F00") t.pendown() t.goto(60, 240) t.end_fill() t.penup() p = t.home() t.pencolor("#321320") t.fillcolor("#8f3a52") t.pensize(2) t.goto(-55, 193) t.pendown() t.begin_fill() t.left(65) t.circle(-90, 25) t.goto(-10, 230) t.left(30) t.fd(8) t.left(90) t.fd(18) t.circle(90, 8) t.left(20) t.circle(90, 10) t.left(40) t.circle(90, 30) t.left(30) t.circle(40, 20) t.penup() t.end_fill() t.pensize(12) t.goto(-63, 195) t.pencolor("#4D1F00") t.fillcolor("#4D1F00") t.pendown() t.left(100) t.circle(-85, 45) t.end_fill() mling_draw_eyeball((-20, 180), (-23, 185), (-25, 188), (-30, 200)) mling_draw_eyeball((30, 193), (27, 200), (25, 203), (20, 213)) t.penup() p = t.home() t.pencolor("#321320") t.fillcolor("#8f3a52") t.pensize(3) t.goto(25, 105) p = t.pos() t.pendown() t.begin_fill() t.circle(85, 65) t.left(16) t.circle(30, 55) t.left(20) t.circle(145, 58) t.left(8) t.circle(20, 55) t.left(8) t.circle(50, 65) t.left(-5) t.circle(310, 8) t.end_fill() t.penup() t.goto(0, 0) t.seth(0) t.pencolor('#321320') t.fillcolor('#a93e54') t.pensize(3) t.begin_fill() t.left(-20) t.goto(9, 66) t.pendown() t.circle(68, 40) t.left(10) t.circle(65, 40) t.left(160) t.circle(-75, 85) t.left(158) t.circle(48, 37) t.goto(9, 66) t.penup() t.end_fill() t.color('#321320') t.penup() t.goto(260, 60) t.pendown() t.write("愿\n你\n没\n有\n烦\n恼\n", align="center", font=("黑体", 20, "normal")) t.penup() t.goto(290, 183) t.pendown() t.write("逆\n境\n清\n醒\n", align="center", font=("黑体", 10, "normal")) t.hideturtle() t.done()
4.3哆啦A梦
"
import turtle
def flyTo(x, y): #开启无轨迹跳跃
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
def drawEye(): #画眼睛
turtle.tracer(False)
a = 2.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
else:
a += 0.05
turtle.left(3)
turtle.fd(a)
turtle.tracer(True)
def beard(): #画胡子
# 左边第一根胡子
flyTo(-37, 135)
turtle.seth(165)
turtle.fd(60)
# 左边第二根胡子
flyTo(-37, 125)
turtle.seth(180)
turtle.fd(60)
# 左边第三根胡子
flyTo(-37, 115)
turtle.seth(193)
turtle.fd(60)
# 右边第一根胡子
flyTo(37, 135)
turtle.seth(15)
turtle.fd(60)
# 右边第二根胡子
flyTo(37, 125)
turtle.seth(0)
turtle.fd(60)
# 右边第三根胡子
flyTo(37, 115)
turtle.seth(-13)
turtle.fd(60)
def drawRedScarf():
turtle.fillcolor("red") # 填充颜色
turtle.begin_fill()
turtle.seth(0) # 调整转向向右
turtle.fd(200)
turtle.circle(-5, 90)
turtle.fd(10)
turtle.circle(-5, 90)
turtle.fd(207)
turtle.circle(-5, 90)
turtle.fd(10)
turtle.circle(-5, 90)
turtle.end_fill()
def drawMouse():
flyTo(5, 148)
turtle.seth(270)
turtle.fd(100)
turtle.seth(0)
turtle.circle(120, 50)
turtle.seth(230)
turtle.circle(-120, 100)
def drawRedNose():
flyTo(-10, 158)
turtle.fillcolor("red") # 填充颜色
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
def drawBlackdrawEye():
turtle.seth(0)
flyTo(-20, 195)
turtle.fillcolor("#000000") # 填充颜色
turtle.begin_fill()
turtle.circle(13)
turtle.end_fill()
turtle.pensize(6)
flyTo(20, 205)
turtle.seth(75)
turtle.circle(-10, 150)
turtle.pensize(3)
flyTo(-17, 200)
turtle.seth(0)
turtle.fillcolor("#ffffff")
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
flyTo(0, 0)
def drawFace():
turtle.forward(183) # 前行183个单位
turtle.fillcolor("white") # 填充颜色为白色
turtle.begin_fill() # 开始填充
turtle.left(45) # 左转45度
turtle.circle(120, 100) # 右边那半边脸
turtle.seth(90) # 调整转向向上
drawEye() # 画右眼睛
turtle.seth(180) # 调整转向向左
turtle.penup() # 抬笔
turtle.fd(60) # 前行60
turtle.pendown() # 落笔
turtle.seth(90) # 调整转向向上
drawEye() # 画左眼睛
turtle.penup() # 抬笔
turtle.seth(180) # 调整转向向左
turtle.fd(64) # 前进64
turtle.pendown() # 落笔
turtle.seth(215) # 修改朝向
turtle.circle(120, 100) # 左边那半边脸
turtle.end_fill() #
def drawHead():
turtle.penup() # 抬笔
turtle.circle(150, 40) # 画圆, 半径150,圆周角40
turtle.pendown() # 落笔
turtle.fillcolor("#00a0de") # 填充色
turtle.begin_fill() # 开始填充
turtle.circle(150, 280) # 画圆,半径150, 圆周角280
turtle.end_fill()
def drawAll():
drawHead()
drawRedScarf()
drawFace()
drawRedNose()
drawMouse()
beard()
flyTo(0, 0)
turtle.seth(0)
turtle.penup()
turtle.circle(150, 50)
turtle.pendown()
turtle.seth(30)
turtle.fd(40)
turtle.seth(70)
turtle.circle(-30, 270)
turtle.fillcolor("#00a0de")
turtle.begin_fill()
turtle.seth(230)
turtle.fd(80)
turtle.seth(90)
turtle.circle(1000, 1)
turtle.seth(-89)
turtle.circle(-1000, 10)
turtle.seth(180)
turtle.fd(70)
turtle.seth(90)
turtle.circle(30, 180)
turtle.seth(180)
turtle.fd(70)
turtle.seth(100)
turtle.circle(-1000, 9)
turtle.seth(-86)
turtle.circle(1000, 2)
turtle.seth(230)
turtle.fd(40)
turtle.circle(-30, 230)
turtle.seth(45)
turtle.fd(81)
turtle.seth(0)
turtle.fd(203)
turtle.circle(5, 90)
turtle.fd(10)
turtle.circle(5, 90)
turtle.fd(7)
turtle.seth(40)
turtle.circle(150, 10)
turtle.seth(30)
turtle.fd(40)
turtle.end_fill()
# 左手
turtle.seth(70)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.circle(-30)
turtle.end_fill()
# 脚
flyTo(103.74, -182.59)
turtle.seth(0)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.fd(15)
turtle.circle(-15, 180)
turtle.fd(90)
turtle.circle(-15, 180)
turtle.fd(10)
turtle.end_fill()
flyTo(-96.26, -182.59)
turtle.seth(180)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.fd(15)
turtle.circle(15, 180)
turtle.fd(90)
turtle.circle(15, 180)
turtle.fd(10)
turtle.end_fill()
# 右手
flyTo(-133.97, -91.81)
turtle.seth(50)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
# 口袋
flyTo(-103.42, 15.09)
turtle.seth(0)
turtle.fd(38)
turtle.seth(230)
turtle.begin_fill()
turtle.circle(90, 260)
turtle.end_fill()
flyTo(5, -40)
turtle.seth(0)
turtle.fd(70)
turtle.seth(-90)
turtle.circle(-70, 180)
turtle.seth(0)
turtle.fd(70)
# 铃铛
flyTo(-103.42, 15.09)
turtle.fd(90)
turtle.seth(70)
turtle.fillcolor("#ffd200")
turtle.begin_fill()
turtle.circle(-20)
turtle.end_fill()
turtle.seth(170)
turtle.fillcolor("#ffd200")
turtle.begin_fill()
turtle.circle(-2, 180)
turtle.seth(10)
turtle.circle(-100, 22)
turtle.circle(-2, 180)
turtle.seth(180 - 10)
turtle.circle(100, 22)
turtle.end_fill()
flyTo(-13.42, 15.09)
turtle.seth(250)
turtle.circle(20, 110)
turtle.seth(90)
turtle.fd(15)
turtle.dot(10)
flyTo(0, -150)
drawBlackdrawEye()
def main():
turtle.screensize(800, 6000, "#F0F0F0")
turtle.pensize(3)
turtle.speed(9)
drawAll()
turtle.penup() #抬笔
turtle.goto(100, -300)
#turtle.color("violet") #粉红色有点那个
turtle.write('by peak', font=("Bradley Hand ITC", 30, "bold"))
if __name__ == "__main__":
main()
turtle.mainloop()
4.4魔鬼螺旋
import turtle as t
from turtle import *
angle = 60 #通过改变角度,绘制出各种多边形
t.setup(1280,720)
t.bgcolor('black')
t.pensize(2)
randomColor = ['red','blue','green','purple','gold','pink']
t.speed(0)
for i in range(600):
t.color(randomColor[i%6])
t.fd(i)
t.rt(angle+1)
up()
color("#0fe6ca")
goto(0,0)
down()
t.done()
4.5 紫色小蛇
from turtle import *
setup(900, 350, 200, 200)
up()
fd(-350)
down()
pensize(25)
color("purple")
# 控制画笔起始方向
seth(-40)
# 绘制蛇身
for i in range(4):
circle(60, 80)
circle(-60, 80)
circle(60, 80 / 2)
# 绘制蛇头
fd(60)
circle(32, 180)
fd(60 * 2 / 3)
done()
总结
以上就是本文讲述海龟绘图的内容,并且尝试使用海龟绘图模块(类)绘制一些基础图形,希望帮助大家更好的学习知识。