comparison pytouhou/ui/gamerunner.py @ 378:11d895b6c0dc

Add the debug feature to start a game at the end of a replay.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 29 Aug 2012 18:38:29 +0200
parents 6a63fd3deb76
children e0e284fcb288
comparison
equal deleted inserted replaced
377:70e2ed71b09c 378:11d895b6c0dc
35 35
36 logger = get_logger(__name__) 36 logger = get_logger(__name__)
37 37
38 38
39 class GameRunner(pyglet.window.Window, GameRenderer): 39 class GameRunner(pyglet.window.Window, GameRenderer):
40 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60, fixed_pipeline=False): 40 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60, fixed_pipeline=False, skip=False):
41 GameRenderer.__init__(self, resource_loader, game, background) 41 GameRenderer.__init__(self, resource_loader, game, background)
42 42
43 config = pyglet.gl.Config(double_buffer=double_buffer) 43 config = pyglet.gl.Config(double_buffer=double_buffer)
44 width, height = (game.interface.width, game.interface.height) if game else (None, None) 44 width, height = (game.interface.width, game.interface.height) if game else (None, None)
45 pyglet.window.Window.__init__(self, width=width, height=height, 45 pyglet.window.Window.__init__(self, width=width, height=height,
47 config=config) 47 config=config)
48 48
49 self.fps_limit = fps_limit 49 self.fps_limit = fps_limit
50 self.use_fixed_pipeline = fixed_pipeline 50 self.use_fixed_pipeline = fixed_pipeline
51 self.replay_level = None 51 self.replay_level = None
52 self.skip = skip
52 53
53 if not self.use_fixed_pipeline: 54 if not self.use_fixed_pipeline:
54 self.game_shader = GameShader() 55 self.game_shader = GameShader()
55 self.background_shader = BackgroundShader() 56 self.background_shader = BackgroundShader()
56 self.interface_shader = self.game_shader 57 self.interface_shader = self.game_shader
61 self.clock = pyglet.clock.get_default() 62 self.clock = pyglet.clock.get_default()
62 63
63 64
64 def load_game(self, game=None, background=None, bgms=None, replay=None, save_keystates=None): 65 def load_game(self, game=None, background=None, bgms=None, replay=None, save_keystates=None):
65 GameRenderer.load_game(self, game, background) 66 GameRenderer.load_game(self, game, background)
66 self.replay_level = None 67 self.set_input(replay)
67 if not replay or not replay.levels[game.stage-1]: 68 if replay and replay.levels[game.stage - 1]:
68 self.keys = pyglet.window.key.KeyStateHandler()
69 self.push_handlers(self.keys)
70 else:
71 self.replay_level = replay.levels[game.stage-1]
72 self.keys = self.replay_level.iter_keystates()
73 game.players[0].state.lives = self.replay_level.lives 69 game.players[0].state.lives = self.replay_level.lives
74 game.players[0].state.power = self.replay_level.power 70 game.players[0].state.power = self.replay_level.power
75 game.players[0].state.bombs = self.replay_level.bombs 71 game.players[0].state.bombs = self.replay_level.bombs
76 game.difficulty = self.replay_level.difficulty 72 game.difficulty = self.replay_level.difficulty
77 73
80 game.music = MusicPlayer(game.resource_loader, bgms) 76 game.music = MusicPlayer(game.resource_loader, bgms)
81 game.music.play(0) 77 game.music.play(0)
82 78
83 game.player_sfx = SFXPlayer(game.resource_loader) 79 game.player_sfx = SFXPlayer(game.resource_loader)
84 game.enemy_sfx = SFXPlayer(game.resource_loader) 80 game.enemy_sfx = SFXPlayer(game.resource_loader)
81
82
83 def set_input(self, replay=None):
84 if not replay or not replay.levels[self.game.stage-1]:
85 self.keys = pyglet.window.key.KeyStateHandler()
86 self.push_handlers(self.keys)
87 self.replay_level = None
88 else:
89 self.replay_level = replay.levels[self.game.stage-1]
90 self.keys = self.replay_level.iter_keystates()
85 91
86 92
87 def start(self, width=None, height=None): 93 def start(self, width=None, height=None):
88 width = width or (self.game.interface.width if self.game else 640) 94 width = width or (self.game.interface.width if self.game else 640)
89 height = height or (self.game.interface.height if self.game else 480) 95 height = height or (self.game.interface.height if self.game else 480)
106 self.interface_mvp = self.ortho_2d(0., float(self.width), float(self.height), 0.) 112 self.interface_mvp = self.ortho_2d(0., float(self.width), float(self.height), 0.)
107 113
108 if self.fps_limit > 0: 114 if self.fps_limit > 0:
109 pyglet.clock.set_fps_limit(self.fps_limit) 115 pyglet.clock.set_fps_limit(self.fps_limit)
110 while not self.has_exit: 116 while not self.has_exit:
111 pyglet.clock.tick() 117 if not self.skip:
112 self.dispatch_events() 118 pyglet.clock.tick()
113 self.update() 119 self.dispatch_events()
114 self.render_game() 120 self.update()
115 self.render_interface() 121 self.render_game()
116 self.flip() 122 self.render_interface()
123 self.flip()
124 else:
125 self.update()
117 126
118 127
119 def _event_text_symbol(self, ev): 128 def _event_text_symbol(self, ev):
120 # XXX: Ugly workaround to a pyglet bug on X11 129 # XXX: Ugly workaround to a pyglet bug on X11
121 #TODO: fix that bug in pyglet 130 #TODO: fix that bug in pyglet
163 else: 172 else:
164 try: 173 try:
165 keystate = self.keys.next() 174 keystate = self.keys.next()
166 except StopIteration: 175 except StopIteration:
167 keystate = 0 176 keystate = 0
177 if self.skip:
178 self.set_input()
179 self.skip = False
168 180
169 if self.save_keystates is not None: 181 if self.save_keystates is not None:
170 self.save_keystates.append(keystate) 182 self.save_keystates.append(keystate)
171 183
172 self.game.run_iter(keystate) 184 self.game.run_iter(keystate)