帮我写python的代码实现画国旗
时间: 2023-07-28 21:59:27 浏览: 116
python画国旗代码
好的,以下是一个简单的 Python 代码实现画中国国旗的例子:
```python
import turtle
# 画矩形
def draw_rectangle(x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
turtle.color('red')
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
# 画五角星
def draw_star(x, y, radius):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
turtle.color('yellow')
turtle.right(72)
for i in range(5):
turtle.forward(radius)
turtle.right(144)
turtle.forward(radius)
turtle.right(72)
turtle.end_fill()
# 画国旗
def draw_flag():
turtle.speed(0)
turtle.hideturtle()
# 画大矩形
draw_rectangle(-300, 150, 600, 400)
# 画小矩形
draw_rectangle(-300, 150, 200, 133)
# 画五颗大星星
positions = [(-170, 80), (-100, 100), (-100, 30), (-70, 0), (-10, 0)]
for position in positions:
x, y = position
draw_star(x, y, 30)
# 画四颗小星星
positions = [(-140, 55), (-40, 55), (-90, 25), (-40, -5)]
for position in positions:
x, y = position
draw_star(x, y, 15)
# 主函数
def main():
draw_flag()
turtle.done()
if __name__ == '__main__':
main()
```
运行代码后,会弹出一个窗口,显示出画好的中国国旗。你可以尝试修改代码来画出不同的国旗。
阅读全文