Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2012-04-12 03:14:18 (GMT)
committer Walter Bender <walter.bender@gmail.com>2012-04-12 03:14:18 (GMT)
commited23cd5fc1d2a4349933b39f2cec27b4fc01ed76 (patch)
treeff98cc731d249c6f5053f59ddefa11fd3f51a437
parentb840ee3f1b4f8c4e2727e2782197fd552ba88574 (diff)
multi-level play
-rw-r--r--ImageIDActivity.py100
-rw-r--r--game.py93
2 files changed, 122 insertions, 71 deletions
diff --git a/ImageIDActivity.py b/ImageIDActivity.py
index 4aad8c2..f7d38b0 100644
--- a/ImageIDActivity.py
+++ b/ImageIDActivity.py
@@ -1,4 +1,4 @@
-#Copyright (c) 2011 Walter Bender
+#Copyright (c) 2011,12 Walter Bender
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -14,20 +14,14 @@ import gtk
from sugar.activity import activity
from sugar import profile
-try:
- from sugar.graphics.toolbarbox import ToolbarBox
- _have_toolbox = True
-except ImportError:
- _have_toolbox = False
-
-if _have_toolbox:
- from sugar.activity.widgets import ActivityToolbarButton
- from sugar.activity.widgets import StopButton
+from sugar.graphics.toolbarbox import ToolbarBox
+from sugar.activity.widgets import ActivityToolbarButton
+from sugar.activity.widgets import StopButton
from gettext import gettext as _
from game import Game
-from toolbar_utils import separator_factory
+from toolbar_utils import separator_factory, radio_factory, label_factory
import logging
_logger = logging.getLogger('imageid-activity')
@@ -35,6 +29,9 @@ _logger = logging.getLogger('imageid-activity')
SERVICE = 'org.sugarlabs.ImageIDActivity'
IFACE = SERVICE
+LABELS = [_('Match the food to its name.'),
+ _('How many calories are there?'),
+ _('How much should you eat?')]
class ImageIDActivity(activity.Activity):
@@ -46,7 +43,7 @@ class ImageIDActivity(activity.Activity):
self.path = activity.get_bundle_path()
- self._setup_toolbars(_have_toolbox)
+ self._setup_toolbars()
# Create a canvas
canvas = gtk.DrawingArea()
@@ -59,37 +56,56 @@ class ImageIDActivity(activity.Activity):
self._game = Game(canvas, parent=self, path=self.path)
self._game.new_game()
- def _setup_toolbars(self, have_toolbox):
+ def _setup_toolbars(self):
""" Setup the toolbars. """
self.max_participants = 1 # No collaboration
- if have_toolbox:
- toolbox = ToolbarBox()
-
- # Activity toolbar
- activity_button = ActivityToolbarButton(self)
-
- toolbox.toolbar.insert(activity_button, 0)
- activity_button.show()
-
- self.set_toolbar_box(toolbox)
- toolbox.show()
- self.toolbar = toolbox.toolbar
-
- else:
- # Use pre-0.86 toolbar design
- games_toolbar = gtk.Toolbar()
- toolbox = activity.ActivityToolbox(self)
- self.set_toolbox(toolbox)
- toolbox.add_toolbar(_('Game'), games_toolbar)
- toolbox.show()
- toolbox.set_current_toolbar(1)
- self.toolbar = games_toolbar
-
- if _have_toolbox:
- separator_factory(toolbox.toolbar, True, False)
- stop_button = StopButton(self)
- stop_button.props.accelerator = '<Ctrl>q'
- toolbox.toolbar.insert(stop_button, -1)
- stop_button.show()
+ toolbox = ToolbarBox()
+
+ # Activity toolbar
+ activity_button = ActivityToolbarButton(self)
+
+ toolbox.toolbar.insert(activity_button, 0)
+ activity_button.show()
+
+ self.set_toolbar_box(toolbox)
+ toolbox.show()
+ self.toolbar = toolbox.toolbar
+
+ self.beginner_button = radio_factory(
+ 'beginner',
+ toolbox.toolbar,
+ self._level_cb,
+ cb_arg=0,
+ tooltip=_('Name the food'),
+ group=None)
+ self.intermediate_button = radio_factory(
+ 'intermediate',
+ toolbox.toolbar,
+ self._level_cb,
+ cb_arg=1,
+ tooltip=_('How many calories?'),
+ group=self.beginner_button)
+ self.expert_button = radio_factory(
+ 'expert',
+ toolbox.toolbar,
+ self._level_cb,
+ cb_arg=2,
+ tooltip=_('Where in the food pyramid?'),
+ group=self.beginner_button)
+
+ separator_factory(toolbox.toolbar, False, True)
+ self._label = label_factory(toolbox.toolbar, LABELS[0])
+
+ separator_factory(toolbox.toolbar, True, False)
+ stop_button = StopButton(self)
+ stop_button.props.accelerator = '<Ctrl>q'
+ toolbox.toolbar.insert(stop_button, -1)
+ stop_button.show()
+
+ def _level_cb(self, button, level):
+ ''' Switch between levels '''
+ self._game.level = level
+ self._label.set_text(LABELS[level])
+ self._game.new_game()
diff --git a/game.py b/game.py
index 07a3152..7cf9ea8 100644
--- a/game.py
+++ b/game.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-#Copyright (c) 2011 Walter Bender
+#Copyright (c) 2011,12 Walter Bender
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -30,19 +30,24 @@ except ImportError:
from sprites import Sprites, Sprite
-GAME_DEFS = [[_('banana'), 'banana.png'],
- [_('apple'), 'pomme.png'],
- [_('cake'), 'cake.png'],
- [_('fish'), 'fish.png'],
- [_('airplane'), 'avion.png'],
- [_('ball'), 'ballon.png'],
- [_('bed'), 'bed.png'],
- [_('car'), 'car.png'],
- [_('bottle'), 'bottle.png'],
- [_('dog'), 'chien.png'],
- [_('house'), 'maison.png'],
- [_('satchel'), 'cartable.png']]
-NCARDS = 5
+LEVELS = [_('minimum'), _('moderate'), _('most'), _('unlimited')]
+GAME_DEFS = [[_('banana'), 105, 2, 'banana.png'],
+ [_('apple'), 72, 2, 'apple.png'],
+ [_('fish'), 100, 1, 'fish.png'],
+ [_('corn'), 96, 2, 'corn.png'],
+ [_('broccoli'), 100, 2, 'broccoli.png'],
+ [_('chicken'), 100, 1, 'chicken.png'],
+ [_('cheese'), 100, 1, 'cheese.png'],
+ [_('orange'), 62, 2, 'orange.png'],
+ [_('potato'), 159, 2, 'potato.png'],
+ [_('water'), 0, 3, 'water.png'],
+ [_('tomato'), 150, 2, 'tomato.png'],
+ [_('cookie'), 68, 0, 'cookie.png'],
+ [_('beef'), 100, 1, 'beef.png'],
+ [_('egg'), 100, 1, 'egg.png'],
+ [_('sweetpotato'), 169, 2, 'sweetpotato.png'],
+ [_('cake'), 387, 0, 'cake.png']]
+NCARDS = 4
class Game():
@@ -58,18 +63,24 @@ class Game():
self._canvas.connect("button-press-event", self._button_press_cb)
self._width = gtk.gdk.screen_width()
- self._height = int(gtk.gdk.screen_height() - (GRID_CELL_SIZE * 1.5))
+ self._height = gtk.gdk.screen_height()
self._scale = self._width / 1200.
self._target = 0
+ self.level = 0
+
# Generate the sprites we'll need...
self._sprites = Sprites(self._canvas)
- background = Sprite(self._sprites, 0, 0,
- gtk.gdk.pixbuf_new_from_file_at_size(
- os.path.join(self._path, 'images', 'background.png'),
- self._width, self._height))
- background.set_layer(0)
- background.type = 'background'
+ self._backgrounds = []
+ for i in range(3):
+ self._backgrounds.append(Sprite(
+ self._sprites, 0, 0, gtk.gdk.pixbuf_new_from_file_at_size(
+ os.path.join(self._path, 'images',
+ 'background%d.png' % (i)),
+ self._width, self._height)))
+ self._backgrounds[-1].set_layer(0)
+ self._backgrounds[-1].type = 'background'
+ self._backgrounds[-1].hide()
self._picture_cards = []
for i in GAME_DEFS:
@@ -78,7 +89,7 @@ class Game():
int(self._width / 2),
int(self._height / 4),
gtk.gdk.pixbuf_new_from_file_at_size(
- os.path.join(self._path, 'images', i[1]),
+ os.path.join(self._path, 'images', i[-1]),
int(self._width / 3),
int(9 * self._width / 12))))
self._picture_cards[-1].type = 'picture'
@@ -107,6 +118,7 @@ class Game():
os.path.join(self._path, 'images', 'smiley_bad.png'),
int(self._width / 2),
int(self._height / 2)))
+ self._frown.set_label_attributes(36)
self._all_clear()
@@ -116,11 +128,16 @@ class Game():
p.hide()
for i, w in enumerate(self._word_cards):
w.set_label_color('black')
- w.set_label(GAME_DEFS[i][0])
+ if self.level < 2:
+ w.set_label(GAME_DEFS[i][self.level])
+ else:
+ w.set_label('')
w.hide()
self._smile.hide()
self._frown.hide()
+ self._backgrounds[self.level].set_layer(1)
+
def new_game(self):
''' Start a new game. '''
self._all_clear()
@@ -129,7 +146,7 @@ class Game():
y = 10 # some small offset from the top edge
dx, dy = self._word_cards[0].get_dimensions()
- # Select 5 cards
+ # Select N cards
self._list = []
for i in range(NCARDS):
j = int(uniform(0, len(GAME_DEFS)))
@@ -138,10 +155,14 @@ class Game():
self._list.append(j)
# Show the word cards from the list
+ j = 0
for i in self._list:
self._word_cards[i].set_layer(100)
self._word_cards[i].move((x, y))
+ if self.level == 2:
+ self._word_cards[i].set_label(LEVELS[j])
y += int(dy * 1.25)
+ j += 1
# Choose a random image from the list and show it.
self._target = int(uniform(0, NCARDS))
@@ -159,13 +180,27 @@ class Game():
# Which card was clicked? Set its label to red.
i = self._word_cards.index(spr)
self._word_cards[i].set_label_color('red')
- self._word_cards[i].set_label(GAME_DEFS[i][0])
+ if self.level < 2:
+ self._word_cards[i].set_label(GAME_DEFS[i][self.level])
+ else:
+ j = self._list.index(i)
+ self._word_cards[i].set_label(LEVELS[j])
# If the label matches the picture, smile
- if i == self._list[self._target]:
- self._smile.set_layer(200)
- else:
- self._frown.set_layer(200)
+ if self.level < 2:
+ if i == self._list[self._target]:
+ self._smile.set_layer(200)
+ else:
+ self._frown.set_layer(200)
+ self._frown.set_label(
+ GAME_DEFS[self._list[self._target]][self.level])
+ else: # Level 2
+ if j == GAME_DEFS[self._list[self._target]][2]:
+ self._smile.set_layer(200)
+ else:
+ self._frown.set_layer(200)
+ self._frown.set_label(
+ LEVELS[GAME_DEFS[self._list[self._target]][2]])
# Play again
gobject.timeout_add(2000, self.new_game)