From c9bdaca34179fe1a301227142a183fa261cd6305 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 07 Dec 2011 16:37:35 +0000 Subject: save/restore game state to Journal --- diff --git a/ReflectionActivity.py b/ReflectionActivity.py index e4b3361..ac59374 100644 --- a/ReflectionActivity.py +++ b/ReflectionActivity.py @@ -63,8 +63,10 @@ class ReflectionActivity(activity.Activity): self._game = Game(canvas, parent=self, colors=self.colors) - # TODO: Restore game state from Journal or start new game - self._game.new_game('horizontal') + if 'dotlist' in self.metadata: + self._restore() + else: + self._game.new_game('horizontal') def _setup_toolbars(self, have_toolbox): """ Setup the toolbars. """ @@ -126,8 +128,23 @@ class ReflectionActivity(activity.Activity): def write_file(self, file_path): """ Write the grid status to the Journal """ - return + (orientation, dot_list) = self._game.save_game() + self.metadata['orientation'] = orientation + self.metadata['dotlist'] = '' + for dot in dot_list: + self.metadata['dotlist'] += str(dot) + if dot_list.index(dot) < len(dot_list) - 1: + self.metadata['dotlist'] += ' ' def _restore(self): """ Restore the game state from metadata """ - return + if 'orientation' in self.metadata: + orientation = self.metadata['orientation'] + else: + orientation = 'horizontal' + + dot_list = [] + dots = self.metadata['dotlist'].split() + for dot in dots: + dot_list.append(int(dot)) + self._game.restore_game(dot_list, orientation) diff --git a/game.py b/game.py index 70ef13c..8b8672d 100644 --- a/game.py +++ b/game.py @@ -91,6 +91,16 @@ class Game(): self._press = None self.saw_game_over = False + # Clear dots + for dot in self._dots: + if dot.type > 0: + dot.type = 0 + dot.set_shape(self._new_dot(self._colors[0])) + + self._set_orientation() + + def _set_orientation(self): + ''' Set bar and message for current orientation ''' if self._orientation == 'horizontal': self.hline.hide() self.vline.set_layer(1000) @@ -101,12 +111,6 @@ class Game(): self.hline.set_layer(1000) self.vline.set_layer(1000) - # Clear dots - for dot in self._dots: - if dot.type > 0: - dot.type = 0 - dot.set_shape(self._new_dot(self._colors[0])) - if self._orientation == 'horizontal': self._set_label( _('Click on the dots to make a horizontal reflection.')) @@ -133,6 +137,22 @@ class Game(): self._dots[n].set_shape(self._new_dot( self._colors[self._dots[n].type])) + def restore_game(self, dot_list, orientation): + ''' Restore a game from the Journal or share ''' + for i, dot in enumerate(dot_list): + self._dots[i].type = dot + self._dots[i].set_shape(self._new_dot( + self._colors[self._dots[i].type])) + self._set_orientation() + + def save_game(self): + ''' Return dot list and orientation for saving to Journal or + sharing ''' + dot_list = [] + for dot in self._dots: + dot_list.append(dot.type) + return (self._orientation, dot_list) + def _set_label(self, string): ''' Set the label in the toolbar or the window frame. ''' self._activity.status.set_label(string) @@ -232,10 +252,6 @@ class Game(): if vertical: self._svg_width = 3 self._svg_height = self._height - _logger.debug( - self._header() + \ - self._rect(3, self._height, 0, 0) + \ - self._footer()) return svg_str_to_pixbuf( self._header() + \ self._rect(3, self._height, 0, 0) + \ -- cgit v0.9.1