"""Guilloche, a scrolling terminal animation by Al Sweigart.""" import math import random import shutil import sys import time # The pause between rows in seconds; smaller values scroll faster. DELAY = 0.03 # The number of character columns. None uses the terminal width. WIDTH = None # Characters ordered from low to high curve intensity. CHARS = ' .,:;+*oO#@' # The number of overlapping curves. NUM_CURVES = 3 # Random ranges for the spirograph outer radius, inner radius, and pen distance. OUTER_RADIUS_MIN = 3.0 OUTER_RADIUS_MAX = 8.0 INNER_RADIUS_MIN = 1.0 INNER_RADIUS_MAX = 4.0 PEN_DISTANCE_MIN = 1.0 PEN_DISTANCE_MAX = 5.0 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 guilloche(delay=DELAY, width=WIDTH): outer_radius = random.uniform(OUTER_RADIUS_MIN, OUTER_RADIUS_MAX) inner_radius = random.uniform(INNER_RADIUS_MIN, INNER_RADIUS_MAX) pen_distance = random.uniform(PEN_DISTANCE_MIN, PEN_DISTANCE_MAX) t = 0.0 while True: current_width = get_width(width) row = [] for x in range(current_width): value = 0.0 for curve in range(NUM_CURVES): offset = curve * 1.7 x_angle = x * 0.05 + offset y_angle = t * 0.02 + offset cx = ((outer_radius - inner_radius) * math.cos(x_angle) + pen_distance * math.cos((outer_radius - inner_radius) / inner_radius * x_angle)) cy = ((outer_radius - inner_radius) * math.sin(y_angle) + pen_distance * math.sin((outer_radius - inner_radius) / inner_radius * y_angle)) distance = math.sqrt(cx * cx + cy * cy) / (outer_radius + pen_distance) wave = math.sin(distance * 8.0 + x * 0.1 + t * 0.03) value += (wave + 1.0) / 2.0 value = max(0.0, min(1.0, value / NUM_CURVES)) row.append(CHARS[int(value * (len(CHARS) - 1))]) print(''.join(row)) t += 1 time.sleep(delay) if random.random() < 0.002: outer_radius = random.uniform(OUTER_RADIUS_MIN, OUTER_RADIUS_MAX) inner_radius = random.uniform(INNER_RADIUS_MIN, INNER_RADIUS_MAX) pen_distance = random.uniform(PEN_DISTANCE_MIN, PEN_DISTANCE_MAX) try: guilloche() except KeyboardInterrupt: print('Guilloche, by Al Sweigart al@inventwithpython.com')