Turtle模块的函数
1.画笔移动函数
函数 | 功能 |
---|
forward(n) | 向画笔的当前方向移动n像素的距离 |
backward(n) | 向画笔当前方向的相反方向移动n像素的距离 |
left(n) | 让画笔逆时针旋转n度 |
right(n) | 让画笔顺时针旋转n度 |
pendown() | 落下画笔 |
penup() | 抬起画笔 |
speed(s) | 设置画笔的移动速度,int(s=0~10) |
goto(x,y) | 将画笔移动到坐标为(x,y)的位置 |
circle(r,n) | 绘制半径为r,角度为n的圆弧,省略n则画圆 |
2.画笔控制函数
函数 | 功能 |
---|
pensize(n) | 设置画笔的粗细 |
pencolor(color) | 设置画笔的颜色 |
fillcolor(color) | 设置图形的填充颜色 |
color(color1,color2) | 设置画笔的颜色和图形的填充颜色 |
begin_fill() | 准备开始填充图形 |
end_fill() | 填充上次调用begin_fill()之后绘制的图形 |
hideturtle() | 隐藏画笔 |
showturtle() | 显示画笔 |
3.全局控制函数
函数 | 功能 |
---|
clear() | 清空画布,不改变画笔位置与状态 |
reset() | 重置画布,让画笔回到初始状态 |
undo() | 撤销上一个画笔动作 |
stamp() | 复制当前图形 |
write(a[,m=(“m-name”, m-size, “m-type”)]) | 在画布上书写文本,a为文本内容 |
爱心
from turtle import *
color("red","red")
pensize(2)
penup()
goto(0,-100)
pendown()
begin_fill()
left(45)
forward(200)
circle(100,180)
right(90)
circle(100,180)
forward(200)
end_fill()
hideturtle()
哆啦A梦
from turtle import *
def my_goto(x, y):
penup()
goto(x, y)
pendown()
def eyes():
fillcolor("#ffffff")
begin_fill()
tracer(False)
a = 2.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
lt(3)
fd(a)
else:
a += 0.05
lt(3)
fd(a)
tracer(True)
end_fill()
def beard():
my_goto(-32, 135)
seth(165)
fd(60)
my_goto(-32, 125)
seth(180)
fd(60)
my_goto(-32, 115)
seth(193)
fd(60)
my_goto(37, 135)
seth(15)
fd(60)
my_goto(37, 125)
seth(0)
fd(60)
my_goto(37, 115)
seth(-13)
fd(60)
def mouth():
my_goto(5, 148)
seth(270)
fd(100)
seth(0)
circle(120, 50)
seth(230)
circle(-120, 100)
def scarf():
fillcolor('#e70010')
begin_fill()
seth(0)
fd(200)
circle(-5, 90)
fd(10)
circle(-5, 90)
fd(207)
circle(-5, 90)
fd(10)
circle(-5, 90)
end_fill()
def nose():
my_goto(-10, 158)
seth(315)
fillcolor('#e70010')
begin_fill()
circle(20)
end_fill()
def black_eyes():
seth(0)
my_goto(-20, 195)
fillcolor('#000000')
begin_fill()
circle(13)
end_fill()
pensize(6)
my_goto(20, 205)
seth(75)
circle(-10, 150)
pensize(3)
my_goto(-17, 200)
seth(0)
fillcolor('#ffffff')
begin_fill()
circle(5)
end_fill()
my_goto(0, 0)
def face():
fd(183)
lt(45)
fillcolor('#ffffff')
begin_fill()
circle(120, 100)
seth(180)
fd(121)
pendown()
seth(215)
circle(120, 100)
end_fill()
my_goto(63.56, 218.24)
seth(90)
eyes()
seth(180)
penup()
fd(60)
pendown()
seth(90)
eyes()
penup()
seth(180)
fd(64)
def head():
penup()
circle(150, 40)
pendown()
fillcolor('#00a0de')
begin_fill()
circle(150, 280)
end_fill()
def Doraemon():
head()
scarf()
face()
nose()
mouth()
beard()
my_goto(0, 0)
seth(0)
penup()
circle(150, 50)
pendown()
seth(30)
fd(40)
seth(70)
circle(-30, 270)
fillcolor('#00a0de')
begin_fill()
seth(230)
fd(80)
seth(90)
circle(1000, 1)
seth(-89)
circle(-1000, 10)
seth(180)
fd(70)
seth(90)
circle(30, 180)
seth(180)
fd(70)
seth(100)
circle(-1000, 9)
seth(-86)
circle(1000, 2)
seth(230)
fd(40)
circle(-30, 230)
seth(45)
fd(81)
seth(0)
fd(203)
circle(5, 90)
fd(10)
circle(5, 90)
fd(7)
seth(40)
circle(150, 10)
seth(30)
fd(40)
end_fill()
seth(70)
fillcolor('#ffffff')
begin_fill()
circle(-30)
end_fill()
my_goto(103.74, -182.59)
seth(0)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(-15, 180)
fd(90)
circle(-15, 180)
fd(10)
end_fill()
my_goto(-96.26, -182.59)
seth(180)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(15, 180)
fd(90)
circle(15, 180)
fd(10)
end_fill()
my_goto(-133.97, -91.81)
seth(50)
fillcolor('#ffffff')
begin_fill()
circle(30)
end_fill()
my_goto(-103.42, 15.09)
seth(0)
fd(38)
seth(230)
begin_fill()
circle(90, 260)
end_fill()
my_goto(5, -40)
seth(0)
fd(70)
seth(-90)
circle(-70, 180)
seth(0)
fd(70)
my_goto(-103.42, 15.09)
fd(90)
seth(70)
fillcolor('#ffd200')
begin_fill()
circle(-20)
end_fill()
seth(170)
fillcolor('#ffd200')
begin_fill()
circle(-2, 180)
seth(10)
circle(-100, 22)
circle(-2, 180)
seth(180 - 10)
circle(100, 22)
end_fill()
goto(-13.42, 15.09)
seth(250)
circle(20, 110)
seth(90)
fd(15)
dot(10)
my_goto(0, -150)
black_eyes()
if __name__ == '__main__':
screensize(800, 600, "#f0f0f0")
pensize(3)
speed(3)
Doraemon()
my_goto(100, -300)
mainloop()
皮卡丘
import turtle as t
import time
t.screensize(800, 600)
t.pensize(2)
t.speed(10)
def radian_left(ang, dis, step, n):
for i in range(n):
dis += step
t.lt(ang)
t.fd(dis)
def radian_right(ang, dis, step, n):
for i in range(n):
dis += step
t.rt(ang)
t.fd(dis)
def InitEars():
t.color("black", "yellow")
t.pu()
t.goto(-50, 100)
t.pd()
t.setheading(110)
t.begin_fill()
radian_left(1.2, 0.4, 0.1, 40)
t.setheading(270)
radian_left(1.2, 0.4, 0.1, 40)
t.setheading(44)
t.forward(32)
t.end_fill()
t.pu()
t.goto(50, 100)
t.pd()
t.setheading(70)
t.begin_fill()
radian_right(1.2, 0.4, 0.1, 40)
t.setheading(270)
radian_right(1.2, 0.4, 0.1, 40)
t.setheading(136)
t.forward(32)
t.end_fill()
t.begin_fill()
t.fillcolor("black")
t.pu()
t.goto(88, 141)
t.pd()
t.setheading(35)
radian_right(1.2, 1.6, 0.1, 16)
t.setheading(270)
radian_right(1.2, 0.4, 0.1, 25)
t.setheading(132)
t.forward(31)
t.end_fill()
t.begin_fill()
t.fillcolor("black")
t.pu()
t.goto(-88, 141)
t.pd()
t.setheading(145)
radian_left(1.2, 1.6, 0.1, 16)
t.setheading(270)
radian_left(1.2, 0.4, 0.1, 25)
t.setheading(48)
t.forward(31)
t.end_fill()
def InitTail():
t.begin_fill()
t.fillcolor("yellow")
t.pu()
t.goto(64, -140)
t.pd()
t.setheading(10)
t.forward(20)
t.setheading(90)
t.forward(20)
t.setheading(10)
t.forward(10)
t.setheading(80)
t.forward(100)
t.setheading(35)
t.forward(80)
t.setheading(260)
t.forward(100)
t.setheading(205)
t.forward(40)
t.setheading(260)
t.forward(37)
t.setheading(205)
t.forward(20)
t.setheading(260)
t.forward(25)
t.setheading(175)
t.forward(30)
t.setheading(100)
t.forward(13)
t.end_fill()
def InitFoots():
t.begin_fill()
t.fillcolor("yellow")
t.pensize(2)
t.pu()
t.goto(-70, -200)
t.pd()
t.setheading(225)
radian_left(0.5, 1.2, 0, 12)
radian_left(35, 0.6, 0, 4)
radian_left(1, 1.2, 0, 18)
t.setheading(160)
t.forward(13)
t.end_fill()
t.begin_fill()
t.fillcolor("yellow")
t.pensize(2)
t.pu()
t.goto(70, -200)
t.pd()
t.setheading(315)
radian_right(0.5, 1.2, 0, 12)
radian_right(35, 0.6, 0, 4)
radian_right(1, 1.2, 0, 18)
t.setheading(20)
t.forward(13)
t.end_fill()
def InitBody():
t.begin_fill()
t.pu()
t.goto(112, 0)
t.pd()
t.setheading(90)
t.circle(112, 180)
t.setheading(250)
radian_left(1.6, 1.3, 0, 50)
radian_left(0.8, 1.5, 0, 25)
t.setheading(255)
radian_left(0.4, 1.6, 0.2, 27)
radian_left(2.8, 1, 0, 45)
radian_right(0.9, 1.4, 0, 31)
t.setheading(355)
radian_right(0.9, 1.4, 0, 31)
radian_left(2.8, 1, 0, 45)
radian_left(0.4, 7.2, -0.2, 27)
t.setheading(10)
radian_left(0.8, 1.5, 0, 25)
radian_left(1.6, 1.3, 0, 50)
t.end_fill()
def InitEyes():
t.begin_fill()
t.fillcolor("black")
t.pu()
t.goto(-46, 10)
t.pd()
t.setheading(90)
t.circle(5, 360)
t.end_fill()
t.begin_fill()
t.fillcolor("black")
t.pu()
t.goto(46, 10)
t.pd()
t.setheading(-90)
t.circle(5, 360)
t.end_fill()
def InitFace():
t.begin_fill()
t.fillcolor("red")
t.pu()
t.goto(-63, -10)
t.pd()
t.setheading(90)
t.circle(10, 360)
t.end_fill()
t.begin_fill()
t.fillcolor("red")
t.pu()
t.goto(63, -10)
t.pd()
t.setheading(-90)
t.circle(10, 360)
t.end_fill()
t.pensize(2.2)
t.pu()
t.goto(0, 0)
t.pd()
t.setheading(235)
radian_right(5, 0.8, 0, 30)
t.pu()
t.goto(0, 0)
t.pd()
t.setheading(305)
radian_left(5, 0.8, 0, 30)
def InitHands():
t.pensize(2)
t.pu()
t.goto(-46, -100)
t.pd()
t.setheading(285)
radian_right(0.4, 1.2, 0, 26)
radian_right(5, 0.35, 0, 26)
radian_right(0.3, 1.2, 0, 15)
t.pu()
t.goto(46, -100)
t.pd()
t.setheading(255)
radian_left(0.4, 1.2, 0, 26)
radian_left(5, 0.35, 0, 26)
radian_left(0.3, 1.2, 0, 15)
def CloseEyes():
t.pu()
t.goto(-46, 12)
t.pd()
t.setheading(180)
t.forward(10)
t.pu()
t.goto(46, 12)
t.pd()
t.setheading(0)
t.forward(10)
def Init():
InitEars()
InitTail()
InitFoots()
InitBody()
InitFace()
InitHands()
InitEyes()
def Upgarde():
InitEars()
InitTail()
InitFoots()
InitBody()
InitFace()
InitHands()
CloseEyes()
def Upgarde_Init():
InitEars()
InitTail()
InitFoots()
InitBody()
InitFace()
InitHands()
InitEyes()
def main():
Init()
t.tracer(False)
for i in range(30):
if i % 2 == 0:
t.reset()
t.hideturtle()
Upgarde()
t.update()
time.sleep(0.3)
else:
t.reset()
t.hideturtle()
Upgarde_Init()
t.update()
time.sleep(1)
main()
捂脸表情
import turtle
def arc(sa, ea, x, y, r):
turtle.penup()
turtle.goto(x, y)
turtle.setheading(0)
turtle.left(sa)
turtle.fd(r)
turtle.pendown()
turtle.left(90)
turtle.circle(r, (ea - sa))
return turtle.position()
turtle.hideturtle()
turtle.speed(5)
turtle.setup(900, 600, 200, 200)
turtle.pensize(5)
turtle.right(90)
turtle.penup()
turtle.fd(100)
turtle.left(90)
turtle.pendown()
turtle.begin_fill()
turtle.pencolor("#B26A0F")
turtle.circle(150)
turtle.fillcolor("#F9E549")
turtle.end_fill()
turtle.penup()
turtle.goto(77, 20)
turtle.pencolor("#744702")
turtle.goto(0, 50)
turtle.right(30)
turtle.fd(110)
turtle.right(90)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("#925902")
turtle.circle(-97, 160)
turtle.goto(92, -3)
turtle.end_fill()
turtle.penup()
turtle.goto(77, -25)
turtle.pencolor("white")
turtle.begin_fill()
turtle.fillcolor("white")
turtle.goto(77, -24)
turtle.goto(-81, 29)
turtle.goto(-70, 43)
turtle.goto(77, -8)
turtle.end_fill()
turtle.penup()
turtle.goto(0, -100)
turtle.setheading(0)
turtle.pendown()
turtle.left(90)
turtle.penup()
turtle.fd(150)
turtle.right(60)
turtle.fd(-150)
turtle.pendown()
turtle.left(20)
turtle.pencolor("#155F84")
turtle.fd(150)
turtle.right(180)
position1 = turtle.position()
turtle.begin_fill()
turtle.fillcolor("#7EB0C8")
turtle.fd(150)
turtle.right(20)
turtle.left(270)
turtle.circle(-150, 18)
turtle.right(52)
turtle.fd(110)
position2 = turtle.position()
turtle.goto(-33, 90)
turtle.end_fill()
turtle.penup()
turtle.goto(0, 0)
turtle.setheading(0)
turtle.left(90)
turtle.fd(50)
turtle.right(150)
turtle.fd(150)
turtle.left(150)
turtle.fd(100)
turtle.pendown()
turtle.begin_fill()
turtle.fd(-100)
turtle.fillcolor("#7EB0C8")
turtle.right(60)
turtle.circle(150, 15)
turtle.left(45)
turtle.fd(66)
turtle.goto(77, 20)
turtle.end_fill()
turtle.penup()
turtle.pencolor("#6C4E00")
turtle.goto(-65, 75)
turtle.setheading(0)
turtle.left(27)
turtle.fd(38)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("#6C4E00")
turtle.left(90)
turtle.circle(38, 86)
turtle.goto(position2[0], position2[1])
turtle.goto(position1[0], position1[1])
turtle.end_fill()
turtle.pencolor("#D57E18")
turtle.begin_fill()
turtle.fillcolor("#EFBD3D")
arc(-110, 10, 110, -40, 30)
turtle.circle(300, 35)
turtle.circle(13, 120)
turtle.setheading(-50)
turtle.fd(20)
turtle.setheading(130)
turtle.circle(200, 15)
turtle.circle(12, 180)
turtle.fd(40)
turtle.setheading(137)
turtle.circle(200, 16)
turtle.circle(12, 160)
turtle.setheading(-35)
turtle.fd(45)
turtle.setheading(140)
turtle.circle(200, 13)
turtle.circle(11, 160)
turtle.setheading(-35)
turtle.fd(40)
turtle.setheading(145)
turtle.circle(200, 9)
turtle.circle(10, 180)
turtle.setheading(-31)
turtle.fd(50)
turtle.setheading(-45)
turtle.pensize(7)
turtle.right(5)
turtle.circle(180, 35)
turtle.end_fill()
turtle.begin_fill()
turtle.setheading(-77)
turtle.pensize(5)
turtle.fd(50)
turtle.left(-270)
turtle.fd(7)
turtle.pencolor("#EFBD3D")
turtle.circle(30, 180)
turtle.end_fill()
turtle.done()
小猪佩奇
from turtle import *
def nose(x, y):
penup()
goto(x, y)
pendown()
setheading(-30)
begin_fill()
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
left(3)
forward(a)
else:
a = a - 0.08
left(3)
forward(a)
end_fill()
penup()
setheading(90)
forward(25)
setheading(0)
forward(10)
pendown()
pencolor(255, 155, 192)
setheading(10)
begin_fill()
circle(5)
color(160, 82, 45)
end_fill()
penup()
setheading(0)
forward(20)
pendown()
pencolor(255, 155, 192)
setheading(10)
begin_fill()
circle(5)
color(160, 82, 45)
end_fill()
def head(x, y):
color((255, 155, 192), "pink")
penup()
goto(x, y)
setheading(0)
pendown()
begin_fill()
setheading(180)
circle(300, -30)
circle(100, -60)
circle(80, -100)
circle(150, -20)
circle(60, -95)
setheading(161)
circle(-300, 15)
penup()
goto(-100, 100)
pendown()
setheading(-30)
a = 0.4
for i in range(60):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
lt(3)
fd(a)
else:
a = a - 0.08
lt(3)
fd(a)
end_fill()
def ears(x, y):
color((255, 155, 192), "pink")
penup()
goto(x, y)
pendown()
begin_fill()
setheading(100)
circle(-50, 50)
circle(-10, 120)
circle(-50, 54)
end_fill()
penup()
setheading(90)
forward(-12)
setheading(0)
forward(30)
pendown()
begin_fill()
setheading(100)
circle(-50, 50)
circle(-10, 120)
circle(-50, 56)
end_fill()
def eyes(x, y):
color((255, 155, 192), "white")
penup()
setheading(90)
forward(-20)
setheading(0)
forward(-95)
pendown()
begin_fill()
circle(15)
end_fill()
color("black")
penup()
setheading(90)
forward(12)
setheading(0)
forward(-3)
pendown()
begin_fill()
circle(3)
end_fill()
color((255, 155, 192), "white")
penup()
seth(90)
forward(-25)
seth(0)
forward(40)
pendown()
begin_fill()
circle(15)
end_fill()
color("black")
penup()
setheading(90)
forward(12)
setheading(0)
forward(-3)
pendown()
begin_fill()
circle(3)
end_fill()
def cheek(x, y):
color((255, 155, 192))
penup()
goto(x, y)
pendown()
setheading(0)
begin_fill()
circle(30)
end_fill()
def mouth(x, y):
color(239, 69, 19)
penup()
goto(x, y)
pendown()
setheading(-80)
circle(30, 40)
circle(40, 80)
def body(x, y):
color("red", (255, 99, 71))
penup()
goto(x, y)
pendown()
begin_fill()
setheading(-130)
circle(100, 10)
circle(300, 30)
setheading(0)
forward(230)
setheading(90)
circle(300, 30)
circle(100, 3)
color((255, 155, 192), (255, 100, 100))
setheading(-135)
circle(-80, 63)
circle(-150, 24)
end_fill()
def hands(x, y):
color((255, 155, 192))
penup()
goto(x, y)
pendown()
setheading(-160)
circle(300, 15)
penup()
setheading(90)
forward(15)
setheading(0)
forward(0)
pendown()
setheading(-10)
circle(-20, 90)
penup()
setheading(90)
forward(30)
setheading(0)
forward(237)
pendown()
setheading(-20)
circle(-300, 15)
penup()
setheading(90)
forward(20)
setheading(0)
forward(0)
pendown()
setheading(-170)
circle(20, 90)
def foot(x, y):
pensize(10)
color((240, 128, 128))
penup()
goto(x, y)
pendown()
setheading(-90)
forward(40)
setheading(-180)
color("black")
pensize(15)
fd(20)
pensize(10)
color((240, 128, 128))
penup()
setheading(90)
forward(40)
setheading(0)
forward(90)
pendown()
setheading(-90)
forward(40)
setheading(-180)
color("black")
pensize(15)
fd(20)
def tail(x, y):
pensize(4)
color((255, 155, 192))
penup()
goto(x, y)
pendown()
seth(0)
circle(70, 20)
circle(10, 330)
circle(70, 30)
def setting():
pensize(4)
hideturtle()
colormode(255)
color((255, 155, 192), "pink")
setup(840, 500)
speed(10)
def main():
setting()
nose(-100, 100)
head(-69, 167)
ears(0, 160)
eyes(0, 140)
cheek(80, 10)
mouth(-20, 30)
body(-32, -8)
hands(-56, -45)
foot(2, -177)
tail(148, -155)
done()
if __name__ == '__main__':
main()