Back to index

gcompris  8.2.2
Public Member Functions | Public Attributes
guessnumber.Gcompris_guessnumber Class Reference

List of all members.

Public Member Functions

def __init__
def start
def end
def ok
def repeat
def key_press
def pause
def set_level
def increment_level
def cleanup_game
def display_game
def entry_text
def enter_char_callback
def enter_callback
def move_step
def move

Public Attributes

 gcomprisBoard
 indicator
 indicator_s
 entry
 min
 max
 board_paused
 gamewon
 move_stepnum
 x
 y
 x_old
 y_old
 moving
 action_start
 anim
 velocity
 sw
 sh
 move_tick
 num_moveticks
 rotation
 movestep_timer
 orig_x
 orig_y
 target_x
 target_y
 helico_width
 helico_height
 rootitem
 solution
 widget

Detailed Description

Tux hide a number, you must guess it

Definition at line 33 of file guessnumber.py.


Constructor & Destructor Documentation

def guessnumber.Gcompris_guessnumber.__init__ (   self,
  gcomprisBoard 
)

Definition at line 37 of file guessnumber.py.

00037 
00038   def __init__(self, gcomprisBoard):
00039 
00040     self.gcomprisBoard = gcomprisBoard
00041 
00042     self.gcomprisBoard.disable_im_context = True
00043 
00044     # A text canvas item use to indicate it's over or lower
00045     self.indicator = None
00046     self.indicator_s = None
00047 
00048     # The text entry for the number
00049     self.entry = None
00050 
00051     # The min and max value that must be found
00052     self.min = -1
00053     self.max = -1
00054 
00055     # These are used to let us restart only after the bonus is displayed.
00056     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00057     self.board_paused  = 0
00058     self.gamewon       = 0
00059 
00060     # Manage the helico move
00061     self.move_stepnum = 0
00062     self.x = self.y = self.x_old = self.y_old = -1
00063     self.moving = False
00064     self.action_start = 0
00065     self.anim = None
00066     self.velocity = []
00067     self.sw = self.sh = 1
00068     self.move_tick = 30
00069     self.num_moveticks = 20
00070     self.rotation = 0
00071     self.movestep_timer = 0
00072 
00073     self.orig_x = 0
00074     self.orig_y = gcompris.BOARD_HEIGHT/2
00075     self.target_x = 700
00076     self.target_y = self.orig_y
00077 
00078     self.helico_width = -1
00079     self.helico_height = -1


Member Function Documentation

Definition at line 173 of file guessnumber.py.

00173 
00174   def cleanup_game(self):
00175     self.gamewon = False
00176     if self.movestep_timer != 0:
00177       gtk.timeout_remove(self.movestep_timer)
00178       self.movestep_timer = 0
00179 
00180     # Remove the root item removes all the others inside it
00181     self.rootitem.destroy()

Here is the caller graph for this function:

Definition at line 183 of file guessnumber.py.

00183 
00184   def display_game(self):
00185 
00186       # Create our rootitem. We put each canvas item in it so at the end we
00187       # only have to kill it. The canvas deletes all the items it contains automaticaly.
00188       self.rootitem = self.gcomprisBoard.canvas.root().add(
00189           gnomecanvas.CanvasGroup,
00190           x=0.0,
00191           y=0.0
00192           )
00193 
00194       self.min = 1
00195       self.max = 10
00196       if(self.gcomprisBoard.level == 2):
00197           self.max = 100
00198       elif(self.gcomprisBoard.level == 3):
00199           self.max = 500
00200       elif(self.gcomprisBoard.level == 4):
00201           self.max = 1000
00202 
00203       # Select the number to find
00204       self.solution = random.randint(self.min, self.max)
00205 
00206       text = _("Guess a number between %d and %d") %(self.min, self.max)
00207 
00208       self.rootitem.add(
00209           gnomecanvas.CanvasText,
00210           x=340.0 + 1.0,
00211           y=30.0 + 1.0,
00212           font=gcompris.skin.get_font("gcompris/title"),
00213           text=(text),
00214           fill_color="white",
00215           justification=gtk.JUSTIFY_CENTER
00216           )
00217 
00218       self.rootitem.add(
00219           gnomecanvas.CanvasText,
00220           x=340.0,
00221           y=30.0,
00222           font=gcompris.skin.get_font("gcompris/title"),
00223           text=(text),
00224           fill_color_rgba=0x1514c4ffL,
00225           justification=gtk.JUSTIFY_CENTER
00226           )
00227 
00228       self.indicator_s = self.rootitem.add(
00229           gnomecanvas.CanvasText,
00230           x=300.0 + 1.0,
00231           y=70.0 + 1.0,
00232           font=gcompris.skin.get_font("gcompris/subtitle"),
00233           text=(""),
00234           fill_color="white",
00235           justification=gtk.JUSTIFY_CENTER
00236           )
00237 
00238       self.indicator = self.rootitem.add(
00239           gnomecanvas.CanvasText,
00240           x=300.0,
00241           y=70.0,
00242           font=gcompris.skin.get_font("gcompris/subtitle"),
00243           text=(""),
00244           fill_color_rgba=0xff0006ffL,
00245           justification=gtk.JUSTIFY_CENTER
00246           )
00247 
00248       self.entry_text()
00249 
00250       #
00251       # Display the helico
00252       #
00253       pixmap = gcompris.utils.load_pixmap("gcompris/misc/tuxhelico.png")
00254       self.helico_width = pixmap.get_width()
00255       self.helico_height = pixmap.get_height()
00256       self.orig_x = self.x_old = self.x = pixmap.get_width()/2 + 10
00257       self.y_old = self.y = self.orig_y
00258 
00259       self.anim = self.rootitem.add(
00260         gnomecanvas.CanvasPixbuf,
00261         pixbuf = pixmap,
00262         x=self.x,
00263         y=self.y,
00264         anchor=gtk.ANCHOR_CENTER,
00265         )

Here is the caller graph for this function:

Definition at line 96 of file guessnumber.py.

00096 
00097   def end(self):
00098 
00099     gcompris.reset_locale()
00100 
00101     # Remove the root item removes all the others inside it
00102     self.cleanup_game()

Here is the call graph for this function:

Definition at line 304 of file guessnumber.py.

00304 
00305   def enter_callback(self, widget):
00306     text = widget.get_text()
00307 
00308     # Find a number game
00309     if str(self.solution) == text:
00310       self.indicator.set(text="")
00311       self.indicator_s.set(text="")
00312       self.gamewon = True
00313       gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.TUX)
00314     else:
00315       try:
00316         # can have been destroyed before by a delete action. No matter
00317         number = int(text)
00318       except:
00319         self.indicator.set(text=_("Please enter a number between %d and %d") %(self.min, self.max))
00320         self.indicator_s.set(text=_("Please enter a number between %d and %d") %(self.min, self.max))
00321         widget.set_text('')
00322         return
00323 
00324       if number > self.max or number <= 0:
00325         self.indicator.set(text=_("Out of range"))
00326         self.indicator_s.set(text=_("Out of range"))
00327       else:
00328         max_distance = max(self.max - self.solution, self.solution)
00329         distance_x = self.target_x - abs(self.solution - number) * float(self.target_x - self.orig_x) / max_distance
00330         distance_y = self.orig_y + float(((self.solution - number) * 170) / max_distance)
00331         if(number > self.solution):
00332           self.indicator.set(text=_("Too high"))
00333           self.indicator_s.set(text=_("Too high"))
00334         else:
00335           self.indicator.set(text=_("Too low"))
00336           self.indicator_s.set(text=_("Too low"))
00337 
00338         self.move(self.x_old, self.y_old,
00339                   distance_x,
00340                   distance_y)
00341 
00342     widget.set_text('')
00343 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 300 of file guessnumber.py.

00300 
00301   def enter_char_callback(self, widget):
00302       text = widget.get_text()
00303       widget.set_text(text.decode('utf8').upper().encode('utf8'))

Here is the caller graph for this function:

Definition at line 266 of file guessnumber.py.

00266 
00267   def entry_text(self):
00268     self.entry = gtk.Entry()
00269 
00270     self.entry.modify_font(pango.FontDescription("sans bold 24"))
00271     text_color = gtk.gdk.color_parse("blue")
00272     text_color_selected = gtk.gdk.color_parse("green")
00273 
00274     self.entry.modify_text(gtk.STATE_NORMAL, text_color)
00275     self.entry.modify_text(gtk.STATE_SELECTED, text_color_selected)
00276 
00277     self.entry.set_max_length(4)
00278     self.entry.connect("activate", self.enter_callback)
00279     self.entry.connect("changed", self.enter_char_callback)
00280 
00281     self.entry.show()
00282 
00283     self.widget = self.rootitem.add(
00284       gnomecanvas.CanvasWidget,
00285       widget=self.entry,
00286       x=730,
00287       y=30,
00288       width=100,
00289       height=46,
00290       anchor=gtk.ANCHOR_CENTER,
00291       size_pixels=False
00292       )
00293 
00294     self.widget.raise_to_top()
00295 
00296     # does not work. Why ?
00297     #self.gcomprisBoard.canvas.grab_focus()
00298     self.widget.grab_focus()
00299     self.entry.grab_focus()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 156 of file guessnumber.py.

00156 
00157   def increment_level(self):
00158     self.gcomprisBoard.sublevel += 1
00159 
00160     if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
00161       # Try the next level
00162       self.gcomprisBoard.sublevel=1
00163       self.gcomprisBoard.level += 1
00164       gcompris.bar_set_level(self.gcomprisBoard)
00165 
00166       if(self.gcomprisBoard.level>self.gcomprisBoard.maxlevel):
00167         # the current board is finished : bail out
00168         gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
00169         return 0
00170 
00171     return 1

Here is the caller graph for this function:

def guessnumber.Gcompris_guessnumber.key_press (   self,
  keyval,
  commit_str,
  preedit_str 
)

Definition at line 111 of file guessnumber.py.

00111 
00112   def key_press(self, keyval, commit_str, preedit_str):
00113     # Return  True  if you did process a key
00114     # Return  False if you did not processed a key
00115     #         (gtk need to send it to next widget)
00116     return False

def guessnumber.Gcompris_guessnumber.move (   self,
  x_old,
  y_old,
  x,
  y 
)

Definition at line 367 of file guessnumber.py.

00367 
00368   def move(self, x_old, y_old, x, y):
00369     if x == x_old and y == y_old:
00370       return
00371 
00372     self.entry.set_editable(False)
00373     self.x_old = x
00374     self.y_old = y
00375     self.x = x
00376     self.y = y
00377     self.velocity = [float(x-x_old), float(y-y_old)]
00378 
00379     if(x>x_old):
00380       self.rotation = 2
00381     elif (x<x_old):
00382       self.rotation = -2
00383     else:
00384       self.rotation = 0
00385     gcompris.utils.item_rotate(self.anim, self.rotation)
00386 
00387     self.moving = True
00388     self.move_stepnum = 0
00389 
00390     # it takes self.num_moveticks iterations of duration self.move_tick to move squares
00391     self.movestep_timer = gtk.timeout_add(self.move_tick, self.move_step)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 344 of file guessnumber.py.

00344 
00345   def move_step(self):
00346     if self.move_stepnum < self.num_moveticks-1:
00347       self.move_stepnum += 1
00348       x_old = self.anim.get_property("x")
00349       y_old = self.anim.get_property("y")
00350       x = self.anim.get_property("x") + self.velocity[0]/self.num_moveticks
00351       y = self.anim.get_property("y") + self.velocity[1]/self.num_moveticks
00352       ret = True
00353       self.anim.set(x=x, y=y)
00354       return True
00355     else:
00356       x = self.anim.get_property("x") + self.velocity[0]/self.num_moveticks
00357       y = self.anim.get_property("y") + self.velocity[1]/self.num_moveticks
00358       self.move_stepnum = 0
00359       self.moving = False
00360       self.movestep_timer = 0
00361       self.anim.set(x=(self.x_old),
00362                     y=(self.y_old))
00363       gcompris.utils.item_rotate(self.anim, 0)
00364       self.entry.set_editable(True)
00365       return False
00366 

Here is the caller graph for this function:

Definition at line 103 of file guessnumber.py.

00103 
00104   def ok(self):
00105     print("Gcompris_guessnumber ok.")
00106 

def guessnumber.Gcompris_guessnumber.pause (   self,
  pause 
)

Definition at line 117 of file guessnumber.py.

00117 
00118   def pause(self, pause):
00119     self.board_paused = pause
00120 
00121     # Hack for widget that can't be covered by bonus and/or help
00122     if pause:
00123        self.entry.hide()
00124     else:
00125       self.entry.show()
00126 
00127     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00128     # the game is won
00129     if(pause == 0 and self.gamewon):
00130       self.increment_level()
00131       self.gamewon = 0
00132 
00133       self.cleanup_game()
00134       self.display_game()
00135 
00136     return
00137 

Here is the call graph for this function:

Definition at line 107 of file guessnumber.py.

00107 
00108   def repeat(self):
00109     print("Gcompris_guessnumber repeat.")
00110 

Here is the caller graph for this function:

def guessnumber.Gcompris_guessnumber.set_level (   self,
  level 
)

Definition at line 138 of file guessnumber.py.

00138 
00139   def set_level(self, level):
00140     self.gcomprisBoard.level=level;
00141     self.gcomprisBoard.sublevel=1;
00142 
00143     # Set the level in the control bar
00144     gcompris.bar_set_level(self.gcomprisBoard);
00145 
00146     self.cleanup_game()
00147     self.display_game()

Here is the call graph for this function:

Definition at line 80 of file guessnumber.py.

00080 
00081   def start(self):
00082 
00083     self.gcomprisBoard.level=1
00084     self.gcomprisBoard.maxlevel=4
00085     self.gcomprisBoard.sublevel=1
00086     self.gcomprisBoard.number_of_sublevel=1
00087 
00088     gcompris.bar_set(gcompris.BAR_OK|gcompris.BAR_LEVEL)
00089 
00090     gcompris.bar_set_level(self.gcomprisBoard)
00091 
00092     gcompris.set_background(self.gcomprisBoard.canvas.root(), "images/cave.png")
00093 
00094     self.display_game()
00095 

Here is the call graph for this function:


Member Data Documentation

Definition at line 63 of file guessnumber.py.

Definition at line 64 of file guessnumber.py.

Definition at line 56 of file guessnumber.py.

Definition at line 48 of file guessnumber.py.

Definition at line 57 of file guessnumber.py.

Definition at line 39 of file guessnumber.py.

Definition at line 78 of file guessnumber.py.

Definition at line 77 of file guessnumber.py.

Definition at line 44 of file guessnumber.py.

Definition at line 45 of file guessnumber.py.

Definition at line 52 of file guessnumber.py.

Definition at line 51 of file guessnumber.py.

Definition at line 60 of file guessnumber.py.

Definition at line 67 of file guessnumber.py.

Definition at line 70 of file guessnumber.py.

Definition at line 62 of file guessnumber.py.

Definition at line 68 of file guessnumber.py.

Definition at line 72 of file guessnumber.py.

Definition at line 73 of file guessnumber.py.

Definition at line 187 of file guessnumber.py.

Definition at line 69 of file guessnumber.py.

Definition at line 66 of file guessnumber.py.

Definition at line 203 of file guessnumber.py.

Definition at line 66 of file guessnumber.py.

Definition at line 74 of file guessnumber.py.

Definition at line 75 of file guessnumber.py.

Definition at line 65 of file guessnumber.py.

Definition at line 282 of file guessnumber.py.

Definition at line 61 of file guessnumber.py.

Definition at line 61 of file guessnumber.py.

Definition at line 61 of file guessnumber.py.

Definition at line 61 of file guessnumber.py.


The documentation for this class was generated from the following file: