Advertisement
Awagner14

Matrix Falling Japanese Blue Text

Mar 7th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. import random
  2. import time
  3. import os
  4. import sys
  5.  
  6. def clear_screen():
  7. if os.name == 'posix':
  8. _ = os.system('clear')
  9. else:
  10. _ = os.system('cls')
  11.  
  12. def get_terminal_size():
  13. size = os.get_terminal_size()
  14. return size.columns, size.lines
  15.  
  16. # Automatically calculate terminal size
  17. WINDOW_WIDTH, WINDOW_HEIGHT = get_terminal_size()
  18. # Adjust as necessary to fit your terminal's character dimensions and desired appearance
  19. WINDOW_HEIGHT -= 1 # Adjusting to leave space for prompt visibility
  20.  
  21. SPEED = 0.05 # Lower is faster
  22.  
  23. def generate_color():
  24. """Generates a random color in RGB format."""
  25. emphasize_color = random.choice(['R', 'G', 'B'])
  26. if emphasize_color == 'R':
  27. color_value = random.randint(95, 255)
  28. return f'\033[38;2;{color_value};0;0m'
  29. elif emphasize_color == 'G':
  30. color_value = random.randint(95, 255)
  31. return f'\033[38;2;0;{color_value};0m'
  32. elif emphasize_color == 'B':
  33. color_value = random.randint(95, 255)
  34. return f'\033[38;2;0;0;{color_value}m'
  35.  
  36. def generate_char():
  37. return chr(random.randint(0x30A0, 0x30FF)) # Japanese characters range
  38.  
  39. def print_at_pos(x, y, char, color):
  40. sys.stdout.write(f"\033[{y};{x}H{color}{char}\033[0m")
  41.  
  42. def init_matrix():
  43. return [[' ' for _ in range(WINDOW_WIDTH)] for _ in range(WINDOW_HEIGHT)]
  44.  
  45. def update_matrix(matrix):
  46. for x in range(WINDOW_WIDTH):
  47. if random.randint(0, 10) < 2:
  48. matrix[0][x] = generate_char()
  49. else:
  50. matrix[0][x] = ' '
  51. for y in range(WINDOW_HEIGHT - 1, 0, -1):
  52. for x in range(WINDOW_WIDTH):
  53. matrix[y][x] = matrix[y-1][x]
  54.  
  55. def print_matrix(matrix):
  56. for y in range(WINDOW_HEIGHT):
  57. for x in range(WINDOW_WIDTH):
  58. char = matrix[y][x]
  59. if char != ' ':
  60. print_at_pos(x+1, y+1, char, generate_color())
  61.  
  62. def main():
  63. clear_screen()
  64. matrix = init_matrix()
  65.  
  66. try:
  67. while True:
  68. update_matrix(matrix)
  69. print_matrix(matrix)
  70. time.sleep(SPEED)
  71. except KeyboardInterrupt:
  72. clear_screen()
  73. sys.exit()
  74.  
  75. if __name__ == "__main__":
  76. main()
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement