海龟绘图绘制国旗
步骤:
- 背景底色
- 大五角星
- 5个小五角星
import turtle
#1. draw_rectangle` 函数用于绘制矩形,用于国旗的背景。
def draw_rectangle(color, x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
#2. draw_star函数用于绘制五角星。
def draw_star(color, x, y, size):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
for i in range(5):
turtle.forward(size)
turtle.right(144) # 144 degrees for star shape
turtle.end_fill()
# 设置画布
turtle.setup(width=600, height=400)
turtle.speed(5)
# 绘制红色背景
draw_rectangle("red", -300, 200, 600, 400)
# 大星
draw_star("yellow", -180, 100, 80)
# 四个小星
small_star_positions = [
(-70, 130),
(-50,100),
(-50, 70),
(-70, 40)
]
for pos in small_star_positions:
draw_star("yellow", pos[0], pos[1], 20)
# 完成绘制
turtle.hideturtle()
turtle.done()