用Python画出五星红旗
时间: 2023-11-05 13:55:29 浏览: 242
这是一个用Python绘制五星红旗的示例代码:
```python
import turtle
def draw_rectangle(x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color('red')
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
def draw_star(x, y, size):
turtle.penup()
turtle.goto(x, y)
turtle.setheading(0)
turtle.pendown()
turtle.begin_fill()
for _ in range(5):
turtle.forward(size)
turtle.right(144)
turtle.end_fill()
def draw_china_flag():
width, height = 1200, 800
turtle.setup(width, height)
turtle.title('China Flag')
# 绘制红色背景
draw_rectangle(-width/2, height/2, width, -height/2)
# 计算并绘制大星星
big_star_x, big_star_y = -width/7, height/14
star_size = height / 10
draw_star(big_star_x, big_star_y, star_size)
# 计算并绘制小星星
small_star_size = star_size / 2
gap = star_size * 2.5
for row in range(4):
for col in range(5):
x = big_star_x + gap * col + gap / 2 * (row % 2)
y = big_star_y - gap * row
draw_star(x, y, small_star_size)
turtle.hideturtle()
turtle.done()
draw_china_flag()
```
阅读全文