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