鲜花飘屏代码python
时间: 2023-10-11 13:07:15 浏览: 91
这里是一个简单的 Python 鲜花飘屏代码:
```python
import random
import curses
# 初始化屏幕
screen = curses.initscr()
# 关闭光标显示
curses.curs_set(0)
# 获取屏幕尺寸
screen_height, screen_width = screen.getmaxyx()
# 鲜花字符
flower_chars = ['❀', '✿', '❁', '☀', '☁', '☂', '☃']
# 生成花朵,每朵花随机位置、速度和字符
flowers = []
for i in range(20):
x = random.randint(0, screen_width - 1)
y = random.randint(0, screen_height - 1)
speed = random.randint(1, 5)
char = random.choice(flower_chars)
flowers.append([x, y, speed, char])
while True:
# 清空屏幕
screen.erase()
# 移动花朵
for flower in flowers:
x, y, speed, char = flower
y += speed
if y > screen_height:
y = 0
x = random.randint(0, screen_width - 1)
speed = random.randint(1, 5)
char = random.choice(flower_chars)
flower[0] = x
flower[1] = y
flower[2] = speed
flower[3] = char
# 绘制花朵
for flower in flowers:
screen.addstr(int(flower[1]), int(flower[0]), flower[3])
# 刷新屏幕
screen.refresh()
# 等待一段时间,控制花朵移动速度
curses.napms(50)
```
注:代码中使用了 Unicode 字符,需要保证终端支持 Unicode。
阅读全文