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