圣诞树代码编程python可复制
时间: 2023-12-18 14:29:55 浏览: 464
以下是一个简单的Python代码,可以绘制一个圣诞树:
```python
# 导入绘图库
import turtle
# 设置画布大小
turtle.setup(800, 600)
# 设置画笔颜色和粗细
turtle.pensize(5)
turtle.pencolor("green")
# 绘制三角形
turtle.fillcolor("green")
turtle.begin_fill()
turtle.forward(200)
turtle.left(120)
turtle.forward(200)
turtle.left(120)
turtle.forward(200)
turtle.end_fill()
# 绘制树干
turtle.pencolor("brown")
turtle.fillcolor("brown")
turtle.begin_fill()
turtle.left(60)
turtle.forward(100)
turtle.right(120)
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(120)
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(120)
turtle.forward(100)
turtle.end_fill()
# 绘制装饰品
turtle.penup()
turtle.goto(-50, 50)
turtle.pendown()
turtle.pencolor("red")
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(0, 100)
turtle.pendown()
turtle.pencolor("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(50, 50)
turtle.pendown()
turtle.pencolor("blue")
turtle.fillcolor("blue")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 显示绘图窗口
turtle.done()
```
阅读全文