braille_lotto.py 17.9 KB
Newer Older
1 2
#  gcompris - braille_lotto.py
#
3
# Copyright (C) 2003, 2008 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 36 37 38
from gcompris import gcompris_gettext as _

COLOR_ON = 0X00FFFFL
COLOR_OFF = 0X00000000L
CIRCLE_FILL = "light green"
39
CIRCLE_STROKE = "black"
40 41
CELL_WIDTH = 30

42 43
CHECK_RANDOM = []

44 45 46 47 48 49 50 51 52 53 54 55 56
class Gcompris_braille_lotto:
  """Empty gcompris python class"""

  def __init__(self, gcomprisBoard):
    print "braille_lotto init"

    # 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

57 58 59 60 61
    for index in range(1, 91):
        CHECK_RANDOM.append(index)

    random.shuffle(CHECK_RANDOM)

62 63 64 65 66 67 68 69 70 71 72 73 74 75
  def start(self):
    print "braille_lotto start"

    # Set the buttons we want in the bar
    gcompris.bar_set(gcompris.BAR_LEVEL)
    gcompris.bar_set(0)
    gcompris.bar_location(400, -1, 0.8)

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

    #Boolean variable declaration
    self.mapActive = False
    self.board_paused = 0
76 77 78 79 80 81 82
    self.timerAnim = 0
    self.counter = 0
    self.gamewon = 0
    self.countAnim = 200
    self.score_player_a = 0
    self.score_player_b = 0
    self.status_timer = 50
83 84
    self.delay_one = 100
    self.delay_two = 100
85

86 87 88 89 90 91 92 93

    #REPEAT ICON
    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.
94
    self.root = goocanvas.Group(parent =
95 96 97 98 99 100 101
                                    self.gcomprisBoard.canvas.get_root_item())

    self.lotto_board()

  def lotto_board(self):
    #Lotto Board
    board = goocanvas.Rect(
102
      parent = self.root,
103 104 105 106 107 108 109 110 111 112 113
      x = 40,
      y = 20,
      width = 420,
      height = 270,
      stroke_color = "blue",
      fill_color = "light blue" ,
      line_width=5)

    #Horizontal polylines for lotto board
    for index in range(8):
        p_points = goocanvas.Points([(40.0, 30 * index + 50),(460.0, 30 * index + 50)])
114
        polyline = goocanvas.Polyline(parent = self.root, points=p_points, stroke_color="blue")
115 116 117 118

    #Vertical polylines for lotto board
    for index in range(9):
        p_points = goocanvas.Points([(42 * index + 80, 20.0),(42 * index + 80, 290.0)])
119 120 121 122 123 124 125 126 127 128 129 130 131 132
        polyline = goocanvas.Polyline(parent = self.root, points=p_points, stroke_color="blue")

    #Display Rectangle Ticket Boxes
    self.rect = []
    self.rect_x = []
    self.rect_y = []
    self.displayTicketBox(40 , 310)
    self.displayTicketBox(500, 310)

    #Rectangle box with ticket number is made clickable
    index = 0
    even = 0
    while (index < 12):
        if(even % 2 == 0):
133
            gcompris.utils.item_focus_init(self.rect[even],self.rect[even])
134 135 136
            self.rect[even].connect("button_press_event",self.cross_number, index)
        even += 2
        index += 1
137 138

    #Displaying player_one and player_two
139 140 141 142 143 144
    #PLAYER 1
    goocanvas.Text(
                parent = self.root,
                x=170.0,
                y=500.0,
                text=_("PLAYER 1"),
145 146 147 148
                fill_color="black",
                anchor = gtk.ANCHOR_CENTER,
                alignment = pango.ALIGN_CENTER,
                )
149 150 151 152 153 154
    #PLAYER TWO
    goocanvas.Text(
                parent = self.root,
                x=640.0,
                y=500.0,
                text=_("PLAYER 2"),
155 156 157 158 159 160
                fill_color="black",
                anchor = gtk.ANCHOR_CENTER,
                alignment = pango.ALIGN_CENTER,
                )

    #Button to display the number to be checked in the ticket
161
    goocanvas.Image(parent = self.root,
162
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/button.jpg"),
163
                    x = 470,
164
                    y = 35,
165 166 167 168
                    )

    #Check number
    goocanvas.Text(
169 170
      parent = self.root,
      text= _("Check Number"),
171
      font = gcompris.skin.get_font("gcompris/board/medium"),
172
      x=560,
173
      y=55,
174 175 176 177 178 179 180 181 182 183 184 185 186 187
      anchor=gtk.ANCHOR_CENTER,
      )

    #Displaying Numbers in Lotto Board
    k = 0
    i = 0
    j = 10
    self.board_array = []
    self.board_array_x = []
    self.board_array_y = []
    while k < 9:
        spacing = 0
        for index in range(i,j):
            goocanvas.Text(
188
                    parent = self.root,
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                    text = (index + 1),
                    font = gcompris.skin.get_font("gcompris/board/medium"),
                    x = spacing * 43 + 55,
                    y = 30 * k + 35,
                    anchor=gtk.ANCHOR_CENTER,
                    )
            #Append number, x and y location to arrays
            self.board_array.append(index + 1)
            self.board_array_x.append(spacing * 43 + 55)
            self.board_array_y.append(30 * k + 35)
            spacing = spacing + 1
        i += 10
        j += 10
        k += 1

204 205 206 207 208 209 210 211 212 213 214
    #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",
                                     tooltip = "Click me to get some hint"
                                     )
    self.hint_left_button.translate(470, 150)
    self.hint_left_button.connect("button_press_event", self.clue_left)
215
    gcompris.utils.item_focus_init(self.hint_left_button, self.hint_left_button)
216 217 218 219 220 221 222 223 224 225

    #RIGHT Button
    self.hint_right_button = goocanvas.Svg(
                                     parent = self.root,
                                     svg_handle = svghandle,
                                     svg_id = "#FIG2",
                                     tooltip = "Click me to get some hint"
                                     )
    self.hint_right_button.translate(470, 150)
    self.hint_right_button.connect("button_press_event", self.clue_right)
226
    gcompris.utils.item_focus_init(self.hint_right_button, self.hint_right_button)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    #Displaying text on clue buttons
    for index in range(2):
        goocanvas.Text(
                    parent = self.root,
                    text = _("I don't have \n""this number \n\n" "PLAYER " + str(index + 1)),
                    font = gcompris.skin.get_font("gcompris/board/medium"),
                    x = index * 160 + 550,
                    y = 215,
                    anchor=gtk.ANCHOR_CENTER,
                    )

    #Displaying Tux Lotto Master
    goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/tux.svg"),
                    x = 360,
                    y = 320,
                    )
    goocanvas.Text(
                    parent = self.root,
                    text = _("Lotto Master"),
                    font = gcompris.skin.get_font("gcompris/board/medium"),
                    x = 400,
                    y = 430,
                    anchor=gtk.ANCHOR_CENTER,
                    )

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    #Generate Number Button
    generate_number = goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/generate_number.png"),
                    x = 670,
                    y = 30,
                    )
    generate_number.connect("button_press_event", self.generateNumber)
    gcompris.utils.item_focus_init(generate_number, None)

    goocanvas.Text(
                    parent = self.root,
                    text = _("Generate")+"\n"+_("Number"),
                    font = gcompris.skin.get_font("gcompris/board/medium"),
                    x = 730,
                    y = 90,
                    anchor=gtk.ANCHOR_CENTER,
                    )

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

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

    #An array to store the ticket numbers
    self.ticket_array = []

    #Displaying the Braille Code for TICKETS A & B
    #TICKET A
    self.displayTicket(1, 25, 55, 320)
    self.displayTicket(1, 25, 55, 432)
    self.displayTicket(26, 50, 122, 377)
    self.displayTicket(51, 75, 191, 320)
    self.displayTicket(51, 75, 191, 432)
    self.displayTicket(76, 90, 259, 377)

    #TICKET B
    self.displayTicket(1, 25, 515, 320)
    self.displayTicket(1, 25, 515, 432)
    self.displayTicket(26, 50, 582, 377)
    self.displayTicket(51, 75, 650, 320)
    self.displayTicket(51, 75, 650, 432)
    self.displayTicket(76, 90, 718, 377)

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  def clue_left(self, event , target, item):
      self.callout1 = goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/callout1.svg"),
                    x = 220,
                    y = 340,
                    )
      self.status_one = goocanvas.Text(
                            parent = self.root,
                            text= "",
                            x=310,
                            y=410,
                            font = "SANS 10 BOLD",
                            anchor=gtk.ANCHOR_CENTER,
                            )

      if (CHECK_RANDOM[self.counter] in self.ticket_array[0:6]):
          self.findColumn()
          self.status_one.props.text = " Hey,you have \n "" it. Its there \n"" in your\n " + self.column + " column"
      else :
          self.status_one.props.text = " Oops, number\n"" isn't there\n" " in your ticket!"
      self.timerAnim = gobject.timeout_add(200, self.hideCalloutLeft)

  def clue_right(self, event , target, item):
      self.callout2 = goocanvas.Image(parent = self.root,
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/callout2.svg"),
                    x = 400,
                    y = 340,
                    )
      self.status_two = goocanvas.Text(
                            parent = self.root,
                            text= "",
                            x=500,
                            y=410,
                            font = "SANS 10 BOLD",
                            anchor=gtk.ANCHOR_CENTER,
                            )
      if (CHECK_RANDOM[self.counter] in self.ticket_array[6:12]):
          self.findColumn()
          self.status_two.props.text = " Hey,you have \n "" it. Its there \n"" in your\n " + self.column + " column"
      else :
          self.status_two.props.text = " Oops, number\n"" isn't there\n" " in your ticket!"
      self.timerAnim = gobject.timeout_add(100, self.hideCalloutRight)


  def hideCalloutLeft(self):
      self.delay_one -= 1
      if(self.delay_one == 0):
          self.callout1.props.visibility = goocanvas.ITEM_INVISIBLE
          self.status_one.props.text = ""
          self.delay_one = 100
      if self.delay_one < 100 :
          self.timer_inc  = gobject.timeout_add(self.delay_one,
                                            self.hideCalloutLeft)

  def hideCalloutRight(self):
      self.delay_two -= 1
      if(self.delay_two == 0):
          self.callout2.props.visibility = goocanvas.ITEM_INVISIBLE
          self.status_two.props.text = ""
          self.delay_two = 100
      if self.delay_two < 100 :
          self.timer_inc  = gobject.timeout_add(self.delay_two,
                                            self.hideCalloutRight)

  def findColumn(self):
      if CHECK_RANDOM[self.counter] <= 25:
          self.column = "1st"
      elif CHECK_RANDOM[self.counter] <= 50 and CHECK_RANDOM[self.counter] > 25 :
          self.column = "2nd"
      elif CHECK_RANDOM[self.counter] <= 75 and CHECK_RANDOM[self.counter] > 50 :
          self.column = "3rd"
      else :
          self.column = "4th"

370

371 372 373 374
  def generateNumber(self, item, event, target):
        self.check_number.set_property("text","")
        self.counter += 1
        self.number_call()
375 376 377

  def number_call(self):
      if(self.counter == 90):
378 379 380
          self.game.props.visibility = goocanvas.ITEM_VISIBLE
          self.game_status.props.text = " Game Over"

381 382 383
      self.check_number = goocanvas.Text(
                            parent = self.root,
                            text= CHECK_RANDOM[self.counter],
384
                            x=560,
385 386 387 388 389 390
                            y=100,
                            font = "SANS 20",
                            anchor=gtk.ANCHOR_CENTER,
                            )
      #Place the checked button on number which is being called
      goocanvas.Image(parent = self.root,
391
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/button_checked.png"),
392 393
                    x = self.board_array_x[CHECK_RANDOM[self.counter] - 1] - 10 ,
                    y = self.board_array_y[CHECK_RANDOM[self.counter] - 1]- 10,
394 395 396
                    )


397 398 399 400 401 402 403 404 405 406
  def displayTicketBox(self, x, y):
      goocanvas.Rect(
      parent = self.root,
      x = x + 5,
      y = y + 5,
      width = 268,
      height = 168,
      stroke_color = "dark green",
      fill_color = "light green" ,
      line_width=7)
407

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
      for i in range(4):
        for j in range(3):
              box = goocanvas.Rect(
                             parent = self.root,
                             x = x + 5 + 68 * i,
                             y = y + 5 + 57 * j,
                             width = 65,
                             height = 54,
                             stroke_color = "dark green",
                             fill_color = "light green" ,
                             line_width=2)
              self.rect.append(box)
              self.rect_x.append(x + 5 + 68 * i)
              self.rect_y.append(y + 5 + 57 * j)

  def displayTicket(self, a, b, x, y):
      ticket = random.randint(a, b)
      self.ticket_array.append(ticket)
      if (ticket < 10):
          BrailleChar(self.root, x, y, 35 , ticket, COLOR_ON, COLOR_OFF ,
428
                  CIRCLE_FILL, CIRCLE_FILL, False, False ,True, None)
429 430 431 432
      else :
          tens_digit = ticket / 10
          ones_digit = ticket % 10
          BrailleChar(self.root, x - 7, y, 33 ,tens_digit, COLOR_ON, COLOR_OFF ,
433
                  CIRCLE_FILL, CIRCLE_FILL, False, False ,True, None)
434
          BrailleChar(self.root, x + 20, y, 33 , ones_digit, COLOR_ON, COLOR_OFF ,
435 436
                  CIRCLE_FILL, CIRCLE_FILL, False, False ,True, None)

437
  def cross_number(self,item, event, target, index):
438
    #Cross Sign
439
    goocanvas.Image(parent = self.root,
440
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/cross_button.png"),
441 442
                    x = self.rect_x[index * 2]+8 ,
                    y = self.rect_y[index * 2]+5,
443 444
                    )

445 446 447 448 449 450 451
    if( CHECK_RANDOM[self.counter] == self.ticket_array[index]):
        if(index in (0, 1, 2, 3, 4, 5)):
            self.score_player_a +=1
        else:
            self.score_player_b +=1

    if(self.score_player_a == 6 or self.score_player_b == 6):
452
        self.game = goocanvas.Image(parent = self.root,
453 454 455 456 457 458
                    pixbuf = gcompris.utils.load_pixmap("braille_lotto/game.svg"),
                    x = 230 ,
                    y = 150,
                    )

        self.game_status = goocanvas.Text(
459
                    parent = self.root,
460 461 462 463 464
                    text= "",
                    x=390,
                    y=220,
                    font = "SANS 30",
                    fill_color = "blue",
465 466
                    anchor=gtk.ANCHOR_CENTER,
                   )
467 468 469 470 471
        if(self.score_player_a == 6):
            self.game_status.props.text = "PLAYER 1\n" "You WON"
        elif(self.score_player_b == 6):
            self.game_status.props.text = "PLAYER 2 \n" "You WON"

472 473 474 475 476 477 478 479 480 481
        self.timer_inc  = gobject.timeout_add(self.status_timer,
                                            self.timer_loop)
  def timer_loop(self):
      self.status_timer -= 1
      if(self.status_timer == 0):
          self.gamewon = 1
          gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.FLOWER)
      self.timer_inc  = gobject.timeout_add(self.status_timer,
                                            self.timer_loop)

482 483 484
  def end(self):
    print "braille_lotto end"

485 486 487
    # Remove the root item removes all the others inside it
    self.root.remove()
    gcompris.end_board()
488 489 490 491 492 493

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

  def repeat(self):
      if(self.mapActive):
494 495
          self.rootitem.props.visibility = goocanvas.ITEM_INVISIBLE
          self.root.props.visibility = goocanvas.ITEM_VISIBLE
496 497 498
          self.mapActive = False
          self.pause(0)
      else :
499
          self.root.props.visibility = goocanvas.ITEM_INVISIBLE
500 501 502
          self.rootitem = goocanvas.Group(parent=
                                   self.gcomprisBoard.canvas.get_root_item())
          gcompris.set_default_background(self.gcomprisBoard.canvas.get_root_item())
503
          map_obj = BrailleMap(self.rootitem, COLOR_ON, COLOR_OFF, CIRCLE_FILL, CIRCLE_STROKE)
504 505 506 507 508 509 510 511 512 513 514 515
          self.mapActive = True
          self.pause(1)

  def config(self):
    print("braille_lotto 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):
      self.board_paused = pause
516 517 518
      if(self.board_paused == 1 and (self.counter == 90 or self.gamewon == 1)):
          self.end()
          self.start()
519 520 521

  def set_level(self, level):
    print("braille_lotto set level. %i" % level)