diff gamelib/gamegui.py @ 88:74ce25ec2073

Autosave & load support
author Neil Muller <drnlmuller@gmail.com>
date Wed, 09 May 2012 20:07:14 +0200
parents b0d97d51df51
children 0823e2529c23
line wrap: on
line diff
--- a/gamelib/gamegui.py	Wed May 09 20:06:44 2012 +0200
+++ b/gamelib/gamegui.py	Wed May 09 20:07:14 2012 +0200
@@ -4,8 +4,15 @@
 """Gui for the actual game"""
 
 from pygame import image
+try:
+    import simplejson
+    json = simplejson
+except ImportError:
+    import json
+
 
 from gamelib.data import filepath
+from gamelib.game_base import get_save_filename
 from gamelib.gui_base import Window, TextLabel, font_small, font_medium
 from gamelib.gui import BigButton, ImageDrawable
 from gamelib.engine import PopWindow, AddWindow
@@ -441,16 +448,17 @@
 class LabWindow(Window):
     """Window for the research lab"""
 
-    def __init__(self, screen):
+    def __init__(self, screen, game_dict):
         super(LabWindow, self).__init__(screen)
         self.screen = screen
-        self.game = Game()
+        self.game = Game(game_dict)
         exit = ExitGameButton()
         self.add_child(exit)
         end_turn = EndTurnButton(self)
         self.add_child(end_turn)
         reset = ResetButton(self)
         self.add_child(reset)
+        self.autosave = get_save_filename()
 
         self.points = ValueLabel((10, 75), 'Available Human Resources')
         self.add_child(self.points)
@@ -501,6 +509,11 @@
         self.update_widgets()
         self.develop.update_widgets()
         self.activity.update_widgets()
+        game_data = self.game.save_data()
+        if self.autosave:
+            savefile = open(self.autosave, 'w')
+            json.dump(game_data, savefile)
+            savefile.close()
         AddWindow.post(results)
 
     def update(self):