用Python的turtle做一个国旗
时间: 2023-11-19 19:17:30 浏览: 35
好的,下面是一个简单的 Python turtle 代码,用来绘制中国国旗:
```python
import turtle
# 设置画布大小
turtle.setup(800, 600)
# 设置画笔速度
turtle.speed(10)
# 设置画笔颜色
turtle.color('red')
# 绘制矩形
turtle.begin_fill()
turtle.forward(400)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(800)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(400)
turtle.end_fill()
# 移动到正方形的左下角
turtle.penup()
turtle.goto(-200, -150)
turtle.pendown()
# 绘制正方形
turtle.color('yellow')
turtle.begin_fill()
for i in range(4):
turtle.forward(150)
turtle.right(90)
turtle.end_fill()
# 移动到星星的起始位置
turtle.penup()
turtle.goto(-125, 30)
turtle.setheading(0)
turtle.pendown()
# 绘制五角星
turtle.color('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
# 移动到下一个五角星的起始位置
turtle.penup()
turtle.goto(-50, 0)
turtle.setheading(0)
turtle.pendown()
# 绘制五角星
turtle.color('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
# 移动到下一个五角星的起始位置
turtle.penup()
turtle.goto(-125, -60)
turtle.setheading(0)
turtle.pendown()
# 绘制五角星
turtle.color('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
# 移动到下一个五角星的起始位置
turtle.penup()
turtle.goto(25, 0)
turtle.setheading(0)
turtle.pendown()
# 绘制五角星
turtle.color('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 显示画布
turtle.done()
```
运行上面的代码,会在窗口中绘制出中国国旗。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)