使用Python画旗台上飘扬的五星红旗
时间: 2024-05-27 22:07:06 浏览: 35
飘动的红旗
4星 · 用户满意度95%
使用Python画五星红旗可以通过turtle库实现,以下是实现的代码示例:
```
import turtle
# 定义红色矩形
def draw_rectangle(x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.color('red', 'red')
turtle.begin_fill()
for i in range(2):
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.end_fill()
# 定义一个五角星
def draw_star(x, y, radius):
turtle.setpos(x, y)
# 计算五个顶点的坐标
pt1 = turtle.pos()
turtle.circle(-radius, 72)
pt2 = turtle.pos()
turtle.circle(-radius, 72)
pt3 = turtle.pos()
turtle.circle(-radius, 72)
pt4 = turtle.pos()
turtle.circle(-radius, 72)
pt5 = turtle.pos()
# 画五角星
turtle.color('yellow', 'yellow')
turtle.begin_fill()
turtle.goto(pt3)
turtle.goto(pt1)
turtle.goto(pt4)
turtle.goto(pt2)
turtle.goto(pt5)
turtle.end_fill()
# 主函数
def main():
# 设置初始位置和朝向
x, y = -270, 200
turtle.speed(10)
turtle.hideturtle()
# 画国旗主体
draw_rectangle(x, y, 540, 360)
# 画大星星
draw_star(x+60, y-30, 30)
# 画小星星
draw_star(x+180, y-60, 15)
draw_star(x+180, y-150, 15)
# 画小小星星
draw_star(x+120, y-90, 10)
draw_star(x+240, y-90, 10)
draw_star(x+300, y-120, 10)
draw_star(x+300, y-210, 10)
if __name__ == '__main__':
main()
```
阅读全文