dicey_dicey.py 12.3 KB
Newer Older
Srishti Sethi's avatar
Srishti Sethi committed
1 2
#  gcompris - dicey_dicey.py
#
SrishAkaTux's avatar
SrishAkaTux committed
3
# Copyright (C) 2003, 2008 Bruno Coudoin | Srishti Sethi
Srishti Sethi's avatar
Srishti Sethi committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# dicey_dicey activity.
import gtk
import gtk.gdk
import gcompris
import gcompris.utils
import gcompris.skin
import gcompris.score
import goocanvas
import pango
import random
import gcompris.bonus
from BrailleChar import *
30
from BrailleMap import *
Srishti Sethi's avatar
Srishti Sethi committed
31 32
from gcompris import gcompris_gettext as _

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#Constants Declaration
COLOR_ON = 0XFFFFFFFFL
COLOR_OFF = 0X00000000L
PLUS_COLOR_ON = 0XFF0000FFL
MINUS_COLOR_ON = 0XFF00FFL
MULT_COLOR_ON = 0X00FFFFL
DIV_COLOR_ON = 0x3D59ABL

CIRCLE_STROKE = "black"
CIRCLE_FILL = "#660066"
SYMBOL_OFF = "#DfDfDf"
CELL_WIDTH = 30
SYMBOL = ['+','_','*','/']
COLOR_LIST = ["red","green","blue","dark green"]

#Declaring list of all questions
#used in different levels
Srishti Sethi's avatar
Srishti Sethi committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
QUESTIONS= [
    [# level 1
     [3,"-",2],[1,"+",1],[2,"-",2]
     ],
    [# level 2
     [2,"+",1],[3,"+",2],[2,"+",2]
     ],
    [#level 3
    [4 ,"+" ,4],[4, "+", 5],[9 ,"-", 3],[9 , "-" ,2]
     ],
    [ #level 4
     [0,"*",1],[1,"+",1],[3,"-",3],[6,"/",3]
    ],
    [# level 5
     [3 , "+" , 2],[5, "-", 2],[4, "*", 1],[8,"/",2]
     ],
     [# level 6
      [4, "+", 5],[9, "-", 2],[3, "*", 2],[8, "/", 1]
      ]
 ]
Srishti Sethi's avatar
Srishti Sethi committed
70 71 72 73 74 75 76 77 78 79
class Gcompris_dicey_dicey:
  """Empty gcompris python class"""

  def __init__(self, gcomprisBoard):
    # Save the gcomprisBoard, it defines everything we need
    # to know from the core
    self.gcomprisBoard = gcomprisBoard

    self.gcomprisBoard.level=1
    self.gcomprisBoard.sublevel=1
Srishti Sethi's avatar
Srishti Sethi committed
80
    self.gcomprisBoard.number_of_sublevel=1
Srishti Sethi's avatar
Srishti Sethi committed
81 82 83
    self.gcomprisBoard.maxlevel = 6

    self.counter = 0
84 85 86 87 88 89

    # These are used to let us restart only after the bonus is displayed.
    # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
    self.board_paused  = 0
    self.gamewon       = 0

Srishti Sethi's avatar
Srishti Sethi committed
90 91 92
    # Needed to get key_press
    gcomprisBoard.disable_im_context = True

Srishti Sethi's avatar
Srishti Sethi committed
93 94 95 96 97
    for index in range(6):
        random.shuffle(QUESTIONS[index])
    random.shuffle(SYMBOL)


Srishti Sethi's avatar
Srishti Sethi committed
98 99 100 101 102 103 104 105
  def start(self):

    # Set the buttons we want in the bar
    gcompris.bar_set(gcompris.BAR_LEVEL)
    gcompris.bar_location(300,-1,0.8)
    # Set a background image
    gcompris.set_default_background(self.gcomprisBoard.canvas.get_root_item())

106 107 108 109 110 111 112
    #Boolean variable declaration
    self.mapActive = False

    #REPEAT ICON
    gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT_ICON)
    gcompris.bar_location(300,-1,0.6)

Srishti Sethi's avatar
Srishti Sethi committed
113 114 115 116 117 118
    # Create our rootitem. We put each canvas item in it so at the end we
    # only have to kill it. The canvas deletes all the items it contains
    # automaticaly.
    self.rootitem = goocanvas.Group(parent =
                                    self.gcomprisBoard.canvas.get_root_item())
    gcompris.bar_set_level(self.gcomprisBoard)
Srishti Sethi's avatar
Srishti Sethi committed
119

Srishti Sethi's avatar
Srishti Sethi committed
120

121 122 123 124
    #Display title of activity
    goocanvas.Text(parent = self.rootitem,
                   x=400.0,
                   y=100.0,
125
                   text=_("Dicey - Dicey"),
126 127 128 129 130
                   fill_color="black",
                   anchor = gtk.ANCHOR_CENTER,
                   alignment = pango.ALIGN_CENTER,
                   font = 'SANS 20'
                   )
Srishti Sethi's avatar
Srishti Sethi committed
131 132 133
    self.display_function(self.gcomprisBoard.level)

  def display_function(self,level):
Srishti Sethi's avatar
Srishti Sethi committed
134
      if(level == 1 or level == 2 or level == 3):
135 136
          self.dicey_dicey()
          self.calculate(level)
Srishti Sethi's avatar
Srishti Sethi committed
137 138 139 140 141
          gcompris.set_background(self.gcomprisBoard.canvas.get_root_item(),
                            "dicey_dicey/dice.svg")
          ok = goocanvas.Svg(parent = self.rootitem,
                         svg_handle = gcompris.skin.svg_get(),
                         svg_id = "#OK",
142
                         tooltip = _("Click to confirm your selection of dots")
Srishti Sethi's avatar
Srishti Sethi committed
143 144 145 146
                         )
          ok.translate(40,-30)
          ok.connect("button_press_event", self.ok_event,level)
          gcompris.utils.item_focus_init(ok, None)
Srishti Sethi's avatar
Srishti Sethi committed
147 148 149 150 151
      elif(level == 4 or level == 5 or level == 6):
          self.dicey_dicey()
          self.dicey_dicey1()
          self.calculate(level)

Srishti Sethi's avatar
Srishti Sethi committed
152

153
  def calculate(self,level):
Srishti Sethi's avatar
Srishti Sethi committed
154 155 156
      i = QUESTIONS[level - 1][self.counter][0]
      j= QUESTIONS[level - 1][self.counter][2]
      self.random_sign = QUESTIONS[level - 1][self.counter][1]
157

Srishti Sethi's avatar
Srishti Sethi committed
158 159
      #Mathematical calculations
      if(self.random_sign == '+'):
160
          self.result = i + j
Srishti Sethi's avatar
Srishti Sethi committed
161
      elif(self.random_sign == '-'):
162 163 164
          self.result = i - j
      elif(self.random_sign == '*'):
          self.result = i * j
Srishti Sethi's avatar
Srishti Sethi committed
165
      elif(self.random_sign == '/'):
166
          self.result = i / j
Srishti Sethi's avatar
Srishti Sethi committed
167

168

Srishti Sethi's avatar
Srishti Sethi committed
169 170 171 172 173 174
      BrailleChar(self.rootitem, 120, 160, 70 , i, COLOR_ON, COLOR_OFF ,
                  CIRCLE_FILL, CIRCLE_STROKE, False, False ,False, None)
      BrailleChar(self.rootitem, 360, 160, 70, j, COLOR_ON, COLOR_OFF ,
                  CIRCLE_FILL, CIRCLE_STROKE, False, False, False, None)
      goocanvas.Text(parent = self.rootitem, x=140.0 ,y=330.0,
                     text="%i" % i, anchor = gtk.ANCHOR_CENTER,
Srishti Sethi's avatar
Srishti Sethi committed
175
                     font='SANS 25')
Srishti Sethi's avatar
Srishti Sethi committed
176 177 178
      goocanvas.Text(parent = self.rootitem,
                     x=400,y=330.0,
                     text="%i" % j,
Srishti Sethi's avatar
Srishti Sethi committed
179 180
                     anchor = gtk.ANCHOR_CENTER,
                     font='SANS 25')
Srishti Sethi's avatar
Srishti Sethi committed
181 182 183 184 185
      if(level == 4 or level == 5 or level == 6):
          BrailleChar(self.rootitem, 600, 160, 70, self.result,
                  COLOR_ON, COLOR_OFF, CIRCLE_FILL,
                  CIRCLE_STROKE, False, False, False, None)
          goocanvas.Text(parent = self.rootitem,
Srishti Sethi's avatar
Srishti Sethi committed
186
                     x=280.0,
Srishti Sethi's avatar
Srishti Sethi committed
187
                     y=230.0,
Srishti Sethi's avatar
Srishti Sethi committed
188 189
                     text='?',
                     anchor = gtk.ANCHOR_CENTER,
Srishti Sethi's avatar
Srishti Sethi committed
190 191 192
                     font='SANS 30')
          goocanvas.Text(parent = self.rootitem, x=640.0,y=330.0,
                     text="%i" % self.result,anchor = gtk.ANCHOR_CENTER,
Srishti Sethi's avatar
Srishti Sethi committed
193 194
                     font='SANS 25')

Srishti Sethi's avatar
Srishti Sethi committed
195 196
      elif(level == 1 or level == 2 or level == 3):
          goocanvas.Text(parent = self.rootitem,
Srishti Sethi's avatar
Srishti Sethi committed
197
                     x=280.0,
Srishti Sethi's avatar
Srishti Sethi committed
198 199
                     y=230.0,
                     text=self.random_sign,
Srishti Sethi's avatar
Srishti Sethi committed
200
                     anchor = gtk.ANCHOR_CENTER,
Srishti Sethi's avatar
Srishti Sethi committed
201 202 203 204
                     font='SANS 30')
          BrailleChar(self.rootitem,600,160,70,'' ,
                      COLOR_ON, COLOR_OFF, CIRCLE_FILL,
                      CIRCLE_STROKE, False, True, False,
205
                      callback = self.letter_change, braille_letter = "number")
Srishti Sethi's avatar
Srishti Sethi committed
206 207


208 209
  def dicey_dicey(self):
      #Display dices
210
      for index in range(3):
Srishti Sethi's avatar
Srishti Sethi committed
211 212
          item = goocanvas.Image(parent = self.rootitem,
                                 pixbuf = gcompris.utils.load_pixmap("dicey_dicey/diceyo.svg"),
213
                                 x = 80 *(3*index+1),
Srishti Sethi's avatar
Srishti Sethi committed
214 215 216 217 218 219
                                 y = 150,
                                 )
      goocanvas.Text(parent = self.rootitem,
                   x=520.0,y=230.0,
                   text="=",
                   anchor = gtk.ANCHOR_CENTER,
Srishti Sethi's avatar
Srishti Sethi committed
220
                   font='SANS 30')
Srishti Sethi's avatar
Srishti Sethi committed
221
      goocanvas.Text(parent = self.rootitem,
Srishti Sethi's avatar
Srishti Sethi committed
222
                   x=655.0,y=323.0,
Srishti Sethi's avatar
Srishti Sethi committed
223 224
                   text="___",
                   anchor = gtk.ANCHOR_CENTER,
Srishti Sethi's avatar
Srishti Sethi committed
225
                       font='SANS 30')
Srishti Sethi's avatar
Srishti Sethi committed
226 227


228
  def dicey_dicey1(self):
Srishti Sethi's avatar
Srishti Sethi committed
229 230 231 232 233
      gcompris.set_background(self.gcomprisBoard.canvas.get_root_item(),
                            "dicey_dicey/dice_area.svg")
      goocanvas.Text(parent = self.rootitem,
                     x=300.0,
                     y=358.0,
234
                     text = _("Choose an operator to indicate the question mark"),
Srishti Sethi's avatar
Srishti Sethi committed
235 236 237 238
                     anchor = gtk.ANCHOR_CENTER,
                     font='SANS 12')

      #Display operator images
239 240 241 242 243 244
      self.cell = []
      for index in range(4):
          Item = goocanvas.Image(parent = self.rootitem,
                         pixbuf = gcompris.utils.load_pixmap("dicey_dicey/cube.svg"),
                         x=48 * (2*index + 5),
                         y=380
Srishti Sethi's avatar
Srishti Sethi committed
245
                         )
246 247 248
          self.cell.append(Item)

          gcompris.utils.item_focus_init(Item,None)
249 250 251 252 253 254 255 256 257 258 259 260
          if(SYMBOL[index] == '+'):
              COLOR_LIST = PLUS_COLOR_ON
              self.braille_math_symbol(index,COLOR_LIST)
          elif(SYMBOL[index] == '_'):
              COLOR_LIST = MINUS_COLOR_ON
              self.braille_math_symbol(index,COLOR_LIST)
          elif(SYMBOL[index] == '*'):
              COLOR_LIST = MULT_COLOR_ON
              self.braille_math_symbol(index,COLOR_LIST)
          elif(SYMBOL[index] == '/'):
              COLOR_LIST = DIV_COLOR_ON
              self.braille_math_symbol(index,COLOR_LIST)
261 262
      for index in range(4):
          self.cell[index].connect("button_press_event",self.symbol_identify,index)
Srishti Sethi's avatar
Srishti Sethi committed
263

264 265 266 267 268 269 270
  def braille_math_symbol(self, index, COLOR_LIST):
      BrailleChar(self.rootitem,(index*98)+255,375,50,
              SYMBOL[index],COLOR_LIST,COLOR_OFF,SYMBOL_OFF,CIRCLE_FILL,
              False,False,False,None)

  def symbol_identify(self, event, target, item, index):
      if((SYMBOL[index] == '+' and self.random_sign == '+') or
Srishti Sethi's avatar
Srishti Sethi committed
271
         (SYMBOL[index] == '_' and self.random_sign == '-') or
272 273
          (SYMBOL[index] == '*' and self.random_sign == '*')
        or (SYMBOL[index] == '/' and self.random_sign == '/')):
274
          self.gamewon = 1
Srishti Sethi's avatar
Srishti Sethi committed
275 276
          gcompris.bonus.display(gcompris.bonus.WIN,gcompris.bonus.FLOWER)
      else :
Srishti Sethi's avatar
Srishti Sethi committed
277
          self.gamewon = 0
Srishti Sethi's avatar
Srishti Sethi committed
278 279
          gcompris.bonus.display(gcompris.bonus.LOOSE,gcompris.bonus.FLOWER)

280
  def ok_event(self, item, target, event, level):
Srishti Sethi's avatar
Srishti Sethi committed
281 282
        if(self.result == self.correct_letter):
              goocanvas.Text(parent = self.rootitem,
Srishti Sethi's avatar
Srishti Sethi committed
283 284 285 286 287
                     x=650.0,
                     y=330.0,
                     text=self.result,
                     anchor = gtk.ANCHOR_CENTER,
                     font='SANS 25')
Srishti Sethi's avatar
Srishti Sethi committed
288 289 290 291 292
              self.gamewon = 1
              gcompris.bonus.display(gcompris.bonus.WIN,gcompris.bonus.FLOWER)
        else :
              self.gamewon = 0
              gcompris.bonus.display(gcompris.bonus.LOOSE,gcompris.bonus.FLOWER)
Srishti Sethi's avatar
Srishti Sethi committed
293

Srishti Sethi's avatar
Srishti Sethi committed
294 295
  def letter_change(self, letter):
      self.correct_letter = letter
Srishti Sethi's avatar
Srishti Sethi committed
296 297 298 299 300 301 302 303 304 305 306 307


  def end(self):
    # Remove the root item removes all the others inside it
    self.rootitem.remove()


  def ok(self):
    print("dicey_dicey ok.")


  def repeat(self):
308 309 310 311 312 313 314 315 316
      if(self.mapActive):
          self.end()
          self.start()
          self.mapActive = False
      else :
          self.rootitem.props.visibility = goocanvas.ITEM_INVISIBLE
          self.rootitem = goocanvas.Group(parent=
                                   self.gcomprisBoard.canvas.get_root_item())
          gcompris.set_default_background(self.gcomprisBoard.canvas.get_root_item())
317
          map_obj = BrailleMap(self.rootitem, COLOR_ON, COLOR_OFF, CIRCLE_FILL, CIRCLE_STROKE)
318 319
          self.mapActive = True

Srishti Sethi's avatar
Srishti Sethi committed
320 321 322 323 324 325 326 327 328
  def config(self):
    print("dicey_dicey config.")


  def key_press(self, keyval, commit_str, preedit_str):
    utf8char = gtk.gdk.keyval_to_unicode(keyval)
    strn = u'%c' % utf8char

  def pause(self, pause):
329
      self.board_paused = pause
330
      if(pause == 0) and (self.gamewon == 1):
331 332
          self.gamewon = 0
          self.counter +=1
Srishti Sethi's avatar
Srishti Sethi committed
333 334 335 336
          if ((self.gcomprisBoard.level == 1 or self.gcomprisBoard.level == 2 or
               self.gcomprisBoard.level == 3) and self.counter == 3):
              self.increment_level()
          elif(self.counter == 4):
337 338 339
              self.increment_level()
          self.end()
          self.start()
Srishti Sethi's avatar
Srishti Sethi committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

  def set_level(self, level):
    gcompris.sound.play_ogg("sounds/receive.wav")
    self.gcomprisBoard.level = level
    self.gcomprisBoard.sublevel = 1
    gcompris.bar_set_level(self.gcomprisBoard)
    self.end()
    self.start()

  def increment_level(self):
    self.counter = 0
    gcompris.sound.play_ogg("sounds/bleep.wav")
    self.gcomprisBoard.sublevel += 1
    if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
        self.gcomprisBoard.sublevel=1
        self.gcomprisBoard.level += 1
        if(self.gcomprisBoard.level > self.gcomprisBoard.maxlevel):
SrishAkaTux's avatar
SrishAkaTux committed
357
            self.gcomprisBoard.level = 1