turtle库绘制五星红旗代码
时间: 2024-03-24 12:34:37 浏览: 104
turtle库是Python中一个常用的绘图库,可以用来进行简单的图形绘制。下面是使用turtle库绘制五星红旗的代码:
```python
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor("red")
# 绘制大五角星
turtle.penup()
turtle.goto(-180, 60)
turtle.pendown()
turtle.begin_fill()
turtle.color("yellow")
for _ in range(5):
turtle.forward(60)
turtle.right(144)
turtle.end_fill()
# 绘制四个小五角星
turtle.penup()
turtle.goto(-120, 90)
turtle.pendown()
turtle.color("yellow")
for _ in range(4):
turtle.begin_fill()
for _ in range(5):
turtle.forward(20)
turtle.right(144)
turtle.end_fill()
turtle.penup()
turtle.forward(40)
turtle.pendown()
# 隐藏画笔
turtle.hideturtle()
# 点击关闭窗口退出程序
turtle.exitonclick()
```
相关问题
利用turtle库绘制五星红旗
可以使用以下代码绘制五星红旗:
```python
import turtle
# 设置画布大小
turtle.setup(800, 600)
# 设置画笔颜色和填充颜色
turtle.color("red", "red")
# 绘制矩形
turtle.begin_fill()
turtle.penup()
turtle.goto(-300, 200)
turtle.pendown()
turtle.forward(600)
turtle.right(90)
turtle.forward(400)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(400)
turtle.end_fill()
# 绘制大星星
turtle.color("yellow", "yellow")
turtle.begin_fill()
turtle.penup()
turtle.goto(-200, 120)
turtle.pendown()
for i in range(5):
turtle.forward(80)
turtle.right(144)
turtle.end_fill()
# 绘制四个小星星
turtle.color("yellow", "yellow")
turtle.begin_fill()
turtle.penup()
turtle.goto(-80, 160)
turtle.pendown()
for i in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
turtle.color("yellow", "yellow")
turtle.begin_fill()
turtle.penup()
turtle.goto(-60, 80)
turtle.pendown()
for i in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
turtle.color("yellow", "yellow")
turtle.begin_fill()
turtle.penup()
turtle.goto(-60, 0)
turtle.pendown()
for i in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
turtle.color("yellow", "yellow")
turtle.begin_fill()
turtle.penup()
turtle.goto(-80, -80)
turtle.pendown()
for i in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 显示绘图窗口
turtle.done()
```
运行以上代码即可绘制出五星红旗。
用python 的turtle库绘制五星红旗
好的,下面是用 Python 的 turtle 库绘制五星红旗的代码:
```python
import turtle
# 设置画布大小
screen = turtle.Screen()
screen.setup(800, 500)
# 画红色背景
turtle.penup()
turtle.goto(-400, 250)
turtle.pendown()
turtle.begin_fill()
turtle.color("red")
for i in range(2):
turtle.forward(800)
turtle.right(90)
turtle.forward(500)
turtle.right(90)
turtle.end_fill()
# 画大星星
turtle.penup()
turtle.goto(-280, 120)
turtle.pendown()
turtle.begin_fill()
turtle.color("yellow")
for i in range(5):
turtle.forward(100)
turtle.right(144)
turtle.end_fill()
# 画四个小星星
positions = [(0, 135), (-40, 85), (40, 85), (-40, 35)]
turtle.penup()
turtle.color("yellow")
for position in positions:
turtle.goto(position)
turtle.pendown()
turtle.begin_fill()
for i in range(5):
turtle.forward(20)
turtle.right(144)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 程序结束,持续显示
turtle.done()
```
运行代码后,就能看到绘制的五星红旗了。如果需要保存图片,可以使用 turtle 库提供的 `getcanvas()` 方法,将画布转换成 tkinter 的 Canvas 对象,再使用 Canvas 对象的 `postscript()` 方法保存成 PS 或 EPS 格式的图片。示例代码如下:
```python
# 保存为 EPS 格式图片
canvas = turtle.getcanvas()
canvas.postscript(file="五星红旗.eps")
```
阅读全文