Rainbow
Rainbow is a simple program that shows a colorful rainbow traveling back and forth across the screen. The program makes use of the fact that when new lines of text appear, the existing text scrolls up, causing it to look like it’s moving. This program is good for beginners, and it’s similar to Project 15, “Deep Cave.”
rainbow.py
1"""Rainbow, by Al Sweigart al@inventwithpython.com
2Shows a simple rainbow animation. Press Ctrl-C to stop.
3This code is available at https://nostarch.com/big-book-small-python-programming
4Tags: tiny, artistic, bext, beginner, scrolling"""
5
6import time, sys
7
8try:
9 import bext
10except ImportError:
11 print('This program requires the bext module, which you')
12 print('can install by following the instructions at')
13 print('https://pypi.org/project/Bext/')
14 sys.exit()
15
16print('Rainbow, by Al Sweigart al@inventwithpython.com')
17print('Press Ctrl-C to stop.')
18time.sleep(3)
19
20indent = 0 # How many spaces to indent.
21indentIncreasing = True # Whether the indentation is increasing or not.
22
23try:
24 while True: # Main program loop.
25 print(' ' * indent, end='')
26 bext.fg('red')
27 print('##', end='')
28 bext.fg('yellow')
29 print('##', end='')
30 bext.fg('green')
31 print('##', end='')
32 bext.fg('blue')
33 print('##', end='')
34 bext.fg('cyan')
35 print('##', end='')
36 bext.fg('purple')
37 print('##')
38
39 if indentIncreasing:
40 # Increase the number of spaces:
41 indent = indent + 1
42 if indent == 60: # (!) Change this to 10 or 30.
43 # Change direction:
44 indentIncreasing = False
45 else:
46 # Decrease the number of spaces:
47 indent = indent - 1
48 if indent == 0:
49 # Change direction:
50 indentIncreasing = True
51
52 time.sleep(0.02) # Add a slight pause.
53except KeyboardInterrupt:
54 sys.exit() # When Ctrl-C is pressed, end the program.