changeset 374:6a63fd3deb76

Use an iterator to get the keystates from a replay.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 05 Aug 2012 18:19:42 +0200
parents 6deab6ad8be8
children 8f2f3053906a
files pytouhou/formats/t6rp.py pytouhou/ui/gamerunner.py
diffstat 2 files changed, 16 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/formats/t6rp.py
+++ b/pytouhou/formats/t6rp.py
@@ -43,6 +43,17 @@ class Level(object):
         self.keys = []
 
 
+    def iter_keystates(self):
+        counter = 0
+        previous = 0
+        for frame, keystate, unknown in self.keys:
+            while frame >= counter:
+                yield previous
+                counter += 1
+            previous = keystate
+
+
+
 class T6RP(object):
     def __init__(self):
         self.version = 0x102
--- a/pytouhou/ui/gamerunner.py
+++ b/pytouhou/ui/gamerunner.py
@@ -68,8 +68,8 @@ class GameRunner(pyglet.window.Window, G
             self.keys = pyglet.window.key.KeyStateHandler()
             self.push_handlers(self.keys)
         else:
-            self.keys = 0
             self.replay_level = replay.levels[game.stage-1]
+            self.keys = self.replay_level.iter_keystates()
             game.players[0].state.lives = self.replay_level.lives
             game.players[0].state.power = self.replay_level.power
             game.players[0].state.bombs = self.replay_level.bombs
@@ -161,12 +161,10 @@ class GameRunner(pyglet.window.Window, G
                 if self.keys[pyglet.window.key.LCTRL]:
                     keystate |= 256
             else:
-                keystate = 0
-                for frame, _keystate, unknown in self.replay_level.keys:
-                    if self.game.frame < frame:
-                        break
-                    else:
-                        keystate = _keystate
+                try:
+                    keystate = self.keys.next()
+                except StopIteration:
+                    keystate = 0
 
             if self.save_keystates is not None:
                 self.save_keystates.append(keystate)