nickm.com > classes > cms workshop i, fall 2007

The Screen Saver

5 September 2007

Programming a "Terminal Saver" in Python

A Simple, Complete Program

We'll start from this simple program and elaborate it to do different things:

from sys import stdout
spaces = 1
while True:
    stdout.write(':')
    stdout.write(' ' * spaces)
    spaces = spaces + 1
    if spaces >= 100:
        spaces = 1

Note that this will have a different appearance when running on a different size terminal window, and it will run at different speeds depending upon what else the machine is doing.

Try modifying the program to print something besides ":" — it doesn't have to be a single character long. Try changing "100" to "10," then to "1000." Try using a different function besides incrementing by 1 to increase the value of spaces — for instance, multiplication by two. Try changing the program to count up to 100 and then back down to 1.

(There is an easier way to print output, using "print". But this adds a space at the end of the output, supposedly as a convenience. To avoid this, the program above uses a slightly more intricate but better-behaved way of printing output.)

Other Interesting Code Snippets

from random import choice
words = ['thinking','across','media','forms','theoretical','domains','cultural','contexts','and','historical','periods']
print choice(words)

This code can be used in the terminal saver above to create a different effect. Try plugging it in.

from time import sleep
while True:
    print '...'
    sleep(1)

Modify this small program using what you've learned from the terminal saver above. Instead of printing three dots, try printing one the first time, two the second time, three the third time, and so on. Have the number of dots reset to 1 after 60 have been printed.

Three Terminal Savers to Try

from random import random
from sys import stdout
p = .5
while True:
    if random() < .001:
        p = random()
    stdout.write([' ','.'][random() < p])
from random import *
from sys import stdout
while True:
    stdout.write(choice([' ','/']) * int(gauss(0,100)))
from sys import stdout
from time import sleep
while True:
    for a in range(0,9) + range(7,0,-1):
        stdout.write((' ' * (2 **a)) + 'I')
        stdout.flush()
        sleep(.05)

More on Python

A great list of links to Python tutorials, including some for non-programmers.

Programming an OS X Screen Saver

We don’t have Xcode installed in 26-139, so there's no easy way to try this during class time. When you have access to the Macs in GAMBIT, you can visit one of the following pages and follow the instructions to type in the code for an OS X screen saver in Objective C:

http://www.mactech.com/articles/mactech/Vol.21/21.06/SaveOurScreens/index.html

http://www.mactech.com/articles/mactech/Vol.20/20.06/ScreenSaversInCocoa/index.html

Working with Xcode and Objective C can be more frustrating than illuminating at this stage. If you find dealing with the minutiae of real screen saver development to be too much, work some more on Python terminal savers instead. You will be making pretty images on the screen just like the "real" screen saver does.

If you do want to check this out, notice that Xcode provides an option for creating a new screen saver project. All you have to do is fill in approximately one method with code. (There are a few other lines to be added here and there.) Once you have either screen saver compiled and working, you can try to make it simpler to see if you understand it. For the first one, for instance, instead of having rectangles of random color, edit the code so that you get medium gray ones. Instead of drawing rectangles, draw squares. For either one, see if you can replace some of the uses of randomness with fixed values or deterministic functions that are pleasing visually.

Having done this, you should be able to convince yourself that you can hack on "real" programs, the sort that are actually used in your comptuer all the time. You can learn from going through one of these pages and copying over a pre-written screen saver how to write a screen saver from scratch, if you like.