1
# gcompris - guessnumber
3
# Copyright (C) 2005 Bruno Coudoin / Clement Coudoin
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.
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.
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/>.
30
from gcompris import gcompris_gettext as _
32
class Gcompris_guessnumber:
33
"""Tux hide a number, you must guess it"""
36
def __init__(self, gcomprisBoard):
38
self.gcomprisBoard = gcomprisBoard
40
self.gcomprisBoard.disable_im_context = True
42
# A text canvas item use to indicate it's over or lower
44
self.indicator_s = None
46
# The text entry for the number
49
# The min and max value that must be found
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)
58
# Manage the helico move
60
self.x = self.y = self.x_old = self.y_old = -1
67
self.num_moveticks = 20
69
self.movestep_timer = 0
72
self.orig_y = gcompris.BOARD_HEIGHT/2
74
self.target_y = self.orig_y
76
self.helico_width = -1
77
self.helico_height = -1
81
self.gcomprisBoard.level=1
82
self.gcomprisBoard.maxlevel=4
83
self.gcomprisBoard.sublevel=1
84
self.gcomprisBoard.number_of_sublevel=1
86
gcompris.bar_set(gcompris.BAR_OK|gcompris.BAR_LEVEL)
88
gcompris.bar_set_level(self.gcomprisBoard)
90
gcompris.set_background(self.gcomprisBoard.canvas.root(), "opt/cave.png")
97
gcompris.reset_locale()
99
# Remove the root item removes all the others inside it
103
print("Gcompris_guessnumber ok.")
107
print("Gcompris_guessnumber repeat.")
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)
116
def pause(self, pause):
117
self.board_paused = pause
119
# Hack for widget that can't be covered by bonus and/or help
125
# When the bonus is displayed, it call us first with pause(1) and then with pause(0)
127
if(pause == 0 and self.gamewon):
128
self.increment_level()
137
def set_level(self, level):
138
self.gcomprisBoard.level=level;
139
self.gcomprisBoard.sublevel=1;
141
# Set the level in the control bar
142
gcompris.bar_set_level(self.gcomprisBoard);
148
# End of Initialisation
149
# ---------------------
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
158
if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
160
self.gcomprisBoard.sublevel=1
161
self.gcomprisBoard.level += 1
162
gcompris.bar_set_level(self.gcomprisBoard)
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)
171
# Display the board game
172
def cleanup_game(self):
174
if self.movestep_timer != 0:
175
gtk.timeout_remove(self.movestep_timer)
176
self.movestep_timer = 0
178
# Remove the root item removes all the others inside it
179
self.rootitem.destroy()
181
# Display the board game
182
def display_game(self):
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,
194
if(self.gcomprisBoard.level == 2):
196
elif(self.gcomprisBoard.level == 3):
198
elif(self.gcomprisBoard.level == 4):
201
# Select the number to find
202
self.solution = random.randint(self.min, self.max)
204
text = _("Guess a number between %d and %d") %(self.min, self.max)
207
gnomecanvas.CanvasText,
210
font=gcompris.skin.get_font("gcompris/title"),
213
justification=gtk.JUSTIFY_CENTER
217
gnomecanvas.CanvasText,
220
font=gcompris.skin.get_font("gcompris/title"),
222
fill_color_rgba=0x1514c4ffL,
223
justification=gtk.JUSTIFY_CENTER
226
self.indicator_s = self.rootitem.add(
227
gnomecanvas.CanvasText,
230
font=gcompris.skin.get_font("gcompris/subtitle"),
233
justification=gtk.JUSTIFY_CENTER
236
self.indicator = self.rootitem.add(
237
gnomecanvas.CanvasText,
240
font=gcompris.skin.get_font("gcompris/subtitle"),
242
fill_color_rgba=0xff0006ffL,
243
justification=gtk.JUSTIFY_CENTER
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
257
self.anim = self.rootitem.add(
258
gnomecanvas.CanvasPixbuf,
262
anchor=gtk.ANCHOR_CENTER,
265
def entry_text(self):
266
self.entry = gtk.Entry()
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")
272
self.entry.modify_text(gtk.STATE_NORMAL, text_color)
273
self.entry.modify_text(gtk.STATE_SELECTED, text_color_selected)
275
self.entry.set_max_length(4)
276
self.entry.connect("activate", self.enter_callback)
277
self.entry.connect("changed", self.enter_char_callback)
281
self.widget = self.rootitem.add(
282
gnomecanvas.CanvasWidget,
288
anchor=gtk.ANCHOR_CENTER,
292
self.widget.raise_to_top()
294
# does not work. Why ?
295
#self.gcomprisBoard.canvas.grab_focus()
296
self.widget.grab_focus()
297
self.entry.grab_focus()
299
def enter_char_callback(self, widget):
300
text = widget.get_text()
301
widget.set_text(text.decode('utf8').upper().encode('utf8'))
303
def enter_callback(self, widget):
304
text = widget.get_text()
307
if str(self.solution) == text:
308
self.indicator.set(text="")
309
self.indicator_s.set(text="")
311
gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.TUX)
314
# can have been destroyed before by a delete action. No matter
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))
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"))
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"))
333
self.indicator.set(text=_("Too low"))
334
self.indicator_s.set(text=_("Too low"))
336
self.move(self.x_old, self.y_old,
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
351
self.anim.set(x=x, y=y)
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
358
self.movestep_timer = 0
359
self.anim.set(x=(self.x_old),
361
gcompris.utils.item_rotate(self.anim, 0)
362
self.entry.set_editable(True)
366
def move(self, x_old, y_old, x, y):
367
if x == x_old and y == y_old:
370
self.entry.set_editable(False)
375
self.velocity = [float(x-x_old), float(y-y_old)]
383
gcompris.utils.item_rotate(self.anim, self.rotation)
386
self.move_stepnum = 0
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)