# Copyright (c) 1996 CNRI. # Not quite as smooth as the Java version, but much smaller... # (And with rivet, it has adequate speed!) from Tkinter import * import string from whrandom import random class Starfield: def __init__(self, master, width=400, height=100, numstars=50): self.width = width self.height = height self.canvas = Canvas(master, width=width, height=height, background='black') self.canvas.pack() self.stars = [] for i in range(numstars): x = int(random()*width) y = int(random()*height) z = 1 + int(random()*10) c = 35 + z*20 color = "#%02x%02x%02x" % (c, c, c) tag = self.canvas.create_oval(x, y, x+4, y+4, fill=color) self.stars.append((tag, z, [x])) self.update() def update(self): # Hack to test if we still exist try: self.canvas['width'] except: return # Keeping track of x this way is much faster than bbox... for tag, z, xlist in self.stars: x = xlist[0] self.canvas.move(tag, z, 0) x = x + z if x > self.width: self.canvas.move(tag, -self.width, 0) x = x - self.width xlist[0] = x self.canvas.after(10, self.update) if __name__ == '__main__': # For stand-alone usage root = Tk() starfield = Starfield(root) root.mainloop()