入门学习

​设置画布大小:若不设置则默认400×300,None

class="python3">turtle.screensize(canvwidth=None, canvheight=None, bg=None)

设置画布位置:

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

在画布上,默认有一个坐标原点为画布中心的坐标轴,坐标原点上有一只面朝x轴正方向的小海龟。在turtle绘图中,正是使用位置、方向来描述小海龟(画笔)的状态。

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

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

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

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

常见的移动命令:

命令

说明

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(radius, extent=None,step=None)

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

setx( )

将当前x轴移动到指定位置

sety( )

将当前y轴移动到指定位置

setheading(angle)/seth(angle)

设置当前朝向为angle角度

home()

设置当前画笔位置为原点,朝向东。

dot(r)

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

distance为传入的数值,代表移动的长度。

degree为传入的数值,代表转动的角度。

angle为传入的数值,以右边(东边)为0。当设置为0时朝向右边(东边),设置为90时朝向北边(上边),以此类推。

常用颜色命令:

命令

说明

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

全局控制命令:

命令

说明

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

其他常用命令:

命令

说明

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

返回最后记录的多边形。

实例

五角星

import turtle                #导入turtle库包
turtle.screensize(300, 300) #设置画布大小
turtle.speed(10) #设置画笔速度为2
turtle.fillcolor("red") #填充颜色
turtle.begin_fill() #开始画,类似起笔
turtle.up() #提笔
turtle.fd(-150)
count = 1 #计时器,用于计录次数
while count <= 5: #控制绘制次数
turtle.forward(250) #画笔绘制的方向,向前移动指定的距离
turtle.right(144) #向右转144度
count += 1 #循环绘制
turtle.down() #下笔
turtle.end_fill() #完成填充图片的绘制
turtle.done() #保持窗口停留

四叶草

import turtle as t

#准备设置
t.screensize(400, 300) #设置画布大小
t.setup(840, 500) #设置主窗口的大小为840*500
t.pensize(5) #设置外花边的大小
t.color('mediumseagreen', 'forestgreen') #设置画笔颜色和填充颜色
t.speed(10) #设置画笔速度为10

def draw_clover(radius, rotate): #参数radius控制叶子的大小,rotate控制叶子的旋转
t.begin_fill() #外形填充开始标志
for i in range(4): #从0到3开始的for循环,共四片花瓣
direction = i * 90
t.seth(60 + direction + rotate) #控制叶子根部的角度为60度
t.fd(4 * radius)
for j in range(2):
t.seth(90 + direction + rotate)
t.circle(radius, 180)
t.seth(-60 + direction + rotate)
t.fd(4 * radius)
t.seth(60 + direction+rotate) #控制叶子根部的角度为60度
t.fd(2 * radius)
for j in range(2): #从0到1开始的for循环,画内花边
t.pencolor("whitesmoke") #设置内花边颜色
t.pensize(8) #设置内花边的大小
t.seth(90 + direction + rotate)
t.circle(radius/2, 180)
t.color('mediumseagreen', 'forestgreen')
t.pensize(5)
t.seth(-60 + direction + rotate)
t.fd(2 * radius)
t.end_fill() #依据轮廓填充颜色
t.seth(-110)
t.fd(6 * radius)

draw_clover(40, 25)
t.done() #保持窗口停留

太阳花

import turtle as t

#准备设置
t.screensize(400, 300) #设置画布大小
t.setup(840,500) #设置主窗口的大小为840*500
t.pensize(2) #设置画笔的大小
t.color('red','yellow') #设置画笔颜色和填充颜色(pink)
t.speed(50) #设置画笔速度为10

t.penup() #提笔
t.goto(-150,0) #画笔前往坐标(-150,0)
t.pendown() #下笔

t.begin_fill() #准备绘制
while True:
t.forward(300) #画笔前进300个像素
t.left(170) #画笔左转170度
if t.distance(-150, 0) < 1: #如果当前坐标点距离出发点(150,0)小于1,则跳出循环
break
t.end_fill() #依据轮廓填充颜色

#保持窗口停留
t.done()

小猪佩奇

import turtle as t

# 准备设置
t.screensize(400, 300) # 设置画布大小
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255, 155, 192), "pink") # 设置画笔颜色和填充颜色(pink)
t.setup(840, 500) # 设置主窗口的大小为840*500
t.speed(50) # 设置画笔速度为10

# 画鼻子
t.penup() # 提笔
t.goto(-100, 100) # 画笔前往坐标(-100,100)
t.pendown() # 下笔
t.seth(-30) # 笔的角度为-30°(鼻子向右倾斜30度),但未行进
t.begin_fill() # 外形填充的开始标志
a = 0.4 # 设置初始步长
for i in range(120): # 循环一个从0开始到119的列表
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) # 向左转3度
t.fd(a) # 向前走a的步长
t.end_fill() # 依据轮廓填充颜色
t.penup() # 提笔
t.seth(90) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10) # 向前移动10
t.pendown() # 下笔
t.pencolor(255, 155, 192) # 设置画笔颜色
t.seth(10) # 转换画笔的角度为10
t.begin_fill() # 外形填充开始标志
t.circle(5) # 画一个半径为5的圆
t.color(160, 82, 45) # 设置填充颜色
t.end_fill() # 依据轮廓填充颜色
t.penup() # 提笔
t.seth(0) # 转换画笔的角度为0
t.fd(20) # 向前移动25
t.pendown() # 下笔
t.pencolor(255, 155, 192) # 设置画笔和填充颜色
t.seth(10) # 转换画笔的角度为10
t.begin_fill() # 外形填充开始标志
t.circle(5) # 画一个半径为5的圆
t.color(160, 82, 45) # 设置填充颜色
t.end_fill() # 依据轮廓填充颜色

# 以此类推,画头
t.color((255, 155, 192), "pink")
t.penup()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pendown()
t.begin_fill()
t.seth(180)
t.circle(300, -30) # 顺时针画一个半径为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.penup()
t.goto(-100, 100)
t.pendown()
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.penup()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pendown()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.penup()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pendown()
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.penup()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pendown()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.penup()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pendown()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.penup()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pendown()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.penup()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pendown()
t.begin_fill()
t.circle(3)
t.end_fill()

# 画腮
t.color((255, 155, 192))
t.penup()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pendown()
t.begin_fill()
t.circle(30)
t.end_fill()

# 画嘴
t.color(239, 69, 19)
t.penup()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pendown()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)

# 画身体
t.color("red", (255, 99, 71))
t.penup()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pendown()
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.penup()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pendown()
t.seth(-160)
t.circle(300, 15)
t.penup()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pendown()
t.seth(-10)
t.circle(-20, 90)
t.penup()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pendown()
t.seth(-20)
t.circle(-300, 15)
t.penup()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pendown()
t.seth(-170)
t.circle(20, 90)

# 画脚
t.pensize(10)
t.color((240, 128, 128))
t.penup()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pendown()
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.penup()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pendown()
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.penup()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pendown()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)

t.done() # 画布窗口停留

当前时刻钟表

import turtle

from datetime import * # 导入datetime模块

# 准备设置

turtle.screensize(400, 300) # 设置画布大小

turtle.setup(840, 500) # 设置主窗口的大小为840*500


# 抬起画笔,向前运动一段距离放下

def Skip(step): # 由于表盘刻度不连续,需频繁抬起画笔,放下画笔

turtle.penup() # 落笔

turtle.forward(step) # 向当前画笔方向移动"step"像素长度,具体的参数由后面方法传入

turtle.pendown() # 提笔


# 定义指针几何形状。

def mkHand(name, length):
turtle.reset() # 重置turtle状态为初始状态

Skip(-length * 0.1) # 调用Skip()方法,移动的长度为"-length*0.1"像素

turtle.begin_poly() # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。

turtle.forward(length * 1.1) # 向当前画笔方向移动"length*1.1"像素长度

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

handForm = turtle.get_poly() # 返回最后记录的多边形。

turtle.register_shape(name, handForm) # 注册handForm为合法的turtle外形


# 初始化表针和文本对象

def Init():
global secHand, minHand, hurHand, printer

turtle.mode("logo") # 重置Turtle指向北

mkHand("secHand", 145) # 建立三个表针Turtle并初始化,设置secHand即秒针长度为135像素

mkHand("minHand", 120) # 设置minHand即分针长度为125像素

mkHand("hurHand", 90) # 设置hurHand即时针长度为90像素

secHand = turtle.Turtle()

secHand.shape("secHand")

minHand = turtle.Turtle()

minHand.shape("minHand")

hurHand = turtle.Turtle()

hurHand.shape("hurHand")

for hand in secHand, minHand, hurHand: # 遍历secHand, minHand, hurHand三个序列

hand.shapesize(1, 1, 3) # 设置表征形状大小为3像素

hand.speed(0) # 设置表针的速度为0

printer = turtle.Turtle() # 建立输出文字Turtle

printer.hideturtle() # 隐藏画笔的turtle形状

printer.penup() # 下笔


def SetupClock(radius): # 建立表的外框,表盘半径radius为参数

turtle.reset() # 重置turtle状态为初始状态

turtle.pensize(7) # 设置画笔的大小为7像素

turtle.pencolor("#0A0A0A") # 设置画笔的颜色为黑色

turtle.fillcolor("green") # 绘制图形的填充颜色为绿色

# 定义一个i值,遍历i值,根据条件画出钟的各个点与1到12的数字

for i in range(60): # 创建一个从0到59的整数列表

Skip(radius) # 调用Skip()方法,radius为参数

if i % 5 == 0: # “%”运算符为返回除法的余数,则如果i除以5余数为0

turtle.forward(20) # 向当前画笔方向移动20像素长度

Skip(-radius - 20) # 调用Skip()方法,向当前画笔方向移动"-radius-20"像素长度,即效果图中粗的点

Skip(radius + 20) # 调用Skip()方法,向当前画笔方向移动"radius + 20"像素长度,即返回原位

if i == 0: # 如果i等于0

turtle.write(int(12), align="center",
font=("Courier", 14, "bold")) # 写下数字12,水平居中,font(字体名称,大小,加粗)为设置字体参数

elif i == 30: # elif语句,当遍历到对应的条件语句后,后面所有的elif和else都不会再被执行。如果i等于30

Skip(25) # 调用Skip()方法,向当前画笔方向移动25像素长度

turtle.write(int(i / 5), align="center",
font=("Courier", 14, "bold")) # 写下数字6,水平居中,font(字体名称,大小,加粗)为设置字体参数

Skip(-25) # 调用Skip()方法,向当前画笔方向移动-25像素长度,即返回原位

elif (i == 25 or i == 35): # “or”为布尔"或",如果i等于25,它返回i等于25,否则它返回 i等于35。

Skip(20) # 调用Skip()方法,向当前画笔方向移动20像素长度

turtle.write(int(i / 5), align="center",
font=("Courier", 14, "bold")) # 写下数字5/7,水平居中,font(字体名称,大小,加粗)为设置字体参数

Skip(-20) # 调用Skip()方法,向当前画笔方向移动-20像素长度,即返回原位

else:

turtle.write(int(i / 5), align="center",
font=("Courier", 14, "bold")) # 写下数字1、2、3、4、8、9、10、11,水平居中,font(字体名称,大小,加粗)为设置字体参数

Skip(-radius - 20) # 调用Skip()方法,向当前画笔方向移动"-radius-20"像素长度

else:

turtle.dot(5) # 绘制一个指定直径为5的圆点

Skip(-radius) # 调用Skip()方法,向当前画笔方向移动"-radius"像素长度

turtle.right(6) # 画笔的角度向右转6度


# 绘制表针的动态显示

def Tick():
t = datetime.today() # 获取时间

second = t.second + t.microsecond * 0.000001

minute = t.minute + second / 60.0

hour = t.hour + minute / 60.0

secHand.setheading(6 * second)

minHand.setheading(6 * minute)

hurHand.setheading(30 * hour)

turtle.tracer(False)

printer.home()

turtle.tracer(True)

turtle.ontimer(Tick, 100) # 100ms后继续调用tick


# 定义执行的程序的main()方法

def main():
turtle.tracer(False) # 打开/关闭快速绘图动画,并为更新图纸设置延迟,False为不显示绘画过程,True为显示绘画过程

Init() # 执行定义的Init()方法

SetupClock(180) # 执行定义的SetupClock()方法,设置钟表盘半径参数radius为180

Tick() # 执行定义的Tick()方法

turtle.mainloop() # 启动事件循环


if __name__ == "__main__":
main() # 执行定义的main()方法

谢尔宾斯基三角形

import turtle as t

# 准备设置
t.screensize(400, 300) # 设置画布大小
t.setup(840, 500) # 设置主窗口的大小为840*500
t.speed(100) # 设置画笔速度为10


def draw_triangle(size): # 传入列表中三个点的坐标,绘制三角形
t.penup()
t.goto(size[0])
t.pendown()
t.goto(size[1])
t.goto(size[2])
t.goto(size[0])


def get_mid(a, b): # 计算返回任意2个点的中间点坐标
x = (a[0] + b[0]) / 2
y = (a[1] + b[1]) / 2
return [x, y]


def draw_s(size, times): # 递归times次,调用绘制函数draw_triangle(size)
draw_triangle(size) # 绘制三角形
if times > 1:
# 绘制左边小三角形
size2 = [size[0], get_mid(size[0], size[1]), get_mid(size[0], size[2])]
draw_s(size2, times - 1)

# 绘制上边的小三角形
size3 = [get_mid(size[0], size[2]), get_mid(size[1], size[2]), size[2]]
draw_s(size3, times - 1)

# 绘制右边的小三角形
size4 = [get_mid(size[0], size[1]), size[1], get_mid(size[1], size[2])]
draw_s(size4, times - 1)


t.left(90) # 向左转90度
points = [[-200, -150], [200, -150], [0, 150]] # 初始三角形坐标
count = 5 # 递归5次
draw_s(points, count) # 调用绘制谢尔宾斯基三角形函数
t.done() # 保持窗口停留