~ml-launchpad/ubuntu/natty/gcompris/fix-for-777349

« back to all changes in this revision

Viewing changes to src/boards/python/guessnumber.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#  gcompris - guessnumber
2
 
#
3
 
# Copyright (C) 2005 Bruno Coudoin / Clement Coudoin
4
 
#
5
 
#   This program is free software; you can redistribute it and/or modify
6
 
#   it under the terms of the GNU General Public License as published by
7
 
#   the Free Software Foundation; either version 3 of the License, or
8
 
#   (at your option) any later version.
9
 
#
10
 
#   This program is distributed in the hope that it will be useful,
11
 
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
#   GNU General Public License for more details.
14
 
#
15
 
#   You should have received a copy of the GNU General Public License
16
 
#   along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 
#
18
 
 
19
 
import gnomecanvas
20
 
import gcompris
21
 
import gcompris.utils
22
 
import gcompris.skin
23
 
import gcompris.admin
24
 
import gcompris.bonus
25
 
import gtk
26
 
import gtk.gdk
27
 
import random
28
 
import pango
29
 
 
30
 
from gcompris import gcompris_gettext as _
31
 
 
32
 
class Gcompris_guessnumber:
33
 
  """Tux hide a number, you must guess it"""
34
 
 
35
 
 
36
 
  def __init__(self, gcomprisBoard):
37
 
 
38
 
    self.gcomprisBoard = gcomprisBoard
39
 
 
40
 
    self.gcomprisBoard.disable_im_context = True
41
 
 
42
 
    # A text canvas item use to indicate it's over or lower
43
 
    self.indicator = None
44
 
    self.indicator_s = None
45
 
 
46
 
    # The text entry for the number
47
 
    self.entry = None
48
 
 
49
 
    # The min and max value that must be found
50
 
    self.min = -1
51
 
    self.max = -1
52
 
 
53
 
    # These are used to let us restart only after the bonus is displayed.
54
 
    # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
55
 
    self.board_paused  = 0
56
 
    self.gamewon       = 0
57
 
 
58
 
    # Manage the helico move
59
 
    self.move_stepnum = 0
60
 
    self.x = self.y = self.x_old = self.y_old = -1
61
 
    self.moving = False
62
 
    self.action_start = 0
63
 
    self.anim = None
64
 
    self.velocity = []
65
 
    self.sw = self.sh = 1
66
 
    self.move_tick = 30
67
 
    self.num_moveticks = 20
68
 
    self.rotation = 0
69
 
    self.movestep_timer = 0
70
 
 
71
 
    self.orig_x = 0
72
 
    self.orig_y = gcompris.BOARD_HEIGHT/2
73
 
    self.target_x = 700
74
 
    self.target_y = self.orig_y
75
 
 
76
 
    self.helico_width = -1
77
 
    self.helico_height = -1
78
 
 
79
 
  def start(self):
80
 
 
81
 
    self.gcomprisBoard.level=1
82
 
    self.gcomprisBoard.maxlevel=4
83
 
    self.gcomprisBoard.sublevel=1
84
 
    self.gcomprisBoard.number_of_sublevel=1
85
 
 
86
 
    gcompris.bar_set(gcompris.BAR_OK|gcompris.BAR_LEVEL)
87
 
 
88
 
    gcompris.bar_set_level(self.gcomprisBoard)
89
 
 
90
 
    gcompris.set_background(self.gcomprisBoard.canvas.root(), "opt/cave.png")
91
 
 
92
 
    self.display_game()
93
 
 
94
 
 
95
 
  def end(self):
96
 
 
97
 
    gcompris.reset_locale()
98
 
 
99
 
    # Remove the root item removes all the others inside it
100
 
    self.cleanup_game()
101
 
 
102
 
  def ok(self):
103
 
    print("Gcompris_guessnumber ok.")
104
 
 
105
 
 
106
 
  def repeat(self):
107
 
    print("Gcompris_guessnumber repeat.")
108
 
 
109
 
 
110
 
  def key_press(self, keyval, commit_str, preedit_str):
111
 
    # Return  True  if you did process a key
112
 
    # Return  False if you did not processed a key
113
 
    #         (gtk need to send it to next widget)
114
 
    return False
115
 
 
116
 
  def pause(self, pause):
117
 
    self.board_paused = pause
118
 
 
119
 
    # Hack for widget that can't be covered by bonus and/or help
120
 
    if pause:
121
 
       self.entry.hide()
122
 
    else:
123
 
      self.entry.show()
124
 
 
125
 
    # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
126
 
    # the game is won
127
 
    if(pause == 0 and self.gamewon):
128
 
      self.increment_level()
129
 
      self.gamewon = 0
130
 
 
131
 
      self.cleanup_game()
132
 
      self.display_game()
133
 
 
134
 
    return
135
 
 
136
 
 
137
 
  def set_level(self, level):
138
 
    self.gcomprisBoard.level=level;
139
 
    self.gcomprisBoard.sublevel=1;
140
 
 
141
 
    # Set the level in the control bar
142
 
    gcompris.bar_set_level(self.gcomprisBoard);
143
 
 
144
 
    self.cleanup_game()
145
 
    self.display_game()
146
 
 
147
 
  #
148
 
  # End of Initialisation
149
 
  # ---------------------
150
 
  #
151
 
 
152
 
  # Code that increments the sublevel and level
153
 
  # And bail out if no more levels are available
154
 
  # return 1 if continue, 0 if bail out
155
 
  def increment_level(self):
156
 
    self.gcomprisBoard.sublevel += 1
157
 
 
158
 
    if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
159
 
      # Try the next level
160
 
      self.gcomprisBoard.sublevel=1
161
 
      self.gcomprisBoard.level += 1
162
 
      gcompris.bar_set_level(self.gcomprisBoard)
163
 
 
164
 
      if(self.gcomprisBoard.level>self.gcomprisBoard.maxlevel):
165
 
        # the current board is finished : bail out
166
 
        gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
167
 
        return 0
168
 
 
169
 
    return 1
170
 
 
171
 
  # Display the board game
172
 
  def cleanup_game(self):
173
 
    self.gamewon = False
174
 
    if self.movestep_timer != 0:
175
 
      gtk.timeout_remove(self.movestep_timer)
176
 
      self.movestep_timer = 0
177
 
 
178
 
    # Remove the root item removes all the others inside it
179
 
    self.rootitem.destroy()
180
 
 
181
 
  # Display the board game
182
 
  def display_game(self):
183
 
 
184
 
      # Create our rootitem. We put each canvas item in it so at the end we
185
 
      # only have to kill it. The canvas deletes all the items it contains automaticaly.
186
 
      self.rootitem = self.gcomprisBoard.canvas.root().add(
187
 
          gnomecanvas.CanvasGroup,
188
 
          x=0.0,
189
 
          y=0.0
190
 
          )
191
 
 
192
 
      self.min = 1
193
 
      self.max = 10
194
 
      if(self.gcomprisBoard.level == 2):
195
 
          self.max = 100
196
 
      elif(self.gcomprisBoard.level == 3):
197
 
          self.max = 500
198
 
      elif(self.gcomprisBoard.level == 4):
199
 
          self.max = 1000
200
 
 
201
 
      # Select the number to find
202
 
      self.solution = random.randint(self.min, self.max)
203
 
 
204
 
      text = _("Guess a number between %d and %d") %(self.min, self.max)
205
 
 
206
 
      self.rootitem.add(
207
 
          gnomecanvas.CanvasText,
208
 
          x=340.0 + 1.0,
209
 
          y=30.0 + 1.0,
210
 
          font=gcompris.skin.get_font("gcompris/title"),
211
 
          text=(text),
212
 
          fill_color="white",
213
 
          justification=gtk.JUSTIFY_CENTER
214
 
          )
215
 
 
216
 
      self.rootitem.add(
217
 
          gnomecanvas.CanvasText,
218
 
          x=340.0,
219
 
          y=30.0,
220
 
          font=gcompris.skin.get_font("gcompris/title"),
221
 
          text=(text),
222
 
          fill_color_rgba=0x1514c4ffL,
223
 
          justification=gtk.JUSTIFY_CENTER
224
 
          )
225
 
 
226
 
      self.indicator_s = self.rootitem.add(
227
 
          gnomecanvas.CanvasText,
228
 
          x=300.0 + 1.0,
229
 
          y=70.0 + 1.0,
230
 
          font=gcompris.skin.get_font("gcompris/subtitle"),
231
 
          text=(""),
232
 
          fill_color="white",
233
 
          justification=gtk.JUSTIFY_CENTER
234
 
          )
235
 
 
236
 
      self.indicator = self.rootitem.add(
237
 
          gnomecanvas.CanvasText,
238
 
          x=300.0,
239
 
          y=70.0,
240
 
          font=gcompris.skin.get_font("gcompris/subtitle"),
241
 
          text=(""),
242
 
          fill_color_rgba=0xff0006ffL,
243
 
          justification=gtk.JUSTIFY_CENTER
244
 
          )
245
 
 
246
 
      self.entry_text()
247
 
 
248
 
      #
249
 
      # Display the helico
250
 
      #
251
 
      pixmap = gcompris.utils.load_pixmap("gcompris/misc/tuxhelico.png")
252
 
      self.helico_width = pixmap.get_width()
253
 
      self.helico_height = pixmap.get_height()
254
 
      self.orig_x = self.x_old = self.x = pixmap.get_width()/2 + 10
255
 
      self.y_old = self.y = self.orig_y
256
 
 
257
 
      self.anim = self.rootitem.add(
258
 
        gnomecanvas.CanvasPixbuf,
259
 
        pixbuf = pixmap,
260
 
        x=self.x,
261
 
        y=self.y,
262
 
        anchor=gtk.ANCHOR_CENTER,
263
 
        )
264
 
 
265
 
  def entry_text(self):
266
 
    self.entry = gtk.Entry()
267
 
 
268
 
    self.entry.modify_font(pango.FontDescription("sans bold 24"))
269
 
    text_color = gtk.gdk.color_parse("blue")
270
 
    text_color_selected = gtk.gdk.color_parse("green")
271
 
 
272
 
    self.entry.modify_text(gtk.STATE_NORMAL, text_color)
273
 
    self.entry.modify_text(gtk.STATE_SELECTED, text_color_selected)
274
 
 
275
 
    self.entry.set_max_length(4)
276
 
    self.entry.connect("activate", self.enter_callback)
277
 
    self.entry.connect("changed", self.enter_char_callback)
278
 
 
279
 
    self.entry.show()
280
 
 
281
 
    self.widget = self.rootitem.add(
282
 
      gnomecanvas.CanvasWidget,
283
 
      widget=self.entry,
284
 
      x=730,
285
 
      y=30,
286
 
      width=100,
287
 
      height=46,
288
 
      anchor=gtk.ANCHOR_CENTER,
289
 
      size_pixels=False
290
 
      )
291
 
 
292
 
    self.widget.raise_to_top()
293
 
 
294
 
    # does not work. Why ?
295
 
    #self.gcomprisBoard.canvas.grab_focus()
296
 
    self.widget.grab_focus()
297
 
    self.entry.grab_focus()
298
 
 
299
 
  def enter_char_callback(self, widget):
300
 
      text = widget.get_text()
301
 
      widget.set_text(text.decode('utf8').upper().encode('utf8'))
302
 
 
303
 
  def enter_callback(self, widget):
304
 
    text = widget.get_text()
305
 
 
306
 
    # Find a number game
307
 
    if str(self.solution) == text:
308
 
      self.indicator.set(text="")
309
 
      self.indicator_s.set(text="")
310
 
      self.gamewon = True
311
 
      gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.TUX)
312
 
    else:
313
 
      try:
314
 
        # can have been destroyed before by a delete action. No matter
315
 
        number = int(text)
316
 
      except:
317
 
        self.indicator.set(text=_("Please enter a number between %d and %d") %(self.min, self.max))
318
 
        self.indicator_s.set(text=_("Please enter a number between %d and %d") %(self.min, self.max))
319
 
        widget.set_text('')
320
 
        return
321
 
 
322
 
      if number > self.max or number <= 0:
323
 
        self.indicator.set(text=_("Out of range"))
324
 
        self.indicator_s.set(text=_("Out of range"))
325
 
      else:
326
 
        max_distance = max(self.max - self.solution, self.solution)
327
 
        distance_x = self.target_x - abs(self.solution - number) * float(self.target_x - self.orig_x) / max_distance
328
 
        distance_y = self.orig_y + float(((self.solution - number) * 170) / max_distance)
329
 
        if(number > self.solution):
330
 
          self.indicator.set(text=_("Too high"))
331
 
          self.indicator_s.set(text=_("Too high"))
332
 
        else:
333
 
          self.indicator.set(text=_("Too low"))
334
 
          self.indicator_s.set(text=_("Too low"))
335
 
 
336
 
        self.move(self.x_old, self.y_old,
337
 
                  distance_x,
338
 
                  distance_y)
339
 
 
340
 
    widget.set_text('')
341
 
 
342
 
 
343
 
  def move_step(self):
344
 
    if self.move_stepnum < self.num_moveticks-1:
345
 
      self.move_stepnum += 1
346
 
      x_old = self.anim.get_property("x")
347
 
      y_old = self.anim.get_property("y")
348
 
      x = self.anim.get_property("x") + self.velocity[0]/self.num_moveticks
349
 
      y = self.anim.get_property("y") + self.velocity[1]/self.num_moveticks
350
 
      ret = True
351
 
      self.anim.set(x=x, y=y)
352
 
      return True
353
 
    else:
354
 
      x = self.anim.get_property("x") + self.velocity[0]/self.num_moveticks
355
 
      y = self.anim.get_property("y") + self.velocity[1]/self.num_moveticks
356
 
      self.move_stepnum = 0
357
 
      self.moving = False
358
 
      self.movestep_timer = 0
359
 
      self.anim.set(x=(self.x_old),
360
 
                    y=(self.y_old))
361
 
      gcompris.utils.item_rotate(self.anim, 0)
362
 
      self.entry.set_editable(True)
363
 
      return False
364
 
 
365
 
 
366
 
  def move(self, x_old, y_old, x, y):
367
 
    if x == x_old and y == y_old:
368
 
      return
369
 
 
370
 
    self.entry.set_editable(False)
371
 
    self.x_old = x
372
 
    self.y_old = y
373
 
    self.x = x
374
 
    self.y = y
375
 
    self.velocity = [float(x-x_old), float(y-y_old)]
376
 
 
377
 
    if(x>x_old):
378
 
      self.rotation = 2
379
 
    elif (x<x_old):
380
 
      self.rotation = -2
381
 
    else:
382
 
      self.rotation = 0
383
 
    gcompris.utils.item_rotate(self.anim, self.rotation)
384
 
 
385
 
    self.moving = True
386
 
    self.move_stepnum = 0
387
 
 
388
 
    # it takes self.num_moveticks iterations of duration self.move_tick to move squares
389
 
    self.movestep_timer = gtk.timeout_add(self.move_tick, self.move_step)