python制作动态圣诞树
时间: 2023-12-17 11:28:51 浏览: 79
以下是使用turtle库制作动态圣诞树的Python代码:
```python
import turtle
import random
# 设置画笔
turtle.speed(0)
turtle.hideturtle()
turtle.pensize(2)
# 定义绘制圣诞树的函数
def drawTree(x, y, size):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(90)
turtle.color("green")
turtle.begin_fill()
turtle.forward(size)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(size)
turtle.end_fill()
if size > 20:
drawTree(x + 20, y + 40, size * 0.7)
drawTree(x - 20, y + 40, size * 0.7)
drawTree(x, y + 60, size * 0.7)
# 定义绘制雪花的函数
def drawSnow():
turtle.hideturtle()
turtle.speed(0)
turtle.pencolor("white")
turtle.pensize(2)
for i in range(80):
turtle.penup()
turtle.goto(random.randint(-248,248), random.randint(-100, 248))
turtle.pendown()
petalNumber = 6
snowSize = int(random.randint(2, 10))
for j in range(petalNumber):
turtle.forward(snowSize)
turtle.backward(snowSize)
turtle.right(360 / petalNumber)
# 绘制圣诞树和雪花
drawTree(0, -200, 100)
drawSnow()
# 显示画布
turtle.done()
```
阅读全文