comparison pytouhou/opengl/gamerunner.py @ 187:46793ccfedca

Implement replays.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 26 Oct 2011 17:54:03 -0700
parents 7769ce7be03c
children 008f90ebfdc0
comparison
equal deleted inserted replaced
186:84da28ae7ee4 187:46793ccfedca
19 19
20 from pytouhou.opengl.gamerenderer import GameRenderer 20 from pytouhou.opengl.gamerenderer import GameRenderer
21 21
22 22
23 class GameRunner(pyglet.window.Window, GameRenderer): 23 class GameRunner(pyglet.window.Window, GameRenderer):
24 def __init__(self, resource_loader, game=None, background=None): 24 def __init__(self, resource_loader, game=None, background=None, replay=None):
25 GameRenderer.__init__(self, resource_loader, game, background) 25 GameRenderer.__init__(self, resource_loader, game, background)
26 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False) 26 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False)
27 self.keys = pyglet.window.key.KeyStateHandler() 27 self.replay_level = None
28 self.push_handlers(self.keys) 28 if not replay or not replay.levels[game.stage-1]:
29 self.keys = pyglet.window.key.KeyStateHandler()
30 self.push_handlers(self.keys)
31 else:
32 self.keys = 0
33 self.instruction_pointer = 0
34 self.replay_level = replay.levels[game.stage-1]
29 35
30 self.fps_display = pyglet.clock.ClockDisplay() 36 self.fps_display = pyglet.clock.ClockDisplay()
31 37
32 38
33 def start(self, width=384, height=448): 39 def start(self, width=384, height=448):
83 89
84 def update(self): 90 def update(self):
85 if self.background: 91 if self.background:
86 self.background.update(self.game.frame) 92 self.background.update(self.game.frame)
87 if self.game: 93 if self.game:
88 #TODO: allow user settings 94 if not self.replay_level:
89 keystate = 0 95 #TODO: allow user settings
90 if self.keys[pyglet.window.key.W]: 96 keystate = 0
91 keystate |= 1 97 if self.keys[pyglet.window.key.W]:
92 if self.keys[pyglet.window.key.X]: 98 keystate |= 1
93 keystate |= 2 99 if self.keys[pyglet.window.key.X]:
94 #TODO: on some configurations, LSHIFT is Shift_L when pressed 100 keystate |= 2
95 # and ISO_Prev_Group when released, confusing the hell out of pyglet 101 #TODO: on some configurations, LSHIFT is Shift_L when pressed
96 # and leading to a always-on LSHIFT... 102 # and ISO_Prev_Group when released, confusing the hell out of pyglet
97 if self.keys[pyglet.window.key.LSHIFT]: 103 # and leading to a always-on LSHIFT...
98 keystate |= 4 104 if self.keys[pyglet.window.key.LSHIFT]:
99 if self.keys[pyglet.window.key.UP]: 105 keystate |= 4
100 keystate |= 16 106 if self.keys[pyglet.window.key.UP]:
101 if self.keys[pyglet.window.key.DOWN]: 107 keystate |= 16
102 keystate |= 32 108 if self.keys[pyglet.window.key.DOWN]:
103 if self.keys[pyglet.window.key.LEFT]: 109 keystate |= 32
104 keystate |= 64 110 if self.keys[pyglet.window.key.LEFT]:
105 if self.keys[pyglet.window.key.RIGHT]: 111 keystate |= 64
106 keystate |= 128 112 if self.keys[pyglet.window.key.RIGHT]:
107 if self.keys[pyglet.window.key.LCTRL]: 113 keystate |= 128
108 keystate |= 256 114 if self.keys[pyglet.window.key.LCTRL]:
109 self.game.run_iter(keystate) #TODO: self.keys... 115 keystate |= 256
116 self.game.run_iter(keystate) #TODO: self.keys...
117 else:
118 frame, keys = self.replay_level.keys[self.instruction_pointer]
119
120 if frame > self.game.frame:
121 self.game.run_iter(self.keys)
122 return
123
124 self.instruction_pointer += 1
125
126 if frame == self.game.frame:
127 self.keys = keys
128
129 self.game.run_iter(self.keys)
110 130
111 131
112 def on_draw(self): 132 def on_draw(self):
113 GameRenderer.render(self) 133 GameRenderer.render(self)
114 134