"""Percolate, a scrolling terminal animation by Al Sweigart.""" import random, shutil, sys, time DELAY = 0.045 # Pause between rows in seconds. WIDTH = None # None uses the terminal width. POROSITY = 0.5 # Initial chance that a cell is open. LIQUID, ROCK, PORE, DRIP = '█', '#', ' ', ':' def get_width(width=None): result = shutil.get_terminal_size()[0] if width is None else width return result - 1 if sys.platform == 'win32' else result def percolate(delay=DELAY, width=WIDTH): current_width, porosity = get_width(width), POROSITY wet = {random.randrange(current_width) for _ in range(max(1, current_width // 8))} while True: current_width = get_width(width) layer = [random.random() < porosity for _ in range(current_width)] new_wet = {x + dx for x in wet for dx in (-1, 0, 1) if 0 <= x + dx < current_width and layer[x + dx]} print(''.join(ROCK if not layer[x] else LIQUID if x in new_wet else DRIP if x in wet else PORE for x in range(current_width))) wet = new_wet or {random.randrange(current_width) for _ in range(max(1, current_width // 6))} porosity = max(0.3, min(0.85, porosity + random.uniform(-0.01, 0.01))) time.sleep(delay) try: percolate() except KeyboardInterrupt: print('Percolate, by Al Sweigart al@inventwithpython.com')