用turtlr绘制出蓝天绿草彩虹的代码
时间: 2024-03-22 12:42:18 浏览: 56
好的,下面是Python利用turtle库绘制蓝天绿草彩虹的代码示例:
```python
import turtle
# 创建一个画笔对象
pen = turtle.Turtle()
# 设置画笔的速度和宽度
pen.speed(10)
pen.pensize(5)
# 绘制蓝天
pen.penup()
pen.setpos(-300, 200)
pen.pendown()
pen.color('blue')
pen.begin_fill()
pen.forward(600)
pen.right(90)
pen.forward(400)
pen.right(90)
pen.forward(600)
pen.right(90)
pen.forward(400)
pen.end_fill()
# 绘制绿草
pen.penup()
pen.setpos(-300, -200)
pen.pendown()
pen.color('green')
pen.begin_fill()
pen.forward(600)
pen.right(90)
pen.forward(200)
pen.right(90)
pen.forward(600)
pen.right(90)
pen.forward(200)
pen.end_fill()
# 定义彩虹的颜色和半径
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
radius = 250
# 绘制彩虹
for i in range(6):
pen.penup()
pen.setpos(-50 * i, 0)
pen.pendown()
pen.color(colors[i])
pen.circle(radius + 50 * i, 180)
# 隐藏画笔
pen.hideturtle()
# 等待用户退出
turtle.done()
```
以上代码可以在Python环境下直接运行,绘制出一个蓝天绿草彩虹的图形。
阅读全文