Back to index

gcompris  8.2.2
algorithm.py
Go to the documentation of this file.
00001 #  gcompris - algorithm
00002 #
00003 # Time-stamp: <2004/29/03 15:31 Ingo Konrad>
00004 #
00005 # Copyright (C) 2004 Christof Petig and Ingo Konrad
00006 #
00007 #   This program is free software; you can redistribute it and/or modify
00008 #   it under the terms of the GNU General Public License as published by
00009 #   the Free Software Foundation; either version 2 of the License, or
00010 #   (at your option) any later version.
00011 #
00012 #   This program is distributed in the hope that it will be useful,
00013 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 #   GNU General Public License for more details.
00016 #
00017 #   You should have received a copy of the GNU General Public License
00018 #   along with this program; if not, write to the Free Software
00019 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 #
00021 import gnomecanvas
00022 import gcompris
00023 import gcompris.utils
00024 import gcompris.skin
00025 import gcompris.bonus
00026 import gcompris.score
00027 import gtk
00028 import gtk.gdk
00029 import random
00030 
00031 def algo(index): return index %4
00032 def algo1(index): return index %3
00033 def algo2(index): return index %5
00034 def algo3(i):
00035   if i %6 > 2:
00036    return 2 - i %3
00037   else:
00038    return i %3
00039 
00040 class Gcompris_algorithm:
00041   """The algorithm activity"""
00042 
00043   def paint_image (self, i, x2, y2):
00044 
00045 
00046     return self.rootitem.add(
00047         gnomecanvas.CanvasPixbuf,
00048         pixbuf = self.pixlist[i],
00049         x=self.leftx+x2*self.distance,
00050         y=y2
00051         )
00052 
00053   def __init__(self, gcomprisBoard):
00054     self.gcomprisBoard = gcomprisBoard
00055     self.anzahl = 8
00056     print("Gcompris_algorithm __init__.")
00057     self.algos = [algo, algo1, algo2, algo3]
00058     self.rootitem = None
00059     self.distance = 80
00060     self.leftx = 90
00061 
00062     # These are used to let us restart only after the bonus is displayed.
00063     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00064     self.board_paused  = 0;
00065     self.gamewon       = 0;
00066 
00067   def start(self):
00068     gcompris.bar_set (0)
00069     gcompris.set_background(self.gcomprisBoard.canvas.root(),
00070                             "images/scenery5_background.png")
00071     self.gcomprisBoard.level=1
00072     self.gcomprisBoard.sublevel=1
00073     self.gcomprisBoard.number_of_sublevel=5
00074     self.gcomprisBoard.maxlevel = 1
00075 
00076     self.symbollist = ["gcompris/misc/apple.png","gcompris/misc/strawberry.png","gcompris/misc/peer.png","gcompris/misc/football.png","gcompris/misc/cerise.png","gcompris/misc/egg.png","gcompris/misc/glass.png","gcompris/misc/eggpot.png"]
00077     self.pixlist = []
00078     for i in range (len(self.symbollist)):
00079      pixbuf2 = gcompris.utils.load_pixmap(self.symbollist [i])
00080      h2 = 60
00081      w2 = pixbuf2.get_width()*h2/pixbuf2.get_height()
00082      self.pixlist.append (pixbuf2.scale_simple(w2, h2, gtk.gdk.
00083       INTERP_BILINEAR))
00084      del pixbuf2
00085     self.display_current_level()
00086     print("Gcompris_algorithm start.")
00087 
00088   def end(self):
00089     self.cleanup()
00090     print("Gcompris_algorithm end.")
00091 
00092   def ok(self):
00093     print("Gcompris_algorithm ok.")
00094 
00095   # Called by gcompris core
00096   def pause(self, pause):
00097 
00098     self.board_paused = pause
00099 
00100     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00101     # the game is won
00102     if(pause == 0):
00103       self.increment_level()
00104       self.gamewon = 0
00105 
00106     return
00107 
00108   # ----------------------------------------------------------------------
00109   # ----------------------------------------------------------------------
00110   # ----------------------------------------------------------------------
00111 
00112 
00113   def cleanup(self):
00114 
00115     # Remove the root item removes all the others inside it
00116     if self.rootitem != None:
00117      self.rootitem.destroy()
00118      self.rootitem = None
00119      gcompris.score.end()
00120 
00121   def display_current_level(self):
00122     self.cleanup()
00123     gcompris.score.start(gcompris.score.STYLE_NOTE, 570, 305,
00124      self.gcomprisBoard.number_of_sublevel)
00125     gcompris.bar_set_level(self.gcomprisBoard)
00126     gcompris.score.set(self.gcomprisBoard.sublevel)
00127 
00128     # Create our rootitem. We put each canvas item in it so at the end we
00129     # only have to kill it. The canvas deletes all the items it contains automaticaly.
00130     self.rootitem = self.gcomprisBoard.canvas.root().add(
00131       gnomecanvas.CanvasGroup,
00132       x=0.0,
00133       y=0.0
00134       )
00135 
00136     # Display our list of items
00137     for i in range(len(self.symbollist)):
00138      s = self.paint_image(i ,i ,390)
00139      s.connect ("event", self.apple_click, i)
00140 
00141     # Display the algorithm
00142     self.algo = random.choice(self.algos)
00143 
00144     # Create a uniq list of index in random order
00145     self.random_index = []
00146     self.selector  = range(self.anzahl)
00147     for i in range(self.anzahl):
00148       j=random.randrange(len(self.selector))
00149       self.random_index.append(self.selector[j])
00150       self.selector.pop(j)
00151 
00152     # Display what to search
00153     for i in range(len(self.symbollist)):
00154      self.paint_image(self.random_index[self.algo(i)], i, 45)
00155 
00156     # Display the uncomplete user area
00157     self.random_index = []
00158 
00159     for i in range(self.anzahl):
00160      self.random_index.append(random.randrange(len(self.symbollist)))
00161 
00162     for i in range(5):
00163      self.paint_image(self.random_index[self.algo(i)], i, 147)
00164 
00165     self.place = 5
00166     self.paint_qm ()
00167 
00168   def paint_qm (self):
00169     self.qm = self.rootitem.add(gnomecanvas.CanvasText, text = "?",
00170      x = self.place*self.distance+30+self.leftx,
00171      y = 185, fill_color_rgba = 0x000000ffL,
00172      font = gcompris.skin.get_font("gcompris/board/huge bold"))
00173 
00174   def key_press(self, keyval, commit_str, preedit_str):
00175     print("got key %i" % keyval)
00176     return False
00177 
00178   def set_level(self, level):
00179     print("Gcompris_algorithm set level. %i" % level)
00180     self.gcomprisBoard.level=level;
00181     self.gcomprisBoard.sublevel=1;
00182     self.cleanup()
00183     self.display_current_level()
00184 
00185   # Code that increments the sublevel and level
00186   # And bail out if no more levels are available
00187   # return 1 if continue, 0 if bail out
00188   def increment_level(self):
00189     self.gcomprisBoard.sublevel += 1
00190 
00191     if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
00192       # Try the next level
00193       self.gcomprisBoard.sublevel=1
00194       self.gcomprisBoard.level += 1
00195       if(self.gcomprisBoard.level>self.gcomprisBoard.maxlevel):
00196         # the current board is finished : bail out
00197         gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
00198         return 0
00199 
00200     self.display_current_level()
00201     return 1
00202 
00203 
00204   def apple_click (self, widget, event=None, index=0):
00205     if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1:
00206      if index == self.random_index[self.algo(self.place)]:
00207       self.qm.destroy()
00208       self.paint_image(index, self.place, 147)
00209       self.place +=1
00210       if self.place == self.anzahl:
00211         gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.TUX)
00212         return
00213       self.paint_qm ()
00214 
00215