Get my coordinate systems straight - 0,0 will be upper-left.
[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 clock = pygame.time.Clock()
16 target = bulletml.Bullet()
17
18 file_idx = 0
19
20 if not argv:
21 raise SystemExit
22
23 while True:
24 doc = bulletml.BulletML(open(argv[file_idx % len(argv)], "rU"))
25 source = bulletml.Bullet.FromDoc(
26 doc, x=150, y=150, target=target, rank=0.5)
27
28 active = set([source])
29 source.vanished = True
30 print filename
31 print " Loaded %d top-level actions." % len(source.actions)
32 frames = 0
33 total = 0
34 paused = False
35 newfile = False
36
37 while active and not newfile:
38 go = False
39
40 for event in pygame.event.get():
41 if event.type == pygame.QUIT:
42 raise SystemExit
43 elif event.type == pygame.KEYDOWN:
44 if event.key == pygame.K_SPACE:
45 paused ^= True
46 elif event.key == pygame.K_RIGHT:
47 go = True
48 elif event.key == pygame.K_PAGEUP:
49 file_idx -= 1
50 newfile = True
51 elif event.key == pygame.K_PAGEDOWN:
52 file_idx += 1
53 newfile = True
54 elif event.key == pygame.K_RETURN:
55 newfile = True
56 target.x, target.y = pygame.mouse.get_pos()
57 target.x /= 2
58 target.y /= 2
59
60 if not paused or go:
61
62 for obj in list(active):
63 new = obj.step()
64 total += len(new)
65 active.update(new)
66 if (obj.finished
67 or not (-50 < obj.x < 350)
68 or not (-50 < obj.y < 350)):
69 active.remove(obj)
70
71 frames += 1
72 if frames % 100 == 0:
73 print " Processing: %04d: %d bullets, %d active." % (
74 frames, total, len(active))
75
76 screen.fill([0, 0, 0])
77 for obj in active:
78 try:
79 x, y = obj.x, obj.y
80 except AttributeError:
81 pass
82 else:
83 if not obj.vanished:
84 x *= 2
85 y *= 2
86 x -= 1
87 y -= 1
88 screen.blit(bullet, [x, y])
89 pygame.display.flip()
90
91 clock.tick(60)
92
93 for event in pygame.event.get():
94 if event.type == pygame.QUIT:
95 raise SystemExit
96 elif event.type == pygame.KEYDOWN:
97 if event.key == pygame.K_SPACE:
98 paused ^= True
99 elif event.key == pygame.K_RIGHT:
100 go = True
101
102 print " Finished: %04d: %d bullets." % (frames, total)
103
104 if __name__ == "__main__":
105 main(sys.argv[1:])