Sine Message

Tags: tiny, artistic

This program displays a message of the user’s choice in a wavy pattern as the text scrolls up. It accomplishes this effect with math.sin(), which implements the trigonometric sine wave function. But even if you don’t understand the math, this program is rather short and easy to copy.

sine_message.py
 1"""Sine Message, by Al Sweigart al@inventwithpython.com
 2Create a sine-wavy message.
 3This code is available at https://nostarch.com/big-book-small-python-programming
 4Tags: tiny, artistic"""
 5
 6import math, shutil, sys, time
 7
 8# Get the size of the terminal window:
 9WIDTH, HEIGHT = shutil.get_terminal_size()
10# We can't print to the last column on Windows without it adding a
11# newline automatically, so reduce the width by one:
12WIDTH -= 1
13
14print('Sine Message, by Al Sweigart al@inventwithpython.com')
15print('(Press Ctrl-C to quit.)')
16print()
17print('What message do you want to display? (Max', WIDTH // 2, 'chars.)')
18while True:
19    message = input('> ')
20    if 1 <= len(message) <= (WIDTH // 2):
21        break
22    print('Message must be 1 to', WIDTH // 2, 'characters long.')
23
24
25step = 0.0  # The "step" determines how far into the sine wave we are.
26# Sine goes from -1.0 to 1.0, so we need to change it by a multiplier:
27multiplier = (WIDTH - len(message)) / 2
28try:
29    while True:  # Main program loop.
30        sinOfStep = math.sin(step)
31        padding = ' ' * int((sinOfStep + 1) * multiplier)
32        print(padding + message)
33        time.sleep(0.1)
34        step += 0.25  # (!) Try changing this to 0.1 or 0.5.
35except KeyboardInterrupt:
36    sys.exit()  # When Ctrl-C is pressed, end the program.

https://inventwithpython.com/bigbookpython/project67.html