
1. 导入库
python
from turtle import * # 导入turtle库所有功能(绘图工具)
from time import sleep # 导入sleep函数(用于延迟,但代码中未实际使用)
2. 核心函数解析
(1)go_to(x, y):移动画笔位置
python
def go_to(x, y):
up() # 抬起画笔(不绘制)
goto(x, y) # 移动到坐标(x, y)
down() # 放下画笔(准备绘制)
- 作用:快速移动画笔到指定位置,避免移动过程中留下痕迹。
(2)head(x, y, r):绘制头部和身体
python
def head(x,y,r):
go_to(x,y) # 移动到头部起始点
speed(1) # 绘图速度(1-10递增,0为最快)
circle(r) # 绘制圆形头部(半径r)
leg(x,y) # 调用leg()函数绘制腿部
- 逻辑:先画一个圆作为头部,然后调用
leg()继续绘制身体的其他部分。
(3)leg(x, y):绘制腿部
python
def leg(x,y):
right(90) # 右转90度(向下)
forward(180) # 画身体主干(长度180)
right(30) # 右转30度
forward(100) # 画右腿
left(120) # 左转120度
go_to(x,y-180) # 移动到身体底部
forward(100) # 画左腿
right(120) # 右转120度
forward(100) # 延伸左腿
left(120) # 左转120度
hand(x,y) # 调用hand()函数绘制手臂
- 逻辑:以头部底部为起点,向下画身体主干,再分左右绘制两条腿,最后调用
hand()画手臂。
(4)hand(x, y):绘制手臂
python
def hand(x,y):
go_to(x,y-60) # 移动到手臂起始点(身体中间)
forward(100) # 画右臂
left(60) # 左转60度
forward(100) # 延伸右臂
go_to(x, y - 90) # 移动到左侧手臂起始点
right(60) # 右转60度
forward(100) # 画左臂
right(60) # 右转60度
forward(100) # 延伸左臂
left(60) # 左转60度
eye(x,y) # 调用eye()函数绘制眼睛
- 逻辑:以身体中间为起点,分左右
(5)eye(x, y):绘制眼睛
python
def eye(x,y):
go_to(x-50,y+130) # 移动到左眼位置
right(90) # 右转90度(向下)
forward(50) # 画左眼(竖线)
go_to(x+40,y+130) # 移动到右眼位置
forward(50) # 画右眼(竖线)
left(90) # 左转90度(复位方向)
- 逻辑:在头部两侧各画一条竖线作为眼睛。
(6)爱心绘制相关函数
python
def big_Circle(size): # 绘制大弧线(爱心外侧)
speed(20)
for i in range(150): # 循环150次,每次移动size距离并右转0.3度
forward(size)
right(0.3)
def line(size): # 绘制直线(爱心中间)
speed(1)
forward(51*size) # 长度为51*size
def small_Circle(size): # 绘制小弧线(爱心内侧)
speed(10)
for i in range(210): # 循环210次,每次移动size距离并右转0.786度
forward(size)
right(0.786)
def heart(x, y, size): # 组合成爱心
go_to(x, y)
left(150)
begin_fill() # 开始填充颜色
line(size) # 画中间直线
big_Circle(size) # 画右侧大弧线
small_Circle(size) # 画右侧小弧线
left(120) # 转向左侧
small_Circle(size) # 画左侧小弧线
big_Circle(size) # 画左侧大弧线
line(size) # 画左侧直线(闭合爱心)
end_fill() # 结束填充(用粉色填充)
- 逻辑:通过直线和不同曲率的弧线组合,绘制一个填充粉色的爱心。
(7)main():主函数(程序入口)
python
def main():
pensize(2) # 画笔粗细为2
color('red', 'pink') # 画笔颜色红色,填充颜色粉色
head(-120, 100, 100) # 绘制头部(坐标(-120,100),半径100)
heart(250, -80, 1) # 绘制爱心(坐标(250,-80),大小1)
go_to(200, -300) # 移动到文字位置
# 写入文字:"To: 史上最好的小鱼~"
write("To: 史上最好的小鱼~", move=True, align="left", font=("楷体", 20, "normal"))
done() # 保持绘图窗口
main() # 执行主函数
- 作用:设置绘图参数(颜色、粗细),依次调用各绘图函数,最后显示文字并保持窗口。
3. 整体效果
运行代码后,会绘制出:
- 一个简笔画人物(圆形头部 + 身体 + 四肢 + 竖线眼睛)
- 一颗粉色爱心
- 底部文字 "To: 史上最好的小鱼~"
4. 可优化点
sleep函数导入后未使用,可删除。- 函数调用嵌套过深(
head→leg→hand→eye),可改为平铺调用更清晰。 - 眼睛绘制为竖线,可改为圆形更形象。
- 人物比例可调整(如头部过大、四肢过短)。
代码
python
from turtle import *
from time import sleep
def go_to(x, y):
up()
goto(x, y)
down()
def head(x,y,r):
go_to(x,y)
speed(1)
circle(r)
leg(x,y)
def leg(x,y):
right(90)
forward(180)
right(30)
forward(100)
left(120)
go_to(x,y-180)
forward(100)
right(120)
forward(100)
left(120)
hand(x,y)
def hand(x,y):
go_to(x,y-60)
forward(100)
left(60)
forward(100)
go_to(x, y - 90)
right(60)
forward(100)
right(60)
forward(100)
left(60)
eye(x,y)
def eye(x,y):
go_to(x-50,y+130)
right(90)
forward(50)
go_to(x+40,y+130)
forward(50)
left(90)
def big_Circle(size):
speed(20)
for i in range(150):
forward(size)
right(0.3)
def line(size):
speed(1)
forward(51*size)
def small_Circle(size):
speed(10)
for i in range(210):
forward(size)
right(0.786)
def heart(x, y, size):
go_to(x, y)
left(150)
begin_fill()
line(size)
big_Circle(size)
small_Circle(size)
left(120)
small_Circle(size)
big_Circle(size)
line(size)
end_fill()
def main():
pensize(2)
color('red', 'pink')
head(-120, 100, 100)
heart(250, -80, 1)
go_to(200, -300)
write("To: 史上最好的小鱼~", move=True, align="left", font=("楷体", 20, "normal"))
done()
main()