[Sugar-devel] [PATCH Maze] Buttons to generate a harder or an easier level

Manuel Kaufmann humitos at gmail.com
Wed Mar 28 19:35:34 EDT 2012


Added two buttons in the toolbar to generate a new level (harder or easier).
These butons do the same work than '+' and '-' keys respectively.

This commit solves ticket: #3376

Signed-off-by: Manuel Kaufmann <humitos at gmail.com>
---
 activity.py |   36 +++++++++++++++++++++++++++++++++++-
 game.py     |    7 ++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/activity.py b/activity.py
index f21efdd..d5ea2eb 100755
--- a/activity.py
+++ b/activity.py
@@ -1,9 +1,43 @@
+import gtk
 import olpcgames
-from gettext import gettext as _
+import pygame
 
+from sugar.graphics.toolbutton import ToolButton
+from gettext import gettext as _
 
 
 class MazeActivity(olpcgames.PyGameActivity):
     game_name = 'game'
     game_title = _('Maze')
     game_size = None    # let olpcgames pick a nice size for us
+
+    def build_toolbar(self):
+        """Build our Activity toolbar for the Sugar system."""
+        toolbar = super(MazeActivity, self).build_toolbar()
+
+        separator = gtk.SeparatorToolItem()
+        separator.set_expand(True)
+        separator.set_draw(False)
+        toolbar.insert(separator, 0)
+
+        easier_button = ToolButton('list-remove')
+        easier_button.set_tooltip(_('Easier level'))
+        easier_button.connect('clicked', self._easier_button_cb)
+        toolbar.insert(easier_button, 2)
+        easier_button.show()
+
+        harder_button = ToolButton('list-add')
+        harder_button.set_tooltip(_('Harder level'))
+        harder_button.connect('clicked', self._harder_button_cb)
+        toolbar.insert(harder_button, 2)
+        harder_button.show()
+
+        return toolbar
+
+    def _easier_button_cb(self, button):
+        pygame.event.post(olpcgames.eventwrap.Event(
+            pygame.USEREVENT, action='easier_button'))
+
+    def _harder_button_cb(self, button):
+        pygame.event.post(olpcgames.eventwrap.Event(
+            pygame.USEREVENT, action='harder_button'))
diff --git a/game.py b/game.py
index 8482123..e9ec3b4 100644
--- a/game.py
+++ b/game.py
@@ -298,8 +298,13 @@ class MazeGame:
                 print "Message from unknown buddy?"
 
         elif event.type == pygame.USEREVENT:
+            # process our buttons
+            if hasattr(event, 'action') and event.action == 'harder_button':
+                self.harder()
+            elif hasattr(event, 'action') and event.action == 'easier_button':
+                self.easier()
             # process file save / restore events
-            if event.code == olpcgames.FILE_READ_REQUEST:
+            elif event.code == olpcgames.FILE_READ_REQUEST:
                 log.debug('Loading the state of the game...')
                 state = json.loads(event.metadata['state'])
                 log.debug('Loaded data: %s', state)
-- 
1.7.9.1



More information about the Sugar-devel mailing list