目录
turtle库的一些函数说明
关于turtle库的一些小例子:
1、画奥运五环
2、小猪佩奇
3、小黄人
turtle库的一些函数说明
turtle.setup()用于启动一个图形窗口,它有四个参数turtle.setup(width,height,startx,starty),分别是启动窗口的宽度、高度和窗口启动时,窗口左上角在屏幕上的坐标位置。
turtle.penup()让画笔离开画布,这样在移动画笔的时候不会出现线条
turtle.pendown()放下画笔
turtle.circle(200),以现在箭头为方向,逆时针画一个半径为200的圆
turtle.pensize()表示线条宽度
turtle.pencolor()表示线条颜色
turtle.seth(angle)表示运动方向,其中0表示东,90度向北,负值表示相反方向
turtle.circle(rad,angle)表示圆形爬行,若rad为负,则半径在小乌龟运行的右侧,angle表示爬行的角度
turtle.fd(),即turtle.forward(),表示向前爬行,参数表示距离
turtle.goto(x,y),以中心为原点建立坐标轴,上正下负,右正左负
turtle库很强大,但是如果想要用好,那么一些基本的数学运算是必需的。
关于turtle库的一些小例子:
1、画奥运五环
效果图:
代码如下:
import turtle as t
t.pensize(9)
t.speed(10)
t.circle(60)
t.penup()
t.color('blue')
t.goto(-130, 0)
t.pendown()
t.circle(60)
t.penup()
t.color('red')
t.goto(130, 0)
t.pendown()
t.circle(60)
t.penup()
t.goto(-75, -65)
t.pendown()
t.color('orange')
t.circle(60)
t.penup()
t.goto(75, -60)
t.color('green')
t.pendown()
t.circle(60)
t.done()
2、小猪佩奇
效果图:
(坐标算的头有点懵,特别是眼睛、衣服、手的位置)(PS:哪位大佬有好的办法计算坐标可以分享一下嘛?,菜鸡先谢谢啦)
import time
import turtle as t
t.speed(10)
t.penup()
t.goto(-100, 100)
t.pendown()
t.setheading(-30)
t.pensize(5)
"""nose"""
t.color('deep pink')
x = 0.4
t.begin_fill()
for i in range(120):
if 0<=i<30 or 60<=i<90:
x = x + 0.08
t.left(3)
t.forward(x)
else:
x = x - 0.08
t.left(3)
t.forward(x)
t.color('pink')
t.end_fill()
"""左鼻孔"""
t.penup()
t.goto(-95, 130)
t.pendown()
t.color('deep pink')
t.circle(3)
t.end_fill()
"""右鼻孔"""
t.penup()
t.goto(-75, 125)
t.pendown()
t.circle(3)
t.end_fill()
"""头部"""
t.penup()
t.goto(-69, 167)
t.pendown()
t.color('deep pink')
t.begin_fill()
t.setheading(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -80)
t.circle(150, -20)
t.circle(60, -95)
t.setheading(145)
t.circle(-300, 19)
#勾画出右半个鼻子的轮廓,避免填充时覆盖掉
t.penup()
t.goto(-100, 100)
t.pendown()
t.setheading(-30)
segment = 0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
segment= segment+0.08
t.left(3)
t.forward(segment)
else:
segment= segment-0.08
t.left(3)
t.forward(segment)
t.color('pink')
t.end_fill()
"""耳朵"""
t.penup()
t.goto(0, 160)
t.pendown()
t.color('deep pink')
t.begin_fill()
t.setheading(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.color('pink')
t.end_fill()
t.penup()
t.setheading(90)
t.forward(-12)
t.setheading(0)
t.forward(30)
t.pendown()
t.color('deep pink')
t.begin_fill()
t.setheading(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.color('pink')
t.end_fill()
"""眼睛"""
t.color('deep pink')
t.penup()
t.setheading(0)
t.goto(-10, 125)
t.pendown()
t.begin_fill()
t.circle(12)
t.color('white')
t.end_fill()
t.penup()
t.setheading(-270)
t.goto(-10, 135)
t.pendown()
t.color('black')
t.begin_fill()
t.circle(4)
t.end_fill()
t.color('deep pink')
t.penup()
t.setheading(0)
t.goto(50, 100)
t.pendown()
t.begin_fill()
t.circle(12)
t.color('white')
t.end_fill()
t.penup()
t.setheading(-270)
t.goto(50, 110)
t.pendown()
t.color('black')
t.begin_fill()
t.circle(4)
t.end_fill()
"""嘴巴"""
t.penup()
t.goto(-20, 30)
t.pendown()
t.color('deep pink')
t.setheading(-80)
t.circle(35, 120)
"""body函数"""
t.color('red')
t.penup()
t.goto(-32, -8)
t.pendown()
t.begin_fill()
t.setheading(-130)
t.circle(100, 10)
t.circle(300, 30)
#身体底边
t.setheading(0)
t.forward(230)
#身体右边的曲线
t.setheading(90)
t.circle(300, 30)
t.circle(100, 3)
#把脸上的下巴颏画出来,避免填充时覆盖掉
t.setheading(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.color('red')
t.end_fill()
"""手"""
t.color('pink')
# 左手的中间手指
t.penup()
t.goto(-56, -45)
t.pendown()
t.setheading(-160)
t.circle(300, 15)
#通过一个弧形表示左手另外两根手指
t.penup()
t.setheading(90)
t.forward(15)
t.setheading(0)
t.pendown()
t.setheading(-10)
t.circle(-20, 90)
# 右手的中间手指
t.penup()
t.setheading(90)
t.forward(30)
t.setheading(0)
t.forward(237)
t.pendown()
t.setheading(-20)
t.circle(-300, 15)
#通过一个弧形表示另外两根手指
t.penup()
t.setheading(90)
t.forward(20)
t.setheading(0)
t.pendown()
t.setheading(-170)
t.circle(20, 90)
"""腿"""
t.pensize(10)
t.color('deep pink')
t.penup()
t.goto(2,-177)
t.pendown()
t.setheading(-90)
t.forward(40)
t.setheading(-180)
#左脚
t.color("black")
t.pensize(15)
t.forward(20)
#右腿
t.pensize(10)
t.color('deep pink')
t.penup()
t.setheading(90)
t.forward(40)
t.setheading(0)
t.forward(90)
t.pendown()
t.setheading(-90)
t.forward(40)
t.setheading(-180)
#右脚
t.color("black")
t.pensize(15)
t.forward(20)
"""尾巴"""
t.pensize(4)
t.color('pink')
t.penup()
t.goto(148, -155)
t.pendown()
t.setheading(0)
#打卷的尾巴
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.done()
3、小黄人
代码:
import turtle as t
"""参数设置"""
t.pensize(4)
"""头部"""
t.penup()
t.color('yellow')
t.goto(-130, -70)
t.pendown()
t.begin_fill()
t.left(90)
t.forward(200)
t.circle(-80, 180)
t.forward(200)
t.circle(-80, 180)
t.end_fill()
"""头发"""
t.penup()
t.forward(200)
t.color('black')
t.goto(-110, 182)
t.pendown()
t.circle(15, 75)
t.forward(10)
t.penup()
t.goto(-100, 189)
t.pendown()
t.setheading(90)
t.circle(25, 105)
"""手"""
#左手
t.penup()
t.goto(-130, -50)
t.setheading(240)
t.color('yellow')
t.pendown()
t.pensize(6)
t.circle(155, 55)
t.pensize(10)
t.color('black')
t.circle(5)
#右手
t.penup()
t.pensize(6)
t.color('yellow')
t.goto(35, -50)
t.pendown()
t.pensize(6)
t.circle(-155, 55)
t.pensize(10)
t.color('black')
t.circle(5)
"""眼睛"""
t.penup()
t.goto(-130, 130)
t.setheading(0)
t.pendown()
t.forward(10)
t.pensize(8)
t.color('grey')
t.setheading(270)
t.begin_fill()
t.circle(35)
t.color('white')
t.end_fill()
t.color('grey')
t.penup()
t.goto(-50, 130)
t.pendown()
t.setheading(270)
t.begin_fill()
t.circle(35)
t.color('white')
t.end_fill()
t.penup()
t.goto(25, 130)
t.setheading(0)
t.pendown()
t.color('black')
t.pensize(10)
t.forward(10)
t.penup()
t.goto(-95, 130)
t.color('black')
t.setheading(270)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-25, 130)
t.setheading(270)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
"""身体"""
t.penup()
t.goto(-130, -70)
t.pendown()
t.begin_fill()
t.color('blue')
t.setheading(270)
t.circle(80, 180)
t.setheading(180)
t.forward(40)#衣服拐角
t.setheading(90)
t.forward(30)
t.setheading(45)
t.forward(50)
t.setheading(180)
t.forward(160)
t.setheading(315)
t.forward(50)
t.setheading(270)
t.forward(30)
t.setheading(180)
t.forward(25)
t.end_fill()
"""嘴巴"""
t.penup()
t.color('black')
t.goto(-90, 30)
t.pensize(4)
t.pendown()
t.setheading(330)
t.circle(80, 60)
"""腿"""
t.penup()
t.color('black')
t.goto(-90, -145)
t.setheading(270)
t.pendown()
t.pensize(5)
t.forward(25)
t.setheading(180)
t.pensize(10)
t.forward(5)
t.penup()
t.goto(-20, -145)
t.pendown()
t.pensize(5)
t.setheading(270)
t.forward(25)
t.setheading(0)
t.pensize(10)
t.forward(5)
t.done()