小编之前的博文中用到了一些数据插图,多为代码或者Excel处理实现的。今天来个纯的,看看我们从字幕雨中能看到Matrix中的动静不。
import cursesimport randomimport timeimport keyboard
def main(stdscr): try: curses.curs_set(0) curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
height, width = stdscr.getmaxyx()
even_columns_List = list(range(0, width, 2))
drops = [0]*width
while True: column = random.choice(even_columns_List) matrix_char = chr(random.randint(33, 126)) stdscr.addch(drops[column], column, matrix_char, curses.color_pair(1)) stdscr.refresh() if drops[column] >= height-2 or random.choice([True] + [False]*18): for k in range(height-1): stdscr.addch(k, column, ' ') drops[column] = 0
stdscr.refresh() else: drops[column] += 1 if keyboard.is_pressed('q'): break time.sleep(0.002) except Exception as e: print(e)
if __name__ == "__main__": curses.wrapper(main)

curses插件(其他的插件类似操作),在python所在环境的终端下,输入:
pip install windows-curses
假设将代码保存为Matrix_Digital_Rain.py,仍然在终端方式的python环境下,输入以下指令执行该代码将在当前终端窗口看到循环运行字幕雨:
python Matrix_Digital_Rain.py
大家完全可以根据自己的喜好修改这个代码让字幕以不同的方式下...

