高仿模拟练习,turtle 绘图与编程应用,答案点击按钮展开
画三个水平 120x40 的矩形,颜色依次红/黄/蓝,用 begin_fill/end_fill 填充。
import turtle
t = turtle.Turtle()
colors = ["red", "yellow", "blue"]
w, h = 120, 40
for i in range(3):
t.____(colors[i]) # 设置颜色(笔画色和填充色同色)
t.begin_fill()
# 画矩形 4 边
for j in range(2):
t.forward(w); t.left(90); t.forward(h); t.left(90)
t.____() # 结束填充
# 抬笔下移 h 画下一块
t.penup(); t.goto(0, -(i+1)*h); t.pendown()
turtle.done()
color
end_fill
t.color(c) 一次设置画笔和填充色;begin_fill 包裹绘制,end_fill 完成填充。循环 3 次每次 goto(0, -(i+1)*h) 垂直下移一行。