"""Suspense, a scrolling terminal animation by Al Sweigart.""" import random import shutil import sys import time # The number of character columns. None uses the terminal width. WIDTH = None # The minimum and maximum number of rows in the buildup. BUILDUP_MIN = 20 BUILDUP_MAX = 40 # The minimum and maximum number of rows in the release. RELEASE_MIN = 10 RELEASE_MAX = 20 # Characters used as tension builds and releases. BUILDUP_CHARS = '.-:=+*#%@' RELEASE_CHARS = '#@%*+=-:. ' def get_width(width=None): result = shutil.get_terminal_size()[0] if width is None else width if sys.platform == 'win32': result -= 1 return result def suspense(width=WIDTH): while True: cycle_length = random.randint(BUILDUP_MIN, BUILDUP_MAX) for index in range(cycle_length): current_width = get_width(width) progress = index / cycle_length center = current_width // 2 spread = int((1 - progress) * (current_width // 2)) row = [' '] * current_width char = BUILDUP_CHARS[int(progress * (len(BUILDUP_CHARS) - 1))] for x in range(max(0, center - spread), min(current_width, center + spread + 1)): if random.random() < 0.3 + progress * 0.7: row[x] = char print(''.join(row)) time.sleep(0.02 * (1 + progress * 8)) release_length = random.randint(RELEASE_MIN, RELEASE_MAX) for index in range(release_length): current_width = get_width(width) progress = index / release_length row = [' '] * current_width char = RELEASE_CHARS[int(progress * (len(RELEASE_CHARS) - 1))] center = current_width // 2 spread = int(progress * (current_width // 2)) for x in range(max(0, center - spread), min(current_width, center + spread + 1)): row[x] = char print(''.join(row)) time.sleep(0.01 + progress * 0.02) for _ in range(3): print(' ' * get_width(width)) time.sleep(0.15) try: suspense() except KeyboardInterrupt: print('Suspense, by Al Sweigart al@inventwithpython.com')