以下是一个简单的Python代码,用于模拟流星雨效果。

import random
import time

num_of_meteors = 20 # 流星数量
width = 70 # 屏幕宽度
height = 20 # 屏幕高度

# 初始化屏幕
screen = [" "] * width * height

# 循环生成流星
while True:
    # 生成一颗流星
    x = random.randint(0, width - 1)
    y = random.randint(0, height - 1)
    length = random.randint(3, 6)
    for i in range(length):
        if y-i < 0:
            break
        # 在屏幕上绘制流星
        screen[x + (y - i) * width] = "*"
        time.sleep(0.05)
    # 显示屏幕
    for i in range(height):
        row = "".join(screen[i * width:(i + 1) * width])
        print(row)
    time.sleep(0.1)
    # 清空屏幕
    screen = [" "] * width * height

你可以根据需要调整流星数量、屏幕宽度、高度和流星长度等参数。运行代码后,你将会看到屏幕上出现随机数量的流星,营造出一个流星雨的效果。

希望这段代码可以帮助到你!

更多推荐

Python流星雨代码