Knock out final bugs caused by misparenting. Runs all tests as the demo applet.
[python-bulletml.git] / bulletml-runner
1 #!/usr/bin/env python
2
3 import sys
4
5 import pygame
6
7 import bulletml
8
9 def main(argv):
10 pygame.display.init()
11 screen = pygame.display.set_mode([600, 600], pygame.DOUBLEBUF)
12 bullet = pygame.Surface([3, 3])
13 bullet.fill([255, 0, 0])
14 filename = argv[0]
15 doc = bulletml.BulletML(open(filename, "rU"))
16 clock = pygame.time.Clock()
17 target = bulletml.Bullet()
18
19 while True:
20 source = bulletml.Bullet.FromDoc(
21 doc, x=150, y=150, target=target, rank=0.5)
22
23 active = set([source])
24 source.vanished = True
25 print filename
26 print " Loaded %d top-level actions." % len(source.actions)
27 frames = 0
28 total = 0
29 paused = False
30
31 while active:
32 go = False
33
34 for event in pygame.event.get():
35 if event.type == pygame.QUIT:
36 raise SystemExit
37 elif event.type == pygame.KEYDOWN:
38 if event.key == pygame.K_SPACE:
39 paused ^= True
40 elif event.key == pygame.K_RIGHT:
41 go = True
42 target.x, target.y = pygame.mouse.get_pos()
43 target.x /= 2
44 target.y = (screen.get_height() - target.y) / 2
45
46 if not paused or go:
47
48 for obj in list(active):
49 new = obj.step()
50 total += len(new)
51 active.update(new)
52 if (obj.finished
53 or not (-50 < obj.x < 350)
54 or not (-50 < obj.y < 350)):
55 active.remove(obj)
56
57 frames += 1
58 if frames % 100 == 0:
59 print " Processing: %04d: %d bullets, %d active." % (
60 frames, total, len(active))
61
62 screen.fill([0, 0, 0])
63 for obj in active:
64 try:
65 x, y = obj.x, obj.y
66 except AttributeError:
67 pass
68 else:
69 if not obj.vanished:
70 x *= 2
71 y *= 2
72 x -= 1
73 y -= 1
74 screen.blit(bullet, [x, screen.get_height() - y])
75 pygame.display.flip()
76
77 clock.tick(60)
78
79 for event in pygame.event.get():
80 if event.type == pygame.QUIT:
81 raise SystemExit
82 elif event.type == pygame.KEYDOWN:
83 if event.key == pygame.K_SPACE:
84 paused ^= True
85 elif event.key == pygame.K_RIGHT:
86 go = True
87
88 print " Finished: %04d: %d bullets." % (frames, total)
89
90 if __name__ == "__main__":
91 main(sys.argv[1:])