python编程turtle画草地
时间: 2023-07-03 17:13:27 浏览: 323
好的,以下是用Python编写的turtle画草地的代码:
```python
import turtle
import random
turtle.setup(800, 800)
turtle.speed(0)
turtle.bgcolor('sky blue')
def draw_grass(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color('green')
turtle.begin_fill()
turtle.forward(20)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(10)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(10)
turtle.end_fill()
while True:
x = random.randint(-400, 400)
y = random.randint(-400, -200)
draw_grass(x, y)
```
运行这段代码,你就能看到一个天蓝色的画布上,随机生成的绿色草地的效果。这个程序使用了Python的turtle模块来绘制图形,函数`draw_grass`用来绘制一株草,通过传入参数`x`和`y`来确定草的位置。草的形状比较复杂,需要用到多个`forward`和`right`或`left`命令,颜色使用了绿色,并且使用了`turtle.begin_fill()`和`turtle.end_fill()`命令来填充颜色。最后,通过不断生成随机数来实现草的随机位置。
阅读全文