Python画出红树林
时间: 2023-11-05 19:02:50 浏览: 87
python画图-使用Python+turtle实现画名字.zip
以下是使用Python绘制红树林的代码演示:
```python
import turtle
# 设置画布大小和起点坐标
turtle.setup(800, 800)
turtle.penup()
turtle.goto(-300, -250)
turtle.pendown()
# 绘制树干
turtle.left(90)
turtle.forward(200)
turtle.right(90)
# 绘制树枝
def draw_branch(branch_length):
if branch_length > 10:
# 绘制右侧树枝
turtle.forward(branch_length)
turtle.right(20)
draw_branch(branch_length - 20)
# 绘制左侧树枝
turtle.left(40)
draw_branch(branch_length - 20)
turtle.right(20)
# 返回原位置
turtle.penup()
turtle.backward(branch_length)
turtle.pendown()
# 绘制树叶
def draw_leaf():
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 绘制红树林
def draw_mangrove_tree():
turtle.pensize(6)
turtle.color("brown")
draw_branch(100)
turtle.penup()
turtle.goto(-200, -50)
turtle.pendown()
turtle.color("green")
draw_leaf()
turtle.penup()
turtle.goto(-250, -100)
turtle.pendown()
draw_leaf()
turtle.penup()
turtle.goto(-150, -100)
turtle.pendown()
draw_leaf()
turtle.penup()
turtle.goto(-200, -150)
turtle.pendown()
draw_leaf()
draw_mangrove_tree()
turtle.done()
```
运行上述代码后,将会在画布上绘制出一棵红树林。
阅读全文