如何用Python绘制圣诞树
时间: 2023-03-30 08:04:30 浏览: 99
您好,以下是用 Python 绘制圣诞树的代码:
```python
# 导入需要用到的库
import turtle
# 定义绘制圣诞树的函数
def draw_tree(t, trunk_length, height):
if height <= :
return
t.forward(trunk_length)
t.left(45)
draw_tree(t, trunk_length * .6, height - 1)
t.right(90)
draw_tree(t, trunk_length * .6, height - 1)
t.left(45)
t.backward(trunk_length)
# 创建画布和画笔
canvas = turtle.Screen()
canvas.bgcolor("white")
pen = turtle.Turtle()
pen.speed()
pen.color("green")
# 绘制圣诞树
pen.penup()
pen.goto(, -200)
pen.pendown()
draw_tree(pen, 150, 5)
# 绘制圣诞树的装饰品
pen.penup()
pen.goto(-100, 50)
pen.pendown()
pen.color("red")
pen.begin_fill()
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(100, 50)
pen.pendown()
pen.color("blue")
pen.begin_fill()
pen.circle(20)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 点击画布退出程序
canvas.exitonclick()
```
希望能帮到您!
阅读全文