Python的 turtle 库是一个简单而强大的绘图工具,特别适合编程初学者学习计算机图形学基础。通过控制屏幕上的"海龟"移动,我们可以创建各种有趣的图形。本文将详细介绍如何使用 turtle 库绘制一个可爱的小黄人形象。
小贴士
在学习本教程前,建议先熟悉Python基础语法和Turtle库的基本命令,如移动、旋转和画笔控制等。
准备工作
首先,我们需要导入 turtle 库并进行基本设置:
import turtle
t = turtle.Turtle()
wn = turtle.Screen()
turtle.colormode(255) # 使用RGB颜色模式
t.hideturtle() # 隐藏海龟图标
t.speed(10) # 设置最快绘图速度
绘制小黄人的身体
小黄人的身体是一个黄色的椭圆形,我们通过组合直线和圆弧来绘制:
t.penup()
t.pensize(4)
t.goto(100, 0)
t.pendown()
t.left(90)
t.color((0, 0, 0), (255, 255, 0)) # 黑色轮廓,黄色填充
# 开始填充黄色身体
t.begin_fill()
t.forward(200)
t.circle(100, 180)
t.forward(200)
t.circle(100, 180)
t.end_fill()
这段代码创建了小黄人标志性的黄色身体,使用黑色边框和黄色填充。
绘制面部特征
小黄人的面部包括两只大眼睛和一张微笑的嘴:
绘制眼睛
# 右眼眼眶
t.pensize(12)
t.penup()
t.goto(-100, 200)
t.pendown()
t.right(100)
t.circle(500, 23)
# 右眼眼球
t.pensize(3)
t.penup()
t.goto(0, 200)
t.pendown()
t.seth(270)
t.color("black", "white")
t.begin_fill()
t.circle(30)
t.end_fill()
# 右眼瞳孔
t.penup()
t.goto(15, 200)
t.pendown()
t.color("black", "black")
t.begin_fill()
t.circle(15)
t.end_fill()
# 右眼高光
t.penup()
t.goto(35, 205)
t.color("black", "white")
t.begin_fill()
t.circle(5)
t.end_fill()
# 左眼绘制(与右眼对称)
t.penup()
t.goto(0, 200)
t.pendown()
t.seth(90)
t.color("black", "white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(-15, 200)
t.pendown()
t.color("black", "black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(-35, 205)
t.color("black", "white")
t.begin_fill()
t.circle(5)
t.end_fill()
绘制嘴巴
# 微笑的嘴
t.penup()
t.goto(-20, 100)
t.pendown()
t.seth(270)
t.color("black", "white")
t.begin_fill()
t.circle(20, 180)
t.left(90)
t.forward(40)
t.end_fill()
绘制服装
小黄人经典的蓝色背带裤:
# 裤子主体
t.penup()
t.goto(-100, 0)
t.pendown()
t.seth(0)
t.color("black", "blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100, 0)
t.circle(100, 180)
t.end_fill()
# 左裤腰带
t.penup()
t.goto(-70, 20)
t.pendown()
t.color("black", "blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70, 30)
t.dot()
# 右裤腰带(对称)
t.penup()
t.goto(70, 20)
t.pendown()
t.color("black", "blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(70, 30)
t.dot()
绘制四肢
小黄人的四肢包括黄色的手臂和黑色的鞋子:
# 左手
t.penup()
t.goto(-100, 50)
t.pendown()
t.seth(225)
t.color("black", "yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
# 右手(对称)
t.penup()
t.goto(100, 50)
t.pendown()
t.seth(315)
t.color("black", "yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()
# 双脚
t.penup()
t.goto(4, -100)
t.pendown()
t.seth(270)
t.color("black", "black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10, 180)
t.circle(400, 2)
t.seth(90)
t.forward(20)
t.goto(4, -100)
t.end_fill()
t.penup()
t.goto(-4, -100)
t.pendown()
t.seth(270)
t.color("black", "black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10, -225)
t.circle(400, -3)
t.seth(90)
t.forward(21)
t.goto(-4, -100)
t.end_fill()
添加细节
最后,我们为小黄人添加一些细节,如头顶的头发和服装装饰:
# 头顶头发
t.penup()
t.goto(2, 300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100, 40)
t.end_fill()
t.penup()
t.goto(2, 300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100, 40)
t.end_fill()
# 服装装饰细节
t.penup()
t.goto(0, -20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10, 180)
t.right(90)
t.circle(10, 180)
t.forward(20)
t.end_fill()
t.penup()
t.color("black")
t.goto(-100, -20)
t.pendown()
t.circle(30, 90)
t.penup()
t.goto(100, -20)
t.pendown()
t.circle(30, -90)
完成效果
通过以上步骤,我们成功地使用Python的 turtle 库绘制了一个可爱的小黄人。整个过程展示了如何通过简单的几何图形组合创建复杂的图像,同时也展示了 turtle 库的基本用法:
- 控制海龟移动和转向
- 设置画笔大小和颜色
- 填充封闭区域
- 绘制基本形状(直线、圆弧等)
扩展练习
你可以通过调整代码中的参数(如颜色、大小、位置)来创建不同风格的小黄人,或者尝试添加更多的细节和表情,让你的小黄人更加生动有趣。
turtle 库不仅适合绘制这样的卡通形象,也是学习编程逻辑和计算机图形学基础的绝佳工具。