blob: e1fe1cb2e5875ed230967b86aefc8d5bd7af50be [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum4450bab1994-10-11 14:57:03 +00002
Guido van Rossum50df3811994-06-28 13:52:31 +00003# Simulate "electrons" migrating across the screen.
4# An optional bitmap file in can be in the background.
5#
6# Usage: electrons [n [bitmapfile]]
7#
8# n is the number of electrons to animate; default is 4, maximum 15.
9#
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.
13
14# This uses Steen Lumholt's Tk interface
15from Tkinter import *
16
17
Guido van Rossum50df3811994-06-28 13:52:31 +000018# The graphical interface
19class Electrons:
20
21 # Create our objects
22 def __init__(self, n, bitmap = None):
23 self.n = n
24 self.tk = tk = Tk()
25 self.canvas = c = Canvas(tk)
26 c.pack()
27 width, height = tk.getint(c['width']), tk.getint(c['height'])
28
29 # Add background bitmap
30 if bitmap:
31 self.bitmap = c.create_bitmap(width/2, height/2,
Guido van Rossum89cb67b1996-07-30 18:57:18 +000032 bitmap=bitmap,
33 foreground='blue')
Guido van Rossum50df3811994-06-28 13:52:31 +000034
35 self.pieces = {}
36 x1, y1, x2, y2 = 10,70,14,74
37 for i in range(n,0,-1):
Guido van Rossum89cb67b1996-07-30 18:57:18 +000038 p = c.create_oval(x1, y1, x2, y2, fill='red')
Guido van Rossum50df3811994-06-28 13:52:31 +000039 self.pieces[i] = p
40 y1, y2 = y1 +2, y2 + 2
41 self.tk.update()
42
43 def random_move(self,n):
Guido van Rossum6c3a2cb1998-05-20 17:13:01 +000044 import random
Guido van Rossum29892d81998-05-19 21:16:10 +000045 c = self.canvas
Guido van Rossum50df3811994-06-28 13:52:31 +000046 for i in range(1,n+1):
47 p = self.pieces[i]
Guido van Rossum6c3a2cb1998-05-20 17:13:01 +000048 x = random.choice(range(-2,4))
49 y = random.choice(range(-3,4))
Guido van Rossum50df3811994-06-28 13:52:31 +000050 c.move(p, x, y)
51 self.tk.update()
Guido van Rossum89cb67b1996-07-30 18:57:18 +000052
Guido van Rossum50df3811994-06-28 13:52:31 +000053 # Run -- never returns
54 def run(self):
Guido van Rossumdac44471997-08-14 19:49:27 +000055 try:
Guido van Rossum29892d81998-05-19 21:16:10 +000056 for i in range(500):
Guido van Rossumdac44471997-08-14 19:49:27 +000057 self.random_move(self.n)
Guido van Rossumbd8341e1998-04-10 19:17:41 +000058 except TclError:
59 try:
60 self.tk.destroy()
61 except TclError:
62 pass
Guido van Rossum89cb67b1996-07-30 18:57:18 +000063
Guido van Rossum50df3811994-06-28 13:52:31 +000064
65# Main program
66def main():
67 import sys, string
68
69 # First argument is number of pegs, default 4
70 if sys.argv[1:]:
71 n = string.atoi(sys.argv[1])
72 else:
73 n = 30
74
75 # Second argument is bitmap file, default none
76 if sys.argv[2:]:
77 bitmap = sys.argv[2]
78 # Reverse meaning of leading '@' compared to Tk
79 if bitmap[0] == '@': bitmap = bitmap[1:]
80 else: bitmap = '@' + bitmap
81 else:
82 bitmap = None
83
84 # Create the graphical objects...
85 h = Electrons(n, bitmap)
86
87 # ...and run!
88 h.run()
89
90
91# Call main when run as script
92if __name__ == '__main__':
93 main()
94
95