用Python绘制五星红旗
时间: 2025-05-05 15:08:51 浏览: 59
使用 Python 绘制五星红旗可以借助像 `turtle` 或者更专业的绘图库如 `matplotlib` 来完成。下面是一个简单的例子,采用 `turtle` 库绘制五星红旗。
```python
import turtle
def draw_rectangle(start_x, start_y, width, height):
# 前往起始位置并准备画矩形
turtle.penup()
turtle.goto(start_x, start_y)
turtle.pendown()
for i in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
def draw_star(center_x, center_y, radius):
points = [(center_x + radius * (-0.58), center_y - radius),
(center_x + radius * 0.58 , center_y - radius ),
(center_x + radius , center_y ),
(center_x + radius * 0.58 , center_y + radius ),
(center_x + radius * (-0.58), center_y + radius )]
turtle.color('yellow', 'yellow')
turtle.begin_fill()
for point in points:
turtle.penup() if points.index(point)==0 else None
turtle.goto(point[0],point[1])
turtle.pendown()
turtle.end_fill()
# 设置背景颜色为中国红色(接近大红)
turtle.bgcolor("#DE2910")
# 定义旗面尺寸及坐标系原点定位
flag_width = 300
flag_height = int(flag_width*2/3)
start_x = -(flag_width / 2)
start_y = flag_height / 2
draw_rectangle(start_x, start_y, flag_width, -flag_height)
# 主星的位置、大小设定;副星们相对于主星的位置和大小计算略作简化处理
main_star_center_x=start_x+5/6*(flag_width)*0.14
main_star_center_y=start_y-flag_height*.07-(flag_height*0.28)/2
sub_stars_centers=[
(-flag_width*.18,-flag_height*.06),(flag_width*.11,-flag_height*.06),
(flag_width*.18 ,-flag_height*.06),(flag_width*.25,-flag_height*.06)]
star_radius=flag_height//10
for sub in sub_stars_centers :
sub=sub[0]+main_star_center_x-sub[1]*0.5 , main_star_center_y+(sub[1]-main_star_center_y)+sub[0]*0.3
draw_star(main_star_center_x,main_star_center_y, star_radius )
for c in [(-flag_width *.18,flag_height*-0.06),(flag_width * .11,flag_height*(-0.06)),
(flag_width * .18,flag_height * (-0.06)),(flag_width * .25,flag_height * (-0.06))]:
cx,cy=c[0]+main_star_center_x-c[1] * 0.5 , main_star_center_y +(c[1 ]-main_star_center_y)+c[0]*0.3
draw_star(cx,cy,int(star_radius //1.5))
# 结束绘画过程,并保持窗口直到用户点击关闭按钮为止
turtle.hideturtle()
turtle.done()
```
请注意这只是一个简化的演示版本,在实际绘制过程中需要更加精确地调整星星之间的相对位置以及它们各自的倾斜角度等细节以确保完全按照国旗标准制作出来。此外,您也可以选择其他图形库比如 Pygame 等来实现这个功能。
--
阅读全文
相关推荐


















