| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from Tkinter import * |
| 5 |
import random |
| 6 |
import sys |
| 7 |
|
| 8 |
WIDTH = 400 |
| 9 |
HEIGHT = 300 |
| 10 |
SIGMA = 10 |
| 11 |
BUZZ = 2 |
| 12 |
RADIUS = 2 |
| 13 |
LAMBDA = 10 |
| 14 |
FILL = 'red' |
| 15 |
|
| 16 |
stop = 0 |
| 17 |
root = None |
| 18 |
|
| 19 |
def particle(canvas): |
| 20 |
r = RADIUS |
| 21 |
x = random.gauss(WIDTH/2.0, SIGMA) |
| 22 |
y = random.gauss(HEIGHT/2.0, SIGMA) |
| 23 |
p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL) |
| 24 |
while not stop: |
| 25 |
dx = random.gauss(0, BUZZ) |
| 26 |
dy = random.gauss(0, BUZZ) |
| 27 |
try: |
| 28 |
canvas.move(p, dx, dy) |
| 29 |
except TclError: |
| 30 |
break |
| 31 |
else: |
| 32 |
yield None |
| 33 |
|
| 34 |
def move(particle): |
| 35 |
particle.next() |
| 36 |
dt = random.expovariate(LAMBDA) |
| 37 |
root.after(int(dt*1000), move, particle) |
| 38 |
|
| 39 |
def main(): |
| 40 |
global root, stop |
| 41 |
root = Tk() |
| 42 |
canvas = Canvas(root, width=WIDTH, height=HEIGHT) |
| 43 |
canvas.pack(fill='both', expand=1) |
| 44 |
np = 30 |
| 45 |
if sys.argv[1:]: |
| 46 |
np = int(sys.argv[1]) |
| 47 |
for i in range(np): |
| 48 |
move(particle(canvas)) |
| 49 |
try: |
| 50 |
root.mainloop() |
| 51 |
finally: |
| 52 |
stop = 1 |
| 53 |
|
| 54 |
if __name__ == '__main__': |
| 55 |
main() |