赞
踩
不废话,直接上源码:
- import time
- import random
- import sys
-
- WIDTH = 80
- HEIGHT = 25
-
- CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
- COLORS = ['\033[1;%dm' % i for i in range(31, 37)] + ['\033[1;%d;%dm' % (i, j) for i in range(41, 47) for j in range(31, 37)]
-
- class CodeRain:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.matrix = [[' ' for y in range(height)] for x in range(width)]
- self.code_matrix = [[' ' for y in range(height)] for x in range(width)]
- self.codes = []
- self.code_count = 50
-
- def init_codes(self):
- for i in range(self.code_count):
- x = random.randint(0, self.width - 1)
- y = random.randint(0, self.height - 1)
- color = random.choice(COLORS)
- code = random.choice(CHARS)
- self.codes.append((x, y, code, color))
-
- def draw_codes(self):
- for x, y, code, color in self.codes:
- self.matrix[x][y] = color + code + '\033[0m'
-
- def update_codes(self):
- for i in range(self.code_count):
- x, y, code, color = self.codes[i]
- if y >= self.height - 1:
- y = random.randint(0, 1)
- else:
- y += 1
- self.codes[i] = (x, y, code, color)
-
- def draw(self):
- for y in range(self.height):
- line = ''
- for x in range(self.width):
- line += self.matrix[x][y]
- self.matrix[x][y] = self.code_matrix[x][y]
- print(line, end='\n' if y == self.height - 1 else '\r')
-
- def run(self):
- self.init_codes()
- while True:
- self.draw_codes()
- self.draw()
- self.update_codes()
- time.sleep(0.1)
-
- if __name__ == '__main__':
- sys.stdout.write('\033[2J')
- sys.stdout.write('\033[?25l')
- CodeRain(WIDTH, HEIGHT).run()
- sys.stdout.write('\033[?25h')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。