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