圣诞树粒子代码python
时间: 2023-12-19 18:31:58 浏览: 222
圣诞树代码编程python
以下是一个用Python绘制圣诞树的粒子代码示例:
```python
import turtle as t
import random
def tree(d, s):
if d <= 0:
return t.forward(s)
tree(d - 1, s * .8)
t.right(120)
tree(d - 3, s * .5)
t.right(120)
tree(d - 3, s * .5)
t.right(120)
t.backward(s)
def draw_tree():
t.speed(0)
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('green')
tree(7, 100)
t.mainloop()
def draw_falling_leaves():
t.speed(0)
t.hideturtle()
t.up()
t.setpos(0, -200)
t.down()
for i in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
if random.randint(0, 1) == 0:
t.color('tomato')
else:
t.color('wheat')
t.circle(2)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
t.mainloop()
draw_tree()
draw_falling_leaves()
```
阅读全文