Advertisement
Guest User

new

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