Psyco-enable the test runner. Fix a copy/paste error.
[python-bulletml.git] / bulletml-runner
1 #!/usr/bin/env python
2
3 import os
4 import sys
5 import time
6
7 import pygame
8
9 import bulletml
10
11 try:
12 import psyco
13 except ImportError:
14 pass
15 else:
16 psyco.full()
17
18 def main(argv):
19 if not argv:
20 raise SystemExit("Usage: %s filename ..." % sys.argv[0])
21
22 pygame.display.init()
23 screen = pygame.display.set_mode([600, 600], pygame.DOUBLEBUF)
24 bullet = pygame.Surface([3, 3])
25 bullet.fill([255, 0, 0])
26 clock = pygame.time.Clock()
27 target = bulletml.Bullet()
28
29 file_idx = 0
30
31 while True:
32 filename = argv[file_idx % len(argv)]
33 doc = bulletml.BulletML.FromDocument(open(filename, "rU"))
34 actions = [act([], 0.5) for act in doc.top]
35 source = bulletml.Bullet(
36 x=150, y=150, target=target, actions=actions, rank=0.5)
37
38 active = set([source])
39 source.vanished = True
40 print filename
41 print " Loaded %d top-level actions." % len(source._actions)
42 frames = 0
43 total = 0
44 paused = False
45 newfile = False
46
47 pygame.display.set_caption(os.path.basename(filename))
48
49 while active and not newfile:
50 go = False
51
52 for event in pygame.event.get():
53 if event.type == pygame.QUIT:
54 raise SystemExit
55 elif event.type == pygame.KEYDOWN:
56 if event.key == pygame.K_SPACE:
57 paused ^= True
58 elif event.key == pygame.K_RIGHT:
59 go = True
60 elif event.key == pygame.K_PAGEUP:
61 file_idx -= 1
62 newfile = True
63 elif event.key == pygame.K_PAGEDOWN:
64 file_idx += 1
65 newfile = True
66 elif event.key == pygame.K_RETURN:
67 newfile = True
68 elif event.key == pygame.K_s:
69 actions = [act([], 0.5) for act in doc.top]
70 source = bulletml.Bullet(
71 x=150, y=150, target=target,
72 actions=actions, rank=0.5)
73 source.vanished = True
74 active.add(source)
75 target.x, target.y = pygame.mouse.get_pos()
76 target.x /= 2
77 target.y /= 2
78
79 if not paused or go:
80
81 start = time.time()
82 count = len(active)
83 for obj in list(active):
84 new = obj.step()
85 total += len(new)
86 active.update(new)
87 if (obj.finished
88 or not (-50 < obj.x < 350)
89 or not (-50 < obj.y < 350)):
90 active.remove(obj)
91 elapsed = time.time() - start
92
93 frames += 1
94 if frames % 100 == 0:
95 print " Processing: %04d: %d bullets, %d active." % (
96 frames, total, count)
97 if elapsed:
98 seconds_per_bullet = elapsed / count
99 bullets_per_second = count / elapsed
100 print " %g seconds per bullet (60Hz max: %g)." % (
101 seconds_per_bullet, bullets_per_second / 60)
102
103 screen.fill([0, 0, 0])
104 for obj in active:
105 try:
106 x, y = obj.x, obj.y
107 except AttributeError:
108 pass
109 else:
110 if not obj.vanished:
111 x *= 2
112 y *= 2
113 x -= 1
114 y -= 1
115 screen.blit(bullet, [x, y])
116 clock.tick(60)
117 pygame.display.flip()
118
119 print " Finished: %04d: %d bullets." % (frames, total)
120
121 if __name__ == "__main__":
122 main(sys.argv[1:])