用python语言写一个树的生成要求五颜六色
时间: 2024-06-06 08:07:41 浏览: 86
用python来画一棵树
以下是一个用Python语言生成五颜六色树的例子:
```python
import turtle
from random import randint
def tree(branchLen,t):
if branchLen > 5:
t.pensize(branchLen/10)
t.color(randint(0,255),randint(0,255),randint(0,255))
t.forward(branchLen)
angle = randint(20,40)
t.right(angle)
tree(branchLen-15,t)
t.left(2*angle)
tree(branchLen-15,t)
t.right(angle)
t.backward(branchLen)
def main():
t = turtle.Turtle()
myWin = turtle.Screen()
t.left(90)
t.up()
t.backward(100)
t.down()
t.speed(0)
tree(75,t)
myWin.exitonclick()
main()
```
运行上述代码,将会生成一棵彩色的树,如下图所示:
![彩色树图](https://cdn.luogu.com.cn/upload/image_hosting/eha3i3zz.png)
阅读全文