python绘制美国国旗
时间: 2023-11-04 14:59:47 浏览: 202
好的,下面是Python代码实现美国国旗的绘制:
```python
import turtle
# 设置画布大小和起点
screen = turtle.Screen()
screen.setup(700, 500)
start_x = -250
start_y = 150
# 绘制红色和白色条纹
stripe_height = 20
stripe_width = 300
turtle.color('red', 'red')
turtle.begin_fill()
turtle.penup()
turtle.goto(start_x, start_y)
turtle.pendown()
turtle.setheading(0)
turtle.forward(stripe_width)
turtle.setheading(-90)
turtle.forward(stripe_height)
turtle.setheading(180)
turtle.forward(stripe_width)
turtle.setheading(90)
turtle.forward(stripe_height)
turtle.end_fill()
start_y -= stripe_height
turtle.color('white', 'white')
turtle.begin_fill()
turtle.penup()
turtle.goto(start_x, start_y)
turtle.pendown()
turtle.setheading(0)
turtle.forward(stripe_width)
turtle.setheading(-90)
turtle.forward(stripe_height)
turtle.setheading(180)
turtle.forward(stripe_width)
turtle.setheading(90)
turtle.forward(stripe_height)
turtle.end_fill()
start_y -= stripe_height
# 绘制蓝色背景和星星
turtle.color('navy', 'navy')
turtle.begin_fill()
turtle.penup()
turtle.goto(start_x, start_y)
turtle.pendown()
turtle.setheading(0)
turtle.forward(420)
turtle.setheading(-90)
turtle.forward(280)
turtle.setheading(180)
turtle.forward(420)
turtle.setheading(90)
turtle.forward(280)
turtle.end_fill()
turtle.color('white', 'white')
turtle.begin_fill()
turtle.penup()
turtle.goto(start_x + 30, start_y - 30)
turtle.pendown()
star_size = 20
turtle.setheading(0)
for i in range(5):
turtle.forward(star_size)
turtle.right(144)
turtle.penup()
turtle.goto(start_x + 60, start_y - 60)
turtle.pendown()
for i in range(4):
turtle.forward(star_size)
turtle.right(144)
turtle.penup()
turtle.goto(start_x + 30, start_y - 70)
turtle.pendown()
for i in range(4):
turtle.forward(star_size)
turtle.right(144)
turtle.penup()
turtle.goto(start_x, start_y - 110)
turtle.pendown()
for i in range(3):
turtle.forward(star_size)
turtle.right(120)
turtle.penup()
turtle.goto(start_x + 15, start_y - 140)
turtle.pendown()
for i in range(3):
turtle.forward(star_size)
turtle.right(120)
turtle.penup()
turtle.goto(start_x + 45, start_y - 140)
turtle.pendown()
for i in range(3):
turtle.forward(star_size)
turtle.right(120)
turtle.penup()
turtle.goto(start_x + 60, start_y - 110)
turtle.pendown()
for i in range(3):
turtle.forward(star_size)
turtle.right(120)
turtle.hideturtle()
turtle.done()
```
此时运行代码,即可看到绘制出了美国国旗:
![美国国旗图片](https://i.imgur.com/umt0WZt.png)
阅读全文