Stricter PEP-8 conformance.
[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 target.px = target.x
90 target.py = target.y
91
92 collides = False
93 if not paused or go:
94 lactive = list(active)
95 start = time.time()
96 count = len(active)
97 for obj in lactive:
98 new = obj.step()
99 total += len(new)
100 active.update(new)
101 if (obj.finished
102 or not (-50 < obj.x < 350)
103 or not (-50 < obj.y < 350)):
104 active.remove(obj)
105 if lactive:
106 collides = collides_all(target, lactive)
107 elapsed = time.time() - start
108
109 frames += 1
110 if frames % 100 == 0:
111 print " Processing: %04d: %d bullets, %d active." % (
112 frames, total, count)
113 if elapsed:
114 seconds_per_bullet = elapsed / count
115 bullets_per_second = count / elapsed
116 print " %g seconds per bullet (120Hz max: %g)." % (
117 seconds_per_bullet, bullets_per_second / 120)
118
119 screen.fill([0, 0, 64] if collides else [0, 0, 0] )
120 for obj in active:
121 try:
122 x, y = obj.x, obj.y
123 except AttributeError:
124 pass
125 else:
126 if not obj.vanished:
127 x *= 2
128 y *= 2
129 x -= 1
130 y -= 1
131 bullet = bullets.get(obj.appearance, red)
132 screen.blit(bullet, [x, 600 - y])
133 clock.tick(60)
134 pygame.display.flip()
135
136 print " Finished: %04d: %d bullets." % (frames, total)
137
138 if __name__ == "__main__":
139 main(sys.argv[1:])