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