python画五星红旗代码
时间: 2023-10-08 21:04:50 浏览: 94
以下是 Python 画五星红旗的代码:
```python
import turtle
# 画矩形
def draw_rectangle(x, y, width, height, color):
turtle.color(color)
turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.begin_fill()
for i in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
# 画五角星
def draw_star(x, y, radius, color):
turtle.color(color)
turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.begin_fill()
turtle.right(18)
for i in range(5):
turtle.forward(radius)
turtle.right(144)
turtle.forward(radius)
turtle.right(72 - 18)
turtle.end_fill()
# 画五星红旗
def draw_china_flag():
turtle.speed(5)
turtle.hideturtle()
draw_rectangle(-300, 200, 600, 400, 'red')
draw_rectangle(-300, 133, 600, 66, 'yellow')
x, y = -250, 170
for i in range(5):
draw_star(x, y, 30, 'yellow')
x += 100
if i == 1:
x += 33
y -= 28
elif i == 3:
x -= 33
y -= 28
if __name__ == '__main__':
draw_china_flag()
turtle.done()
```
运行代码后,会弹出一个窗口,显示画出的五星红旗。