```python
import random
import time
设置屏幕大小
WIDTH = 80
HEIGHT = 20
创建流星
def create_meteor():
x = random.randint(0, WIDTH)
y = random.randint(0, HEIGHT // 2)
speed = random.randint(1, 3)
return {'x': x, 'y': y, 'speed': speed}
移动流星
def move_meteor(meteor):
meteor['y'] += meteor['speed']
if meteor['y'] > HEIGHT:
meteor['x'] = random.randint(0, WIDTH)
meteor['y'] = random.randint(0, HEIGHT // 2)
meteor['speed'] = random.randint(1, 3)
绘制画面
def draw_screen(meteors):
screen = [[' ' for _ in range(WIDTH)] for _ in range(HEIGHT)]
for meteor in meteors:
x = meteor['x']
y = meteor['y']
screen[y][x] = '*'
for row in screen:
print(''.join(row))
主循环
def main():
meteors = [create_meteor() for _ in range(10)]
while meteors:
for meteor in meteors:
move_meteor(meteor)
draw_screen(meteors)
time.sleep(0.1)
if __name__ == "__main__":
main()
```
这个代码示例创建了一个简单的命令行流星雨程序,其中流星从屏幕顶部随机位置开始下落,速度随机。当流星到达屏幕底部时,它会重新出现在屏幕顶部的随机位置。程序会持续运行,直到所有流星都到达屏幕底部。