python用函数画一朵小花,并随机生成20朵花
时间: 2024-04-06 16:10:42 浏览: 7
以下是使用Python函数绘制一朵小花,并随机生成20朵花的代码:
```python
import turtle
import random
# 函数绘制一朵小花
def draw_flower():
turtle.speed(10)
turtle.color("red", "green")
turtle.begin_fill()
for i in range(6):
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(120)
turtle.end_fill()
# 随机生成20朵花
for i in range(20):
x = random.randint(-200, 200)
y = random.randint(-200, 200)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
draw_flower()
turtle.done()
```
运行上述代码将会在屏幕上随机生成20朵小花。每朵小花由六个三角形构成,三角形的边长为50。函数`draw_flower()`用于绘制一朵小花,函数中使用了海龟绘图库中的一些函数,如`turtle.speed()`、`turtle.color()`、`turtle.begin_fill()`、`turtle.forward()`等。在随机生成20朵花时,使用了`random`库中的`randint()`函数随机生成花的位置。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)