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

Manuel Kaufmann humitos at gmail.com
Wed Mar 28 08:39:13 EDT 2012


Addedi 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     |    5 +++++
 2 files changed, 40 insertions(+), 1 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 579c5c4..5f2c09b 100644
--- a/game.py
+++ b/game.py
@@ -283,6 +283,11 @@ class MazeGame:
                     (event, sys.exc_info())
             else:
                 print "Message from unknown buddy?"
+        if event.type == pygame.USEREVENT:
+            if event.action == 'harder_button':
+                self.harder()
+            elif event.action == 'easier_button':
+                self.easier()
         else:
             print "Unknown event:", event
 
-- 
1.7.9.1



More information about the Sugar-devel mailing list