"""Brownian, a scrolling terminal animation by Al Sweigart.""" import random, shutil, sys, time DELAY = 0.12 # Pause between rows in seconds. WIDTH = None # None uses the terminal width. TEMPERATURE = 1.0 # Initial particle jitter. LARGE_PARTICLES = 3 TRAIL_LENGTH = 8 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 brownian(delay=DELAY, width=WIDTH): current_width, particles, temperature = get_width(width), [], TEMPERATURE for _ in range(LARGE_PARTICLES): particles.append({'x': random.uniform(10, current_width - 10), 'large': True, 'trail': []}) for _ in range(max(10, current_width // 5)): particles.append({'x': random.uniform(0, current_width), 'large': False, 'trail': []}) while True: current_width, row = get_width(width), [' '] * get_width(width) for particle in particles: for old_x in particle['trail'][-TRAIL_LENGTH:]: index = int(old_x) % current_width if row[index] == ' ': row[index] = ',' particle['trail'].append(particle['x']); particle['trail'] = particle['trail'][-12:] particle['x'] = (particle['x'] + random.gauss(0, (0.5 if particle['large'] else 2.0) * temperature)) % current_width row[int(particle['x'])] = 'O' if particle['large'] else 'ยท' print(''.join(row)); time.sleep(delay) temperature = max(0.3, min(2.5, temperature + random.uniform(-0.02, 0.02))) try: brownian() except KeyboardInterrupt: print('Brownian, by Al Sweigart al@inventwithpython.com')