# 🎆 终端烟花动画 15 行

37 阅读1分钟

🎆 终端烟花动画 15 行

纯标准库,Python 3 直接跑;复制即看烟火!

微信图片_20251014151033_10_20.jpg

import random, time, os, math
rows, cols = os.get_terminal_size()
def fade(c): return max(0, c - 2)
while True:
    x, y, ticks = random.randint(0, cols), rows, 0
    trail = [(x, y, 9)]          # 9=白强度
    while y > 0:
        os.system('clear||cls'); ticks += 1
        trail.append((x, y := y - 2, 9))
        for (px, py, c) in trail:
            print(f"\033[{py};{px}H\033[38;2;{c*25};{c*25};{c*25}m★")
        time.sleep(0.03)
    # 爆炸粒子
    particles = [(x, y, 9, random.uniform(0, 6.28), random.uniform(0.5, 1.5)) for _ in range(70)]
    for t in range(25):
        os.system('clear||cls')
        for (px, py, c, a, s) in particles[:]:
            c = fade(c); if c <= 0: continue
            px = int(px + math.cos(a) * s); py = int(py + math.sin(a) * s + 0.15)
            print(f"\033[{py};{px}H\033[38;2;{255-c*20};{255-c*10};{255}m✨")
        time.sleep(0.04)

Ctrl+C 退出;调整 70 改变粒子数,修改 fade(c) 控制消散速度。