Back to index

gcompris  8.2.2
mosaic.py
Go to the documentation of this file.
00001 #  gcompris - mosaic
00002 #
00003 # Copyright (C) 2005 Bruno Coudoin / Clara Coudoin
00004 #
00005 #   This program is free software; you can redistribute it and/or modify
00006 #   it under the terms of the GNU General Public License as published by
00007 #   the Free Software Foundation; either version 2 of the License, or
00008 #   (at your option) any later version.
00009 #
00010 #   This program is distributed in the hope that it will be useful,
00011 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #   GNU General Public License for more details.
00014 #
00015 #   You should have received a copy of the GNU General Public License
00016 #   along with this program; if not, write to the Free Software
00017 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 
00020 import gnomecanvas
00021 import gcompris
00022 import gcompris.utils
00023 import gcompris.skin
00024 import gcompris.admin
00025 import gcompris.bonus
00026 import gtk
00027 import gtk.gdk
00028 import random
00029 import pango
00030 
00031 from gcompris import gcompris_gettext as _
00032 
00033 class Gcompris_mosaic:
00034   """Tux hide a number, you must guess it"""
00035 
00036 
00037   def __init__(self, gcomprisBoard):
00038 
00039     self.gcomprisBoard = gcomprisBoard
00040 
00041     self.gcomprisBoard.disable_im_context = True
00042 
00043     # A text canvas item use to indicate it's over or lower
00044     self.indicator = None
00045     self.indicator_s = None
00046 
00047     # The current selected color
00048     self.current_index = None
00049 
00050     # The min and max value that must be found
00051     self.min = -1
00052     self.max = -1
00053 
00054     # These are used to let us restart only after the bonus is displayed.
00055     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00056     self.board_paused  = 0
00057     self.gamewon       = 0
00058 
00059     self.orig_x = 35
00060     self.orig_y = 50
00061     self.target_x = gcompris.BOARD_WIDTH/2 + 17
00062     self.target_y = 50
00063 
00064     self.palette_x = 40
00065     self.palette_y = 330
00066 
00067     # In the palette png, the size of each image
00068     self.palette_item_width  = 50
00069     self.palette_item_height = 50
00070 
00071     # In the palette png, the number of image in x and y
00072     self.palette_number_of_item_x = 8
00073     self.palette_number_of_item_y = 3
00074 
00075     # Index of the grey item in the palette
00076     self.palette_grey_item_x = 0
00077     self.palette_grey_item_y = 3
00078 
00079     # What the user have to find
00080     self.target_list = []
00081 
00082     # The user answer
00083     self.user_list   = []
00084 
00085   def start(self):
00086 
00087     self.gcomprisBoard.level=1
00088     self.gcomprisBoard.maxlevel=6
00089     self.gcomprisBoard.sublevel=1
00090     self.gcomprisBoard.number_of_sublevel=1
00091 
00092     gcompris.bar_set(gcompris.BAR_LEVEL)
00093 
00094     gcompris.bar_set_level(self.gcomprisBoard)
00095 
00096     gcompris.set_background(self.gcomprisBoard.canvas.root(),
00097                             "images/mosaic_bg.png")
00098 
00099     self.display_game()
00100 
00101 
00102   def end(self):
00103 
00104     gcompris.reset_locale()
00105 
00106     # Remove the root item removes all the others inside it
00107     self.cleanup_game()
00108 
00109   def ok(self):
00110     pass
00111 
00112   def repeat(self):
00113     print("Gcompris_mosaic repeat.")
00114 
00115 
00116   def key_press(self, keyval, commit_str, preedit_str):
00117     # Return  True  if you did process a key
00118     # Return  False if you did not processed a key
00119     #         (gtk need to send it to next widget)
00120     return False
00121 
00122   def pause(self, pause):
00123     self.board_paused = pause
00124 
00125     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00126     # the game is won
00127     if(pause == 0 and self.gamewon):
00128       self.increment_level()
00129       self.gamewon = 0
00130       self.cleanup_game()
00131       self.display_game()
00132 
00133     return
00134 
00135 
00136   def set_level(self, level):
00137     self.gcomprisBoard.level=level;
00138     self.gcomprisBoard.sublevel=1;
00139 
00140     # Set the level in the control bar
00141     gcompris.bar_set_level(self.gcomprisBoard);
00142 
00143     self.cleanup_game()
00144     self.display_game()
00145 
00146   #
00147   # End of Initialisation
00148   # ---------------------
00149   #
00150 
00151   # Code that increments the sublevel and level
00152   # And bail out if no more levels are available
00153   # return 1 if continue, 0 if bail out
00154   def increment_level(self):
00155     self.gcomprisBoard.sublevel += 1
00156 
00157     if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
00158       # Try the next level
00159       self.gcomprisBoard.sublevel=1
00160       self.gcomprisBoard.level += 1
00161       gcompris.bar_set_level(self.gcomprisBoard)
00162 
00163       if(self.gcomprisBoard.level>self.gcomprisBoard.maxlevel):
00164         # the current board is finished : bail out
00165         gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
00166         return 0
00167 
00168     return 1
00169 
00170   # Display the board game
00171   def cleanup_game(self):
00172 
00173       self.current_index = None
00174       # Remove the root item removes all the others inside it
00175       self.rootitem.destroy()
00176 
00177   # Display the board game
00178   def display_game(self):
00179 
00180       # What the user have to find
00181       self.target_list = []
00182       # The user answer
00183       self.user_list   = []
00184 
00185       # Create our rootitem. We put each canvas item in it so at the end we
00186       # only have to kill it. The canvas deletes all the items it contains automaticaly.
00187       self.rootitem = self.gcomprisBoard.canvas.root().add(
00188           gnomecanvas.CanvasGroup,
00189           x=0.0,
00190           y=0.0
00191           )
00192 
00193       text = _("Rebuild the same mosaic on the right area")
00194       self.rootitem.add(
00195           gnomecanvas.CanvasText,
00196           x=gcompris.BOARD_WIDTH/2 + 1.0,
00197           y=gcompris.BOARD_HEIGHT - 30 + 1.0,
00198           font=gcompris.skin.get_font("gcompris/title"),
00199           text=(text),
00200           fill_color="black",
00201           justification=gtk.JUSTIFY_CENTER
00202           )
00203       self.rootitem.add(
00204           gnomecanvas.CanvasText,
00205           x=gcompris.BOARD_WIDTH/2,
00206           y=gcompris.BOARD_HEIGHT - 30,
00207           font=gcompris.skin.get_font("gcompris/title"),
00208           text=(text),
00209           fill_color="white",
00210           justification=gtk.JUSTIFY_CENTER
00211           )
00212 
00213 
00214 
00215       self.number_item_x = 4
00216       self.number_item_y = 2
00217       self.number_max_of_color_x = 8
00218       self.number_max_of_color_y = 1
00219       if(self.gcomprisBoard.level == 2):
00220         self.number_item_x = 6
00221         self.number_item_y = 2
00222         self.number_max_of_color_y = 1
00223       elif(self.gcomprisBoard.level == 3):
00224         self.number_item_x = 6
00225         self.number_item_y = 3
00226         self.number_max_of_color_y = 2
00227       elif(self.gcomprisBoard.level == 4):
00228         self.number_item_x = 6
00229         self.number_item_y = 3
00230         self.number_max_of_color_y = 3
00231       elif(self.gcomprisBoard.level == 5):
00232         self.number_item_x = 6
00233         self.number_item_y = 4
00234         self.number_max_of_color_y = 3
00235       elif(self.gcomprisBoard.level == 6):
00236         self.number_item_x = 6
00237         self.number_item_y = 4
00238         self.number_max_of_color_y = 3
00239 
00240 
00241       self.current_index_x = -1
00242       self.current_index_y = -1
00243 
00244       palette = gcompris.utils.load_pixmap("images/mosaic_palette.png")
00245 
00246       self.display_mosaic(self.orig_x, self.orig_y, True, palette)
00247 
00248       self.display_mosaic(self.target_x, self.target_y, False, palette)
00249 
00250       self.display_palette(self.palette_x, self.palette_y, palette)
00251 
00252       # Create the check button to show the selected color
00253       self.checked_color_item = self.rootitem.add(
00254         gnomecanvas.CanvasPixbuf,
00255         pixbuf = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("button_checked.png")),
00256         x = 0,
00257         y = 0)
00258 
00259       self.checked_color_item.hide()
00260 
00261   #
00262   # Display the mosaic itself (colored or not)
00263   #
00264   def display_mosaic(self, orig_x, orig_y, colored, palette):
00265 
00266       gap_x = 10
00267       gap_y = 10
00268 
00269       i = 0
00270       for y in range(0, self.number_item_y):
00271         for x in range(0, self.number_item_x):
00272 
00273               # Select an image in the palette
00274               color_index_x = random.randint(0, (self.number_max_of_color_x-1))
00275               color_index_y = random.randint(0, (self.number_max_of_color_y-1))
00276 
00277               if colored:
00278                 image  = palette.subpixbuf(color_index_x * self.palette_item_width,
00279                                            color_index_y * self.palette_item_height,
00280                                            self.palette_item_width, self.palette_item_height)
00281               else:
00282                 image  = palette.subpixbuf(self.palette_grey_item_x * self.palette_item_width,
00283                                            self.palette_grey_item_y * self.palette_item_height,
00284                                            self.palette_item_width, self.palette_item_height)
00285 
00286               item = self.rootitem.add(
00287                 gnomecanvas.CanvasPixbuf,
00288                 pixbuf = image,
00289                 x = orig_x + x * (self.palette_item_width  + gap_x),
00290                 y = orig_y + y * (self.palette_item_height + gap_y))
00291 
00292               if not colored:
00293                 item.connect("event", self.set_focus_item_event, (i,
00294                                                                   item, palette))
00295                 self.user_list.append((-1, -1))
00296               else:
00297                 self.target_list.append((color_index_x, color_index_y))
00298 
00299               i += 1
00300 
00301 
00302 
00303   #
00304   # Display the color palette
00305   #
00306   def display_palette(self, orig_x, orig_y, palette):
00307 
00308       gap_x = 10
00309       gap_y = 10
00310 
00311       number_item_x = 12
00312       number_item_y = 2
00313 
00314       for i in range(0, self.number_max_of_color_x * self.number_max_of_color_y):
00315 
00316             x         = i % number_item_x
00317             y         = i / number_item_x
00318 
00319             palette_x = i % self.palette_number_of_item_x
00320             palette_y = i / self.palette_number_of_item_x
00321 
00322             image  = palette.subpixbuf(palette_x * self.palette_item_width,
00323                                        palette_y * self.palette_item_height,
00324                                        self.palette_item_width, self.palette_item_height)
00325 
00326             coord_x = orig_x + x * (self.palette_item_width  + gap_x)
00327             coord_y = orig_y + y * (self.palette_item_height + gap_y)
00328             item = self.rootitem.add(
00329               gnomecanvas.CanvasPixbuf,
00330               pixbuf = image,
00331               x = coord_x,
00332               y = coord_y)
00333 
00334             item.connect("event", gcompris.utils.item_event_focus)
00335             item.connect("event", self.set_color_item_event, (item, palette_x, palette_y,
00336                                                               coord_x, coord_y))
00337 
00338 
00339   # Event when a target square is selected
00340   def set_focus_item_event(self, item, event, data):
00341     (index, box_item, palette)  = data
00342 
00343     if event.type == gtk.gdk.BUTTON_PRESS:
00344       # A color is selected
00345       if(self.current_index_x >= 0):
00346 
00347         image = palette.subpixbuf(self.current_index_x * self.palette_item_width,
00348                                   self.current_index_y * self.palette_item_height,
00349                                   self.palette_item_width, self.palette_item_height)
00350 
00351         box_item.set(pixbuf = image)
00352 
00353         self.user_list[index] = (self.current_index_x,
00354                                  self.current_index_y)
00355 
00356         if(self.target_list == self.user_list):
00357           self.gamewon = True
00358           gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.GNU)
00359 
00360 
00361 
00362   # Event when a color square is selected
00363   def set_color_item_event(self, item, event, data):
00364 
00365     if event.type == gtk.gdk.BUTTON_PRESS:
00366       (box_item, color_index_x, color_index_y, coord_x, coord_y) = data
00367 
00368       self.current_index_x = color_index_x
00369       self.current_index_y = color_index_y
00370 
00371       index = color_index_y * self.palette_number_of_item_x + color_index_x
00372 
00373       self.checked_color_item.set( x = coord_x,
00374                                    y = coord_y + self.palette_item_height/3)
00375       self.checked_color_item.show()