braille_lotto.py 17.6 KB
Newer Older
1 2
#  gcompris - braille_lotto.py
#
3
# Copyright (C) 2011 Bruno Coudoin | Srishti Sethi
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
#   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/>.
#
# braille_lotto activity.
19 20
import gtk
import gtk.gdk
21
import gcompris
22
import gobject
23
import gcompris.utils
24
import gcompris.skin
25
import gcompris.bonus
26
import gcompris.timer
27
import gcompris.anim
28
import goocanvas
29
import random
30
import pango
31

32
from BrailleChar import *
33
from BrailleMap import *
34 35
from gcompris import gcompris_gettext as _

36 37
COLOR_ON = 0X00FFFF
COLOR_OFF = 0X000000
38
CIRCLE_FILL = "light green"
39
CIRCLE_STROKE = "black"
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
CELL_WIDTH = 30

class Gcompris_braille_lotto:
  """Empty gcompris python class"""

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

    # Needed to get key_press
    gcomprisBoard.disable_im_context = True

  def start(self):
    # Set a background image
    gcompris.set_default_background(self.gcomprisBoard.canvas.get_root_item())

    #Boolean variable declaration
    self.mapActive = False
59 60

    #CONSTANT Declarations
61
    self.board_paused = 0
62 63 64 65 66
    self.timerAnim = 0
    self.counter = 0
    self.gamewon = 0
    self.score_player_a = 0
    self.score_player_b = 0
67 68
    self.tile_counter = 0
    self.rectangle_counter = 0
69 70

    #REPEAT ICON
71 72
    pixmap = gcompris.utils.load_svg("braille_alphabets/target.svg")
    gcompris.bar_set_repeat_icon(pixmap)
73 74 75 76 77 78
    gcompris.bar_set(gcompris.BAR_REPEAT_ICON)
    gcompris.bar_location(320,-1,0.8)

    # 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.
79
    self.root = goocanvas.Group(parent =
80 81 82 83 84
                                    self.gcomprisBoard.canvas.get_root_item())

    self.lotto_board()

  def lotto_board(self):
85 86 87 88
    #Display Rectangle Ticket Boxes
    self.rect = []
    self.rect_x = []
    self.rect_y = []
89 90
    self.displayTicketBox(40 , 40)
    self.displayTicketBox(420, 40)
91 92 93 94 95 96

    #Rectangle box with ticket number is made clickable
    index = 0
    even = 0
    while (index < 12):
        if(even % 2 == 0):
97
            gcompris.utils.item_focus_init(self.rect[even],None)
98 99
            self.rect[even].connect("button_press_event",
                                    self.cross_number, index)
100 101
        even += 2
        index += 1
102 103

    #Displaying player_one and player_two
104 105 106
    #PLAYER 1
    goocanvas.Text(
                parent = self.root,
107 108
                x=200.0,
                y=300.0,
109
                text=_("PLAYER 1"),
110 111 112 113
                fill_color="black",
                anchor = gtk.ANCHOR_CENTER,
                alignment = pango.ALIGN_CENTER,
                )
114 115 116
    #PLAYER TWO
    goocanvas.Text(
                parent = self.root,
117 118
                x=580.0,
                y=300.0,
119
                text=_("PLAYER 2"),
120 121 122 123 124 125
                fill_color="black",
                anchor = gtk.ANCHOR_CENTER,
                alignment = pango.ALIGN_CENTER,
                )

    #Button to display the number to be checked in the ticket
126 127 128 129 130 131 132 133 134 135
    goocanvas.Rect(parent = self.root,
                   x = 25,
                   y = 350,
                   width = 170,
                   height = 90,
                   radius_x = 17,
                   radius_y = 17,
                   stroke_color = "black",
                   fill_color = "#d38d5f" ,
                   line_width = 2)
136 137 138

    #Check number
    goocanvas.Text(
139 140
      parent = self.root,
      text= _("Check Number"),
141
      font = gcompris.skin.get_font("gcompris/board/medium"),
142 143 144
      x = 110,
      y = 380,
      anchor = gtk.ANCHOR_CENTER,
145 146 147
      )


148 149 150 151 152 153 154
    #Buttons for Clue
    svghandle = gcompris.utils.load_svg("braille_lotto/button1.svg")
    #LEFT Button
    self.hint_left_button = goocanvas.Svg(
                                     parent = self.root,
                                     svg_handle = svghandle,
                                     svg_id = "#FIG1",
155
                                     tooltip = _("Click me to get some hint")
156
                                     )
157
    self.hint_left_button.translate(210, 330)
158
    self.hint_left_button.connect("button_press_event", self.clue_left)
159 160
    gcompris.utils.item_focus_init(self.hint_left_button, None)

161 162 163 164 165 166

    #RIGHT Button
    self.hint_right_button = goocanvas.Svg(
                                     parent = self.root,
                                     svg_handle = svghandle,
                                     svg_id = "#FIG2",
167
                                     tooltip = _("Click me to get some hint")
168
                                     )
169
    self.hint_right_button.translate(290, 330)
170
    self.hint_right_button.connect("button_press_event", self.clue_right)
171 172
    gcompris.utils.item_focus_init(self.hint_right_button, None)

173 174

    #Displaying text on clue buttons
175
    self.text_array = []
176
    for index in range(2):
177
        #Translators : Do not translate the token {number}
178
        clue_text = goocanvas.Text(
179
                    parent = self.root,
180
                    text = _("I don't have this number PLAYER {number}").format(number = str(index + 1)),
181
                    font = gcompris.skin.get_font("gcompris/board/medium"),
182
                    x = index * 230 + 310,
183
                    y = 395,
184
                    width = 150,
185 186
                    anchor=gtk.ANCHOR_CENTER,
                    )
187 188 189 190 191 192
        self.text_array.append(clue_text)
    gcompris.utils.item_focus_init(self.text_array[0], self.hint_left_button)
    self.text_array[0].connect("button_press_event", self.clue_left)
    gcompris.utils.item_focus_init(self.text_array[1], self.hint_right_button)
    self.text_array[1].connect("button_press_event", self.clue_right)

193 194 195 196 197

    #Displaying Tux Lotto Master
    goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/tux.svg"),
                    x = 360,
198
                    y = 330,
199 200 201 202 203
                    )
    goocanvas.Text(
                    parent = self.root,
                    text = _("Lotto Master"),
                    font = gcompris.skin.get_font("gcompris/board/medium"),
204 205
                    x = 410,
                    y = 455,
206 207 208
                    anchor=gtk.ANCHOR_CENTER,
                    )

209
    #Generate Number Button
210 211 212 213 214 215 216 217 218 219 220
    generate_number = goocanvas.Rect(parent = self.root,
                   x = 610,
                   y = 350,
                   width = 170,
                   height = 90,
                   radius_x = 17,
                   radius_y = 17,
                   stroke_color = "black",
                   fill_color = "#d33e5f",
                   line_width = 2)

221 222 223
    generate_number.connect("button_press_event", self.generateNumber)
    gcompris.utils.item_focus_init(generate_number, None)

224
    generate_text = goocanvas.Text(
225
                    parent = self.root,
226
                    text = _("Generate a number"),
227
                    font = gcompris.skin.get_font("gcompris/board/medium"),
228
                    x = 695,
229
                    y = 390,
230
                    width = 50,
231 232
                    anchor = gtk.ANCHOR_CENTER,
                    alignment = pango.ALIGN_CENTER,
233
                    )
234 235
    generate_text.connect("button_press_event", self.generateNumber)
    gcompris.utils.item_focus_init(generate_text, generate_number)
236

237 238 239 240 241
    #An array to store the ticket numbers
    self.ticket_array = []

    #Displaying the Braille Code for TICKETS A & B
    #TICKET A
242 243 244 245 246 247
    self.displayTicket(1, 25, 60, 50)
    self.displayTicket(1, 25, 60, 200)
    self.displayTicket(26, 50, 140, 125)
    self.displayTicket(51, 75, 230, 50)
    self.displayTicket(51, 75, 230, 200)
    self.displayTicket(76, 90, 320, 125)
248 249

    #TICKET B
250 251 252 253 254 255
    self.displayTicket(1, 25, 440, 50)
    self.displayTicket(1, 25, 440, 200)
    self.displayTicket(26, 50, 520, 125)
    self.displayTicket(51, 75, 610, 50)
    self.displayTicket(51, 75, 610, 200)
    self.displayTicket(76, 90, 700, 125)
256

257 258 259 260 261 262 263
    #Copy the contents of ticket array into another for shuffling
    self.check_random = self.ticket_array[:]
    random.shuffle(self.check_random)

    #Calling the random number and checking it on lotto board
    self.number_call()

264 265 266
  def clue_left(self, event , target, item):
      self.callout1 = goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/callout1.svg"),
267
                    x = 230,
268
                    y =250,
269 270 271 272
                    )
      self.status_one = goocanvas.Text(
                            parent = self.root,
                            text= "",
273
                            x=315,
274
                            y=310,
275
                            width = 130,
276 277 278 279
                            font = "SANS 10 BOLD",
                            anchor=gtk.ANCHOR_CENTER,
                            )

280
      if (self.check_random[self.counter] in self.ticket_array[0:6]):
281
          self.findColumn()
282
          #Translators : Do not translate the token {column}
283 284
          self.status_one.props.text = \
              _("Hey, you have it. Its there in your {column} column").format(column = self.column)
285
      else :
286
          self.status_one.props.text = _("Oops,number isn't there in your ticket!")
287
      self.timerAnim = gobject.timeout_add(1500, self.hideCalloutLeft)
288 289 290 291

  def clue_right(self, event , target, item):
      self.callout2 = goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/callout2.svg"),
292
                    x = 410,
293
                    y = 250,
294 295 296 297
                    )
      self.status_two = goocanvas.Text(
                            parent = self.root,
                            text= "",
298
                            x=510,
299
                            y=310,
300
                            width = 130,
301 302 303
                            font = "SANS 10 BOLD",
                            anchor=gtk.ANCHOR_CENTER,
                            )
304
      if (self.check_random[self.counter] in self.ticket_array[6:12]):
305
          self.findColumn()
306
          #Translators : Do not translate the token {column}
307 308
          self.status_two.props.text = \
              _("Hey, you have it. Its there in your {column} column").format(column = self.column)
309
      else :
310
          self.status_two.props.text = _("Oops, number isn't there in your ticket!")
311
      self.timerAnim = gobject.timeout_add(1500, self.hideCalloutRight)
312 313 314


  def hideCalloutLeft(self):
315 316
    self.callout1.props.visibility = goocanvas.ITEM_INVISIBLE
    self.status_one.props.text = ""
317 318

  def hideCalloutRight(self):
319 320
    self.callout2.props.visibility = goocanvas.ITEM_INVISIBLE
    self.status_two.props.text = ""
321 322

  def findColumn(self):
323
      if self.check_random[self.counter] <= 25:
324
          self.column = "1st"
325 326
      elif self.check_random[self.counter] <= 50 \
            and self.check_random[self.counter] > 25 :
327
          self.column = "2nd"
328 329
      elif self.check_random[self.counter] <= 75 \
            and self.check_random[self.counter] > 50 :
330 331 332 333
          self.column = "3rd"
      else :
          self.column = "4th"

334

335 336 337 338
  def generateNumber(self, item, event, target):
        self.check_number.set_property("text","")
        self.counter += 1
        self.number_call()
339 340

  def number_call(self):
341
      if(self.counter == 11):
342 343
          self.displayGameStatus( _("Game Over") )
          self.timer_inc  = gobject.timeout_add(1000, self.game_over)
344
      elif (self.counter < 11):
345 346
        self.check_number = \
            goocanvas.Text(parent = self.root,
347
                           text = self.check_random[self.counter],
348 349 350 351 352
                           x=110,
                           y=420,
                           font = gcompris.skin.get_font("gcompris/board/title bold"),
                           anchor=gtk.ANCHOR_CENTER,
                           )
353

354 355
  def game_over(self):
          self.pause(1)
356

357 358 359 360 361
  def displayTicketBox(self, x, y):
      goocanvas.Rect(
      parent = self.root,
      x = x + 5,
      y = y + 5,
362 363
      width = 350,
      height = 230,
364 365 366
      stroke_color = "dark green",
      fill_color = "light green" ,
      line_width=7)
367

368 369 370 371
      for i in range(4):
        for j in range(3):
              box = goocanvas.Rect(
                             parent = self.root,
372 373 374 375
                             x = x + 7 + 88 * i,
                             y = y + 7 + 77 * j,
                             width = 82,
                             height = 73,
376 377 378 379
                             stroke_color = "dark green",
                             fill_color = "light green" ,
                             line_width=2)
              self.rect.append(box)
380 381
              self.rect_x.append(x + 7 + 88 * i)
              self.rect_y.append(y + 7 + 77 * j)
382 383 384 385 386

  def displayTicket(self, a, b, x, y):
      ticket = random.randint(a, b)
      self.ticket_array.append(ticket)
      if (ticket < 10):
387
          obj = BrailleChar(self.root, x, y, 50 , ticket, COLOR_ON, COLOR_OFF ,
388
                  CIRCLE_FILL, CIRCLE_FILL, False, False ,False, None)
389 390
          obj.ticket_focus(self.rect[self.rectangle_counter],
                           self.cross_number, self.tile_counter)
391 392 393
      else :
          tens_digit = ticket / 10
          ones_digit = ticket % 10
394
          obj1 = BrailleChar(self.root, x - 7, y, 50 ,tens_digit, COLOR_ON, COLOR_OFF ,
395
                  CIRCLE_FILL, CIRCLE_FILL, False, False ,False, None)
396 397
          obj1.ticket_focus(self.rect[self.rectangle_counter],
                            self.cross_number, self.tile_counter)
398 399

          obj2 = BrailleChar(self.root, x + 25, y, 50 , ones_digit, COLOR_ON, COLOR_OFF ,
400
                  CIRCLE_FILL, CIRCLE_FILL, False, False ,False, None)
401 402
          obj2.ticket_focus(self.rect[self.rectangle_counter],
                            self.cross_number, self.tile_counter)
403

404 405
      self.rectangle_counter += 2
      self.tile_counter += 1
406

407
  def cross_number(self, item, event, target, index):
408
    if( self.check_random[self.counter] == self.ticket_array[index]):
409
        # This is a win
410 411 412 413 414
        if(index in (0, 1, 2, 3, 4, 5)):
            self.score_player_a +=1
        else:
            self.score_player_b +=1

415 416 417 418 419 420 421
        #Checked_button
        goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/button_checked.png"),
                    x = self.rect_x[index * 2] + 8,
                    y = self.rect_y[index * 2] + 5,
                    )
    else :
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
      # This is a loss, indicate it with a cross mark
      item = \
          goocanvas.Image(parent = self.root,
                          pixbuf = gcompris.utils.load_pixmap("braille_lotto/cross_button.png"),
                          x = self.rect_x[index * 2] + 8,
                          y = self.rect_y[index * 2] + 5,
                          )
      gobject.timeout_add( 1000, lambda: item.remove() )

    winner = 0
    if(self.score_player_a == 6):
      winner = 1
    elif(self.score_player_b == 6):
      winner = 2

    if winner:
      self.displayGameStatus( \
        _("Congratulation player {player_id},  you won").format(player_id = str(winner) ) )
      self.timer_inc  = gobject.timeout_add(1500, self.timer_loop)


  def displayGameStatus(self, message):
444
      self.game = goocanvas.Image(parent = self.root,
445
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/game.svg"),
446 447
                    x = 200 ,
                    y = 100,
448
                    )
449
      self.game_status = goocanvas.Text(
450
                    parent = self.root,
451 452 453 454 455 456 457 458 459
                    text = message,
                    x = 365,
                    y = 200,
                    width = 100,
                    font = gcompris.skin.get_font("gcompris/board/title bold"),
                    fill_color = "black",
                    anchor = gtk.ANCHOR_CENTER,
                    alignment = pango.ALIGN_CENTER,
                    )
Srishti Sethi's avatar
Srishti Sethi committed
460

461
  def timer_loop(self):
462 463 464 465 466
    # Hide the game status
    self.game.props.visibility = goocanvas.ITEM_INVISIBLE
    self.game_status.props.visibility = goocanvas.ITEM_INVISIBLE
    self.gamewon = 1
    gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.FLOWER)
467

468
  def end(self):
469 470 471
    # Remove the root item removes all the others inside it
    self.root.remove()
    gcompris.end_board()
472 473 474 475 476 477

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

  def repeat(self):
      if(self.mapActive):
478 479
          self.rootitem.props.visibility = goocanvas.ITEM_INVISIBLE
          self.root.props.visibility = goocanvas.ITEM_VISIBLE
480 481 482
          self.mapActive = False
          self.pause(0)
      else :
483
          self.root.props.visibility = goocanvas.ITEM_INVISIBLE
484 485 486
          self.rootitem = goocanvas.Group(parent=
                                   self.gcomprisBoard.canvas.get_root_item())
          gcompris.set_default_background(self.gcomprisBoard.canvas.get_root_item())
487
          map_obj = BrailleMap(self.rootitem, COLOR_ON, COLOR_OFF, CIRCLE_FILL, CIRCLE_STROKE)
488 489 490 491
          self.mapActive = True
          self.pause(1)

  def config(self):
492
    pass
493 494

  def key_press(self, keyval, commit_str, preedit_str):
495
    pass
496 497 498

  def pause(self, pause):
      self.board_paused = pause
499
      if(self.board_paused and (self.counter == 11 or self.gamewon == 1)):
500 501
          self.end()
          self.start()
502 503

  def set_level(self, level):
504
    pass