Mercurial > rinkhals
comparison gamelib/savegame.py @ 512:b112bcf4d435
Add restore game button to main menu.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 26 Nov 2009 23:36:20 +0000 |
parents | 3e4bb2c9556c |
children | 882b582da618 |
comparison
equal
deleted
inserted
replaced
511:57f9077fb7fb | 512:b112bcf4d435 |
---|---|
10 from pgu import gui | 10 from pgu import gui |
11 import pygame | 11 import pygame |
12 | 12 |
13 import config | 13 import config |
14 import version | 14 import version |
15 import gameboard | |
16 import serializer | |
15 | 17 |
16 TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S" | 18 TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S" |
17 | 19 |
18 def read_savegame(fullpath): | 20 def read_savegame(fullpath): |
19 """Open a save game file.""" | 21 """Open a save game file.""" |
237 | 239 |
238 | 240 |
239 class RestoreDialog(BaseSaveRestoreDialog): | 241 class RestoreDialog(BaseSaveRestoreDialog): |
240 """Restore game dialog.""" | 242 """Restore game dialog.""" |
241 | 243 |
242 def __init__(self, gameboard): | 244 def __init__(self, restore_func): |
243 BaseSaveRestoreDialog.__init__(self, "Load Game ...", "Load", allow_new=False) | 245 BaseSaveRestoreDialog.__init__(self, "Load Game ...", "Load", allow_new=False) |
244 self.connect(gui.CHANGE, self._restore, gameboard) | 246 self.connect(gui.CHANGE, self._restore, restore_func) |
245 | 247 |
246 def _restore(self, gameboard): | 248 def _restore(self, restore_func): |
247 filename = self.get_fullpath() | 249 filename = self.get_fullpath() |
248 if filename is None: | 250 if filename is None: |
249 return | 251 return |
250 | 252 |
251 try: | 253 try: |
252 data, screenshot, level_name, timestamp = read_savegame(filename) | 254 data, screenshot, level_name, timestamp = read_savegame(filename) |
253 except Exception, e: | 255 except Exception, e: |
254 print "Failed to load game: %s" % (e,) | 256 print "Failed to load game: %s" % (e,) |
255 return | 257 return |
256 | 258 |
257 gameboard.restore_game(data) | 259 if 'refid' not in data or 'class' not in data or data['class'] != gameboard.GameBoard.__name__: |
260 print "Failed to load game: Invalid game data." | |
261 return | |
262 | |
263 try: | |
264 new_gameboard = serializer.unsimplify(data) | |
265 except Exception, e: | |
266 print "Failed to load game: %s" % (e,) | |
267 return | |
268 | |
269 restore_func(new_gameboard) |