如何使用Python画国旗
时间: 2024-03-28 17:29:09 浏览: 47
你好,以下是使用 Python 绘制中国国旗的代码示例:
```python
import turtle
# 设置画布大小
turtle.setup(800, 600)
# 设置画笔颜色和大小
turtle.pensize(3)
turtle.pencolor("red")
# 绘制红色背景
turtle.begin_fill()
turtle.penup()
turtle.goto(-400, 300)
turtle.pendown()
turtle.forward(800)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(800)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.end_fill()
# 绘制大星星
turtle.pencolor("yellow")
turtle.fillcolor("yellow")
turtle.penup()
turtle.goto(-220, 130)
turtle.pendown()
turtle.begin_fill()
for _ in range(5):
turtle.forward(80)
turtle.right(144)
turtle.end_fill()
# 绘制小星星
turtle.penup()
turtle.goto(-100, 200)
turtle.pendown()
turtle.begin_fill()
for _ in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
turtle.penup()
turtle.goto(-70, 120)
turtle.pendown()
turtle.begin_fill()
for _ in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
turtle.penup()
turtle.goto(-70, 60)
turtle.pendown()
turtle.begin_fill()
for _ in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
turtle.begin_fill()
for _ in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 显示画布
turtle.mainloop()
```
运行代码后,会弹出一个窗口,显示绘制的国旗。可以根据需要调整画布大小和星星的位置和大小等参数。
希望对你有帮助!
阅读全文