前面几节课我们用了很多颜色,但都是简单设置。这节课系统学习 turtle 的颜色填充——边框色、填充色、渐变色、RGB颜色,让图案更漂亮!
一、两个颜色,一幅画
| 命令 | 作用 | 比喻 |
t.pencolor("red") | 设置边框/线条颜色 | 笔的颜色 |
t.fillcolor("gold") | 设置填充颜色 | 颜料的颜色 |
t.color("red","gold") | 同时设置边框和填充 | 两支笔一起换 |
import turtle
t = turtle.Turtle()
t.speed(5)
# 画一个蓝色边框、金色填充的圆
t.pencolor("royalblue")
t.fillcolor("gold")
t.begin_fill()
t.circle(80)
t.end_fill()
# 用 color() 同时设置
t.penup()
t.goto(0, -50)
t.pendown()
t.color("red", "pink")
t.begin_fill()
t.circle(30)
t.end_fill()
turtle.done()二、begin_fill / end_fill 填色三步曲
记住这个万能公式:
t.fillcolor("颜色") # 第1步:选好填充色
t.begin_fill() # 第2步:开始记录形状
...(画形状)...
t.end_fill() # 第3步:填满!
重要:begin_fill 和 end_fill 之间的路径必须是封闭的(起点=终点),否则填色会出错!
三、实战:彩色三角形队列 🎯
import turtle
t = turtle.Turtle()
t.speed(5)
colors = ["red", "gold", "green", "royalblue", "purple"]
for i in range(len(colors)):
t.fillcolor(colors[i])
t.begin_fill()
t.forward(50) # 往前画一边
t.left(120)
t.forward(50) # 画第二条边
t.left(120)
t.forward(50) # 画第三条边,回到起点
t.end_fill()
turtle.done()每个三角形独立填色,互不干扰!🔺🔻🔺🔻🔺
四、实战:彩色房子 🏠
import turtle
t = turtle.Turtle()
t.speed(5)
t.pensize(2)
# === 外墙(矩形)===
t.color("navajowhite", "wheat")
t.begin_fill()
t.goto(150, 0)
t.goto(150, 100)
t.goto(0, 100)
t.goto(0, 0)
t.end_fill()
# === 屋顶(三角形)===
t.color("firebrick", "tomato")
t.begin_fill()
t.goto(75, 160)
t.goto(0, 100)
t.goto(150, 100)
t.end_fill()
# === 门 ===
t.color("saddlebrown", "chocolate")
t.begin_fill()
t.goto(60, 0)
t.goto(60, 50)
t.goto(90, 50)
t.goto(90, 0)
t.end_fill()
# === 窗户 ===
t.color("steelblue", "lightskyblue")
t.begin_fill()
t.goto(110, 50)
t.goto(110, 80)
t.goto(140, 80)
t.goto(140, 50)
t.end_fill()
turtle.done()五、实战:渐变色圆环 🌈
import turtle
t = turtle.Turtle()
t.speed(0)
t.pensize(3)
for i in range(50):
# 颜色从红色渐变到蓝色
r = int(255 - i * 5) # 255 → 5
b = int(i * 5) # 0 → 245
g = int(122 + i * 2) # 122 → 240(先增后减)
if g > 255:
g = 255
color = f"#{r:02x}{g:02x}{b:02x}"
t.pencolor(color)
t.circle(10 + i * 4) # 半径逐渐增大
turtle.done()每画一圈颜色变一点,彩虹效果就出来了!🌈
六、实战:彩色风车 🎡
import turtle
import math
t = turtle.Turtle()
t.speed(5)
colors = ["red", "orange", "gold", "green", "royalblue", "purple"]
for i in range(12):
angle = i * 30
rad = math.radians(angle)
t.fillcolor(colors[i % len(colors)])
t.begin_fill()
# 三角形的三个顶点
x1 = 100 * math.cos(rad)
y1 = 100 * math.sin(rad)
x2 = 100 * math.cos(rad + math.radians(120))
y2 = 100 * math.sin(rad + math.radians(120))
t.goto(0, 0)
t.goto(x1, y1)
t.goto(x2, y2)
t.end_fill()
turtle.done()用数学公式算出每个三角形的顶点,画出12瓣彩色风车!🎡
七、常用颜色对照表
| 英文名 | 中文 | 英文名 | 中文 |
| red | 红色 | royalblue | 宝蓝色 |
| gold | 金色 | purple | 紫色 |
| green | 绿色 | orange | 橙色 |
| pink | 粉色 | tomato | 番茄红 |
| wheat | 小麦色 | navajowhite | 奶白色 |
| chocolate | 巧克力色 | saddlebrown | 深棕色 |
八、小挑战
-
- 🟢 简单:画3个不同颜色的圆形,每个独立填色
-
- 🟡 中等:画一个彩色圆形(彩虹渐变,至少5个颜色带)
-
- 🔴 困难:画一只彩色蝴蝶(两片对称的彩色翅膀 + 身体)
-
九、下期预告
填充技术已掌握!下节课我们来玩动画——让海龟动起来,画出有趣的动态图案!
下期:《turtle第8课:让海龟动起来(动画基础)》
有问题欢迎留言!