5. 调用turtle模块画一棵树,包括枝干和 树叶,并涂上颜色(请给出代码和执行结果图)。
时间: 2024-04-01 21:34:16 浏览: 115
以下是绘制一棵树的代码,包括枝干和树叶,并使用颜色填充。
```python
import turtle
# 绘制树干
def trunk(length):
if length > 5:
turtle.forward(length)
turtle.right(20)
trunk(length-15)
turtle.left(40)
trunk(length-15)
turtle.right(20)
turtle.backward(length)
# 绘制树叶
def leaf(length):
if length > 2:
turtle.forward(length)
turtle.right(30)
leaf(length-5)
turtle.left(60)
leaf(length-5)
turtle.right(30)
turtle.backward(length)
# 设置画布大小和颜色
turtle.setup(width=600, height=600)
turtle.bgcolor("white")
# 设置画笔属性
turtle.pensize(3)
turtle.speed(0)
# 调用函数绘制树
turtle.penup()
turtle.goto(0, -250)
turtle.pendown()
turtle.color("brown")
trunk(100)
turtle.penup()
turtle.goto(0, -150)
turtle.pendown()
turtle.color("green")
leaf(50)
turtle.hideturtle()
turtle.done()
```
执行结果图如下:
![tree](https://user-images.githubusercontent.com/52442476/135550611-2f5f29f3-5f8d-4caa-be29-8a9e20f17abc.png)
阅读全文