python粒子动态圣诞树代码
时间: 2023-12-27 14:24:49 浏览: 121
以下是一个使用turtle库绘制动态圣诞树的Python代码:
```python
import turtle
import random
def tree(d, s):
if d <= 0:
return
turtle.forward(s)
tree(d - 1, s * .8)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
turtle.backward(s)
def draw_tree():
turtle.speed(0)
turtle.up()
turtle.goto(0, -200)
turtle.down()
turtle.color('green')
turtle.begin_fill()
tree(7, 100)
turtle.end_fill()
def draw_leaves():
turtle.up()
for _ in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
turtle.forward(b)
turtle.left(90)
turtle.forward(a)
turtle.down()
if random.randint(0, 1) == 0:
turtle.color('tomato')
else:
turtle.color('wheat')
turtle.circle(2)
turtle.up()
turtle.backward(a)
turtle.right(90)
turtle.backward(b)
def draw_christmas():
turtle.bgcolor('black')
draw_tree()
draw_leaves()
turtle.hideturtle()
turtle.done()
draw_christmas()
```
阅读全文