使用trutle模块绘制奥运五环
时间: 2024-03-01 21:42:29 浏览: 51
Python中使用turtle画奥运五环
5星 · 资源好评率100%
好的,让我们来绘制奥运五环。首先,我们需要导入turtle模块:
```python
import turtle
```
然后,我们可以创建一个Turtle对象,命名为"pen",并设置画笔的颜色和大小:
```python
pen = turtle.Turtle()
pen.color("blue", "yellow")
pen.pensize(10)
```
接下来,我们可以使用pen对象绘制五个圆形,每个圆形表示一个奥运五环的颜色。我们可以使用turtle.circle()方法来绘制圆形,它需要两个参数:半径和角度。我们可以通过调整半径和角度来绘制不同大小的圆形。
```python
# 绘制蓝色圆环
pen.penup()
pen.goto(-120, 0)
pen.pendown()
pen.circle(50)
# 绘制黄色圆环
pen.penup()
pen.goto(60, 60)
pen.pendown()
pen.circle(50)
# 绘制黑色圆环
pen.penup()
pen.goto(-60, 60)
pen.pendown()
pen.circle(50)
# 绘制绿色圆环
pen.penup()
pen.goto(-180, 60)
pen.pendown()
pen.circle(50)
# 绘制红色圆环
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.circle(50)
```
最后,我们需要调用turtle.done()方法来保持窗口打开,直到我们关闭它。
```python
turtle.done()
```
完整代码如下:
```python
import turtle
pen = turtle.Turtle()
pen.color("blue", "yellow")
pen.pensize(10)
# 绘制蓝色圆环
pen.penup()
pen.goto(-120, 0)
pen.pendown()
pen.circle(50)
# 绘制黄色圆环
pen.penup()
pen.goto(60, 60)
pen.pendown()
pen.circle(50)
# 绘制黑色圆环
pen.penup()
pen.goto(-60, 60)
pen.pendown()
pen.circle(50)
# 绘制绿色圆环
pen.penup()
pen.goto(-180, 60)
pen.pendown()
pen.circle(50)
# 绘制红色圆环
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.circle(50)
turtle.done()
```
阅读全文