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