粒子光效圣诞树代码python
时间: 2023-12-25 17:30:15 浏览: 251
以下是一个使用Python实现粒子光效圣诞树的代码示例:
```python
import random
import time
# 设置屏幕大小
screen_width = 80
screen_height = 20
# 定义树的高度和宽度
tree_height = 10
tree_width = 7
# 定义树干的高度和宽度
trunk_height = 3
trunk_width = 3
# 定义粒子的数量
num_particles = 100
# 定义粒子的符号
particle_char = "*"
# 定义粒子的颜色
particle_colors = ["\033[91m", "\033[92m", "\033[93m", "\033[94m", "\033[95m", "\033[96m"]
# 清空屏幕
def clear_screen():
print("\033c", end="")
# 绘制树
def draw_tree():
for i in range(tree_height):
spaces = " " * (screen_width // 2 - tree_width // 2 - i)
stars = "*" * (tree_width + 2 * i)
print(spaces + stars)
# 绘制树干
def draw_trunk():
for i in range(trunk_height):
spaces = " " * (screen_width // 2 - trunk_width // 2)
trunk = "*" * trunk_width
print(spaces + trunk)
# 绘制粒子
def draw_particles():
particles = []
for _ in range(num_particles):
x = random.randint(0, screen_width - 1)
y = random.randint(0, screen_height - 1)
color = random.choice(particle_colors)
particles.append((x, y, color))
while True:
clear_screen()
for particle in particles:
x, y, color = particle
print("\033[{};{}H{}{}".format(y, x, color, particle_char))
particle = (x, y + 1, color)
if y >= screen_height - 1:
particle = (x, 0, color)
particles.remove(particle)
particles.append(particle)
time.sleep(0.1)
# 主函数
def main():
clear_screen()
draw_tree()
draw_trunk()
draw_particles()
# 运行主函数
if __name__ == "__main__":
main()
```
阅读全文