1 #! /usr/local/bin/python
3 # Simulate "electrons" migrating across the screen.
4 # An optional bitmap file in can be in the background.
6 # Usage: electrons [n [bitmapfile]]
8 # n is the number of electrons to animate; default is 4, maximum 15.
10 # The bitmap file can be any X11 bitmap file (look in
11 # /usr/include/X11/bitmaps for samples); it is displayed as the
12 # background of the animation. Default is no bitmap.
14 # This uses Steen Lumholt's Tk interface
19 # The graphical interface
23 def __init__(self
, n
, bitmap
= None):
26 self
.canvas
= c
= Canvas(tk
)
28 width
, height
= tk
.getint(c
['width']), tk
.getint(c
['height'])
30 # Add background bitmap
32 self
.bitmap
= c
.create_bitmap(width
/2, height
/2,
34 'foreground': 'blue'})
37 x1
, y1
, x2
, y2
= 10,70,14,74
38 for i
in range(n
,0,-1):
39 p
= c
.create_oval(x1
, y1
, x2
, y2
,
42 y1
, y2
= y1
+2, y2
+ 2
45 def random_move(self
,n
):
46 for i
in range(1,n
+1):
50 x
= rand
.choice(range(-2,4))
51 y
= rand
.choice(range(-3,4))
54 # Run -- never returns
57 self
.random_move(self
.n
)
58 self
.tk
.mainloop() # Hang around...
64 # First argument is number of pegs, default 4
66 n
= string
.atoi(sys
.argv
[1])
70 # Second argument is bitmap file, default none
73 # Reverse meaning of leading '@' compared to Tk
74 if bitmap
[0] == '@': bitmap
= bitmap
[1:]
75 else: bitmap
= '@' + bitmap
79 # Create the graphical objects...
80 h
= Electrons(n
, bitmap
)
86 # Call main when run as script
87 if __name__
== '__main__':