Advertisement
Guest User

new

a guest
May 25th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # Author: Joao S. O. Bueno
  4. # GPL. v3.0
  5.  
  6. MAX_CASCADES = 600
  7. MAX_COLS = 20
  8. FRAME_DELAY = 0.03
  9.  
  10. MAX_SPEED  = 5
  11.  
  12. import shutil, sys, time
  13. from random import choice, randrange, paretovariate
  14.  
  15. CSI = "\x1b["
  16. pr = lambda command: print("\x1b[", command, sep="", end="")
  17. getchars = lambda start, end: [chr(i) for i in range(start, end)]
  18.  
  19. black, green, white = "30", "32", "37"
  20.  
  21. latin = getchars(0x30, 0x80)
  22. greek = getchars(0x390, 0x3d0)
  23. hebrew = getchars(0x5d0, 0x5eb)
  24. cyrillic = getchars(0x400, 0x50)
  25.  
  26. chars= latin + greek + hebrew + cyrillic
  27.  
  28. def pareto(limit):
  29.     scale = lines // 2
  30.     number = (paretovariate(1.16) - 1) * scale
  31.     return max(0, limit - number)
  32.  
  33. def init():
  34.     global cols, lines
  35.     cols, lines = shutil.get_terminal_size()
  36.     pr("?25l")  # Hides cursor
  37.     pr("s")  # Saves cursor position
  38.  
  39. def end():
  40.     pr("m")   # reset attributes
  41.     pr("2J")  # clear screen
  42.     pr("u")  # Restores cursor position
  43.     pr("?25h")  # Show cursor
  44.  
  45. def print_at(char, x, y, color="", bright="0"):
  46.     pr("%d;%df" % (y, x))
  47.     pr(bright + ";" + color + "m")
  48.     print(char, end="", flush=True)
  49.  
  50. def update_line(speed, counter, line):
  51.     counter += 1
  52.     if counter >= speed:
  53.         line += 1
  54.         counter = 0
  55.     return counter, line
  56.  
  57. def cascade(col):
  58.     speed = randrange(1, MAX_SPEED)
  59.     espeed = randrange(1, MAX_SPEED)
  60.     line = counter = ecounter = 0
  61.     oldline = eline = -1
  62.     erasing = False
  63.     bright = "1"
  64.     limit = pareto(lines)
  65.     while True:
  66.         counter, line = update_line(speed , counter, line)
  67.         if randrange(10 * speed) < 1:
  68.             bright = "0"
  69.         if line > 1 and line <= limit and oldline != line:
  70.             print_at(choice(chars),col, line-1, green, bright)
  71.         if line < limit:
  72.             print_at(choice(chars),col, line, white, "1")
  73.         if erasing:
  74.             ecounter, eline = update_line(espeed, ecounter, eline)
  75.             print_at(" ",col, eline, black)
  76.         else:
  77.             erasing = randrange(line + 1) > (lines / 2)
  78.             eline = 0
  79.         yield None
  80.         oldline = line
  81.         if eline >= limit:
  82.             print_at(" ", col, oldline, black)
  83.             break
  84.  
  85. def main():
  86.     cascading = set()
  87.     added_new = True
  88.     while True:
  89.         while add_new(cascading): pass
  90.         stopped = iterate(cascading)
  91.         sys.stdout.flush()
  92.         cascading.difference_update(stopped)
  93.         time.sleep(FRAME_DELAY)
  94.  
  95. def add_new(cascading):
  96.     if randrange(MAX_CASCADES + 1) > len(cascading):
  97.         col = randrange(cols)
  98.         for i in range(randrange(MAX_COLS)):
  99.             cascading.add(cascade((col + i) % cols))
  100.         return True
  101.     return False
  102.  
  103. def iterate(cascading):
  104.     stopped = set()
  105.     for c in cascading:
  106.         try:
  107.             next(c)
  108.         except StopIteration:
  109.             stopped.add(c)
  110.     return stopped
  111.  
  112. def doit():
  113.     try:
  114.         init()
  115.         main()
  116.     except KeyboardInterrupt:
  117.         pass
  118.     finally:
  119.         end()
  120.  
  121. if __name__=="__main__":
  122.     doit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement