Python 控制台烟花动画代码示例
以下是一个简单的 Python 代码示例,用于在控制台上显示烟花效果:
import random
import time
def explode_firework():
colors = ['red', 'green', 'blue', 'yellow', 'magenta', 'cyan']
for _ in range(50):
color = random.choice(colors)
x = random.randint(1, 80)
y = random.randint(1, 24)
print(f'�33[{y};{x}H�33[48;5;{color}m �33[0m', end='')
time.sleep(0.1)
def launch_firework():
for _ in range(10):
x = random.randint(1, 80)
y = random.randint(1, 24)
print(f'�33[{y};{x}H*', end='')
time.sleep(0.5)
print(f'�33[{y};{x}H ', end='')
explode_firework()
# 主程序
while True:
launch_firework()
time.sleep(1)
请注意,上述代码是在控制台上模拟烟花效果,可能无法在某些 IDE 或环境中正常运行。您可以尝试将代码复制到 Python 环境中(如 Python 解释器或 Jupyter Notebook)并运行它,以查看效果。
原文地址: https://www.cveoy.top/t/topic/cbHp 著作权归作者所有。请勿转载和采集!