效果如下
初次尝试画表情,主要是方向的控制和画椭圆函数控制好弧度
import turtle as t
'''
x=goto方法x坐标
y=goto方法y坐标
redis 圆的半径
color=填充颜色
'''
# 画脸
def face(x, y, radius, color='orange'):
# 画笔颜色
t.pencolor(color)
# 开始填充颜色
t.begin_fill()
# 抬起画笔
t.penup()
t.goto(x, y)
# 放下画笔
t.pendown()
# 填充颜色
t.fillcolor('yellow')
# 画圆 参数 int =半径
t.circle(radius)
t.end_fill()
'''
x=goto方法x坐标
y=goto方法y坐标
heading=画笔头的方向角度
radius 圆的半径
extend = 画圆的多少 例:180 就是画一半
color=填充颜色
'''
# 眉毛
def eyebrow(x, y, heading, radius, extend, color='brown'):
t.pencolor(color)
# 抬起画笔
t.penup()
t.goto(x, y)
# 设置画笔方向
t.seth(heading)
t.pendown()
t.circle(radius, extend)
'''
x=goto方法x坐标
y=goto方法y坐标
radius 圆的半径
color=填充颜色
'''
# 画眼睛
def eye(x, y, redis, color='brown'):
t.penup()
t.goto(x, y)
# 开始填充
t.begin_fill()
t.circle(redis)
# 设置填充的颜色
t.fillcolor(color)
# 结束填充
t.end_fill()
# 嘴巴
def mouth():
t.begin_fill()
t.fillcolor('white')
t.seth(145)
t.pendown()
t.circle(35, 140)
t.penup()
t.goto(-90, -40)
t.pendown()
t.seth(-30)
t.circle(180, 65)
t.circle(-35, 155)
t.seth(-132)
t.circle(-200, 97)
t.end_fill()
'''
x=goto方法x坐标
y=goto方法y坐标
sety = 笔头y轴移动距离
'''
# 牙齿
def tooth(x, y, sety):
t.penup()
t.pensize(7)
t.goto(x, y)
t.pendown()
t.sety(sety)
'''
x : int起始点的横坐标.
y : int 起始点的纵坐标.
theta : int椭圆长轴与水平方向的夹角.
length : int椭圆长轴的长度(不建议太大),0 - 1 即可,实际长度为该值的 800 倍左右.
size : int画笔的粗细程度,默认为 1.
color : str画笔的颜色,默认为黑色.
'''
# 椭圆
def ellipse(x, y, theta, length, size=1, color='pink'):
t.penup()
t.goto(x, y)
t.begin_fill()
t.fillcolor(color)
t.setheading(theta + 270)
t.pensize(size)
t.pencolor(color)
t.pendown()
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a += length
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a -= length
t.lt(3)
t.fd(a)
t.penup()
t.end_fill()
if __name__ == '__main__':
# 笔的大小
t.pensize(5)
# 速度
t.speed(1000)
# 脸
face(0, -200, 200)
# 椭圆
ellipse(-180, -2, 1, 0.1)
ellipse(100, -2, 1, 0.1)
# 两个眉毛
eyebrow(-80, 50, 90, 40, 180)
eyebrow(150, 50, 90, 40, 180)
# 两个眼睛
eye(-130, 20, 15)
eye(90, 20, 15)
# 调整一下
t.penup()
t.goto(-90, -40)
# 嘴巴
mouth()
# 牙齿
tooth(-90, -40, -120)
tooth(-40, -59, -134)
tooth(5, -70, -145)
tooth(60, -59, -135)
tooth(100, -35, -120)
# 停止画笔 窗口不动
t.hideturtle()
t.done()
萌新第一次使用python,可能函数定义上不是特别简洁,希望大家可以讨论提供其它思路
关于ellipse椭圆函数
需满足以下条件:
转动度数与循环最大值需满足: t.lt(y)中的y * range(x)里的x=360
range(x) x需是4的倍数,可以四等分
比如改变一下数据:
import turtle as t
t.penup()
t.begin_fill()
t.fillcolor('red')
t.pensize(4)
t.pencolor('blue')
t.pendown()
a = 0.1
for i in range(60):
if 0 <= i < 15 or 30 <= i < 45:
a += 0.2
t.lt(6) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a -= 0.2
t.lt(6)
t.fd(a)
t.penup()
t.end_fill()
t.done()
这里满足 60 * 6 = 360
15 * 4 = 60
当满足这两个条件时就会画圈
当然,这里的圈是椭圆