当前位置:   article > 正文

python源码-代码雨_python代码雨源码

python代码雨源码

 不废话,直接上源码

  1. import time
  2. import random
  3. import sys
  4. WIDTH = 80
  5. HEIGHT = 25
  6. CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
  7. 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)]
  8. class CodeRain:
  9. def __init__(self, width, height):
  10. self.width = width
  11. self.height = height
  12. self.matrix = [[' ' for y in range(height)] for x in range(width)]
  13. self.code_matrix = [[' ' for y in range(height)] for x in range(width)]
  14. self.codes = []
  15. self.code_count = 50
  16. def init_codes(self):
  17. for i in range(self.code_count):
  18. x = random.randint(0, self.width - 1)
  19. y = random.randint(0, self.height - 1)
  20. color = random.choice(COLORS)
  21. code = random.choice(CHARS)
  22. self.codes.append((x, y, code, color))
  23. def draw_codes(self):
  24. for x, y, code, color in self.codes:
  25. self.matrix[x][y] = color + code + '\033[0m'
  26. def update_codes(self):
  27. for i in range(self.code_count):
  28. x, y, code, color = self.codes[i]
  29. if y >= self.height - 1:
  30. y = random.randint(0, 1)
  31. else:
  32. y += 1
  33. self.codes[i] = (x, y, code, color)
  34. def draw(self):
  35. for y in range(self.height):
  36. line = ''
  37. for x in range(self.width):
  38. line += self.matrix[x][y]
  39. self.matrix[x][y] = self.code_matrix[x][y]
  40. print(line, end='\n' if y == self.height - 1 else '\r')
  41. def run(self):
  42. self.init_codes()
  43. while True:
  44. self.draw_codes()
  45. self.draw()
  46. self.update_codes()
  47. time.sleep(0.1)
  48. if __name__ == '__main__':
  49. sys.stdout.write('\033[2J')
  50. sys.stdout.write('\033[?25l')
  51. CodeRain(WIDTH, HEIGHT).run()
  52. sys.stdout.write('\033[?25h')

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号