Esempio n. 1
0
class ExchangePuzzle(Activity):

    Grid1 = None

    PressedCell = None
    numRows = None
    nomCols = None

    def Load(self, display_surf):
        self.setBgColor(display_surf)
        '''Loading xml values'''
        xmlGrid1 = self.xmlActivity.getElementsByTagName('cells')[0]
        self.Grid1 = Grid(xmlGrid1)
        '''Loading constants for the activity'''

        xActual = Constants.MARGIN_TOP
        yActual = Constants.MARGIN_LEFT

        if self.Grid1.imagePath != None:
            '''Loading constants for the activity'''
            width = Constants.ACTIVITY_WIDTH
            height = Constants.ACTIVITY_HEIGHT
            ''' 1 Imagen de fondo '''
            self.Grid1.imagePath = self.mediaInformation[self.Grid1.imagePath]
            self.Grid1.LoadWithImage(self.Grid1.numRows, self.Grid1.numCols,
                                     width, height, xActual, yActual,
                                     display_surf, self.pathToMedia)

        else:
            ''' Calculate Real size'''
            height = self.Grid1.cellHeight * self.Grid1.numRows
            width = self.Grid1.cellWidth * self.Grid1.numCols
            '''Maximize size'''
            coef = self.calculateCoef(width, height)
            height = self.Grid1.cellHeight * self.Grid1.numRows * coef
            width = self.Grid1.cellWidth * self.Grid1.numCols * coef

            xGrid = (Constants.ACTIVITY_WIDTH - width) / 2
            yGrid = (Constants.ACTIVITY_HEIGHT - height) / 2
            xGrid = max(xGrid, xActual)
            yGrid = max(yGrid, yActual)
            ''' 1 Imagen por cada celda '''
            self.Grid1.Load(self.Grid1.numRows, self.Grid1.numCols, width,
                            height, xGrid, yGrid, display_surf)
            cells = xmlGrid1.getElementsByTagName('cell')
            self.styleCell = StyleCell(xmlGrid1)
            i = 0
            for cell in cells:
                '''Recuperamos el texto de la celda y lo  bliteamos en imagen actual de celda.. '''
                self.printxmlCellinCell(self.Grid1.Cells[i], cell,
                                        self.styleCell)
                i = i + 1
        self.Grid1.unsort()

    def OnEvent(self, PointOfMouse):
        '''
            -----------LOGICS OF THE GAME-----------
            self.PressedCell = celda anterior
            cell = celda actual
        '''
        for cell in self.Grid1.Cells:
            if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                '''celda anterior apretada '''
                if self.PressedCell != None:
                    '''celda anterior = celda actual '''
                    if self.PressedCell.contentCell.id == cell.idCell:
                        idCell1 = self.PressedCell.idCell
                        idCell2 = cell.idCell
                        self.Grid1.changeImages(idCell1, idCell2)
                    ''' else: no cambiamos -> no hacemos nada... '''
                    ''' borramos margenes.. '''
                    self.PressedCell.actualColorCell = Constants.colorCell
                    self.PressedCell = None

                else:
                    '''no hay ninguna anterior apretada'''
                    self.PressedCell = cell
                    self.PressedCell.actualColorCell = Constants.colorPressedCell

    def OnRender(self, display_surf):
        display_surf.blit(self.containerBg, (0, 0))
        '''repintamos el grid...'''
        self.Grid1.OnRender(display_surf)
        '''si la celda anterior  se ha apretado, la repintamos para que se vean los margenes bien'''
        if self.PressedCell != None:
            self.PressedCell.OnRender(display_surf)

    def isGameFinished(self):
        for cell in self.Grid1.Cells:
            if cell.idCell != cell.contentCell.id:
                return False
        return True
class ComplexAssociation(Activity):

    Grid1 = None
    PressedCell = None

    color = None

    def Load(self, display_surf):
        self.setBgColor(display_surf)

        self.desactGrid1 = []
        self.desactGrid2 = []
        ''' ----Dos posibilidades en XML----

         orientation   -1 Grid: Hay que doblar el tamano del Grid para duplicar las posibilidades
            -2 Grids: Hay que printar todas las posibilidades
        '''
        '''Loading xml values'''

        print "xmlActiviti!!!!!!!!!!!!!!!!!!"
        self.inverse = False
        try:
            if (self.xmlActivity.getAttribute('inverse') == 'true'):
                self.inverse = True
        except:
            self.inverse = False

        orientation = self.xmlActivity.getElementsByTagName(
            'layout')[0].getAttribute('position')

        xmlGrid1 = self.xmlActivity.getElementsByTagName('cells')[0]
        self.Grid1 = Grid(xmlGrid1, self.pathToMedia)
        print xmlGrid1.toxml()
        xmlGrid2 = self.xmlActivity.getElementsByTagName('cells')[1]
        self.Grid2 = Grid(xmlGrid2, self.pathToMedia)

        try:
            xmlGrid3 = self.xmlActivity.getElementsByTagName('cells')[2]
            self.Grid3 = Grid(xmlGrid3, self.pathToMedia)
        except:
            self.Grid3 = Grid()

        print "proporcionsss!!!!!!!!!!!!!!!!!"

        print self.Grid1.cellWidth
        print self.Grid2.cellWidth

        width = self.Grid1.cellWidth * self.Grid1.numCols
        height = self.Grid1.cellHeight * self.Grid1.numRows

        width2 = self.Grid2.cellWidth * self.Grid2.numCols
        height2 = self.Grid2.cellHeight * self.Grid2.numRows

        if orientation == 'AUB' or orientation == 'BUA':
            '''Sumamos el height al tamano'''
            height = height + self.Grid2.cellHeight
        else:
            '''Sumamos el width al tamano total'''
            width = width + self.Grid2.cellWidth

        print self.Grid2.cellHeight
        print self.Grid1.cellHeight
        '''Maximize size'''
        coef = self.calculateCoef(width, height)
        #coefx = self.calculateCoefPart(height + height2 + 10)
        height = self.Grid1.cellHeight * self.Grid1.numRows * coef
        width = self.Grid1.cellWidth * self.Grid1.numCols * coef

        #coef = self.calculateCoef(width2, height2)
        #coefx = self.calculateCoefPart(height2)
        height2 = self.Grid2.cellHeight * self.Grid2.numRows * coef
        width2 = self.Grid2.cellWidth * self.Grid2.numCols * coef

        print "paramentres"
        print height
        print self.Grid1.cellHeight
        print self.Grid1.numRows
        '''Loading constants for the activity'''

        xActual = Constants.MARGIN_TOP
        yActual = Constants.MARGIN_LEFT
        '''Cargamos grupo de celdas comunes...'''
        cellsPrimary = self.xmlActivity.getElementsByTagName('cells')[0]
        '''Load grid...
            Notar que el NumCols se ha multiplicado x 2 para Duplicar el tamano del Grid...
        '''
        ''' print "1"
	print self.Grid1.numRows
	print self.Grid1.numCols
	print width
	print height
	print xActual
	print yActual

        print "2"
	print self.Grid2.numRows
	print self.Grid2.numCols
	print width2
	print height2
	print xActual
	print yActual'''

        xGrid1 = (Constants.ACTIVITY_WIDTH - width) / 2
        yGrid1 = (Constants.ACTIVITY_HEIGHT - height) / 2
        xGrid1 = max(xGrid1, xActual)
        yGrid1 = max(yGrid1, yActual)

        if orientation == 'AUB' or orientation == 'BUA':
            '''Sumamos el height al tamano'''
            newWidth = width
            newHeight = self.Grid2.cellHeight * coef
            yGrid1 = (Constants.ACTIVITY_HEIGHT - height - newHeight - 10) / 2
            yGrid1 = max(yGrid1, yActual)
            if (self.Grid2.imagePath == None):
                self.Grid2.Load(self.Grid2.numRows, self.Grid2.numCols, width,
                                newHeight, xGrid1, yGrid1 + height + 10,
                                display_surf)
            else:
                self.Grid2.LoadWithImage(self.Grid2.numRows,
                                         self.Grid2.numCols, width, newHeight,
                                         xGrid1, yGrid1 + height + 10,
                                         display_surf, self.pathToMedia)
        else:
            '''Sumamos el width al tamano total'''
            newWidth = self.Grid2.cellWidth * coef
            newHeight = height
            xGrid1 = (Constants.ACTIVITY_WIDTH - width - newWidth - 10) / 2
            xGrid1 = max(xGrid1, xActual)
            if (self.Grid2.imagePath == None):
                self.Grid2.Load(self.Grid2.numRows, self.Grid2.numCols,
                                newWidth, height, xGrid1 + width + 10, yGrid1,
                                display_surf)
            else:
                self.Grid2.LoadWithImage(self.Grid2.numRows,
                                         self.Grid2.numCols, newWidth, height,
                                         xGrid1 + width + 10, yGrid1,
                                         display_surf, self.pathToMedia)

        if self.Grid1.imagePath == None:
            self.Grid1.Load(self.Grid1.numRows, self.Grid1.numCols, width,
                            height, xGrid1, yGrid1, display_surf)

        try:
            print "pasa1"
            '''if cells 3 not exists, only create an empty Grid'''
            if self.Grid3.imagePath == None:
                if self.inverse == False:
                    self.Grid3.Load(self.Grid1.numRows, self.Grid1.numCols,
                                    width, height, xActual, yActual,
                                    display_surf)
                else:
                    self.Grid3.Load(self.Grid2.numRows, self.Grid2.numCols,
                                    newWidth, newHeight, xActual, yActual,
                                    display_surf)
                cellsSolved = self.xmlActivity.getElementsByTagName('cells')[2]
                self.styleCell3 = StyleCell(cellsSolved)
                cells = cellsSolved.getElementsByTagName('cell')
                #self.styleCell3 = StyleCell(self.xmlGrid3)

                i = 0
                for cell in cells:
                    print "ho fa??"
                    self.printxmlCellinCell(self.Grid3.Cells[i], cell,
                                            self.styleCell3)

                    i = i + 1
            else:
                print "pasa3"
                self.Grid3.LoadWithImage(self.Grid1.numRows,
                                         self.Grid1.numCols, width, height,
                                         xGrid1, yGrid1, display_surf,
                                         self.pathToMedia)
        except:
            print "pasa4"
            pass

        if self.xmlActivity.getElementsByTagName('cells').length >= 2:
            '''Cargamos secondaryCells'''
            cellsSecondary = self.xmlActivity.getElementsByTagName('cells')[1]
            self.styleCell2 = StyleCell(cellsSecondary)
            '''Cargamos primer Grid del XML'''
            cells = cellsPrimary.getElementsByTagName('cell')
            self.styleCell = StyleCell(cellsPrimary)
            '''indexCell  = Numero de Celda que ocupa:'''
            indexCell = 0
            indexCell = self.doBucle(cells, indexCell)
            '''Cargamos segundo Grid del XML'''
            cells2 = cellsSecondary.getElementsByTagName('cell')

            if self.Grid1.imagePath != None:
                ''' 1 Imagen de fondo '''
                self.Grid1.LoadWithImage(self.Grid1.numRows,
                                         self.Grid1.numCols, width, height,
                                         xGrid1, yGrid1, display_surf,
                                         self.pathToMedia)

            print "whola"
            #print cells[0].toxml()
            #print cellsSecondary.toxml()

            #self.doBucle(cellsSecondary,0)
            #indexCell = self.doBucle(cells,indexCell)

            i = 0
            id = 0

            #print "imagepath=",self.Grid2.imagePath
            if self.Grid2.imagePath == None:
                self.styleCell2 = StyleCell(cellsSecondary)

                for cell in cells2:
                    self.printxmlCellinCell(self.Grid2.Cells[i], cell,
                                            self.styleCell2)
                    '''Guardamos las imagenes en el Grid'''
                    self.Grid2.Cells[i].contentCell.img2 = self.Grid2.Cells[
                        i].contentCell.img
                    self.Grid2.Cells[i].contentCell.id = id
                    id = id + 1
                    i = i + 1

    #indexCell = self.doBucle(cells2,indexCell)

        else:
            '''indexCell  = Numero de Celda que ocupa:'''
            indexCell = 0
            '''Cargamos primer Grid del XML'''
            cells = cellsPrimary.getElementsByTagName('cell')
            self.styleCell = StyleCell(cellsPrimary)
            indexCell = self.doBucle(cells, indexCell)
            '''Recargamos el primer Grid del XML'''
            indexCell = self.doBucle(cells, indexCell)

        if self.Grid1.imagePath == None:
            if (len(self.Grid1.Cells) == len(self.Grid3.Cells)):
                self.Grid1.unsort(self.Grid3)
            else:
                self.Grid1.unsort()

        if self.Grid2.imagePath == None:
            self.Grid2.unsort()

    def doBucle(self, cells, i):
        id = 0
        for cell in cells:
            self.printxmlCellinCell(self.Grid1.Cells[i], cell, self.styleCell)
            print "---------------------------------------------------"
            print "ids grid2"
            print self.Grid1.ids
            '''Guardamos las imagenes en el Grid'''
            self.Grid1.Cells[i].contentCell.img2 = self.Grid1.Cells[
                i].contentCell.img
            if (self.Grid1.ids == []):
                id = cell.getAttribute('id')
                if cell.hasAttribute('id'):
                    self.Grid1.Cells[i].contentCell.id = int(
                        cell.getAttribute('id'))
                    id2 = id
                else:
                    self.Grid1.Cells[i].contentCell.id = int(id2)
            else:
                self.Grid1.Cells[i].contentCell.id = int(self.Grid1.ids[i])

            i = i + 1
        return i

    def OnEvent(self, PointOfMouse):
        for cell in self.Grid1.Cells:
            if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                #si la celda ya ha sido
                if self.desactGrid1.count(
                        cell) == 0 and cell.contentCell.id != -1:
                    # celda anterior apretada...
                    if self.PressedCell != None:
                        #los dos son iguales
                        print 'id PressedCell = ', self.PressedCell.contentCell.id
                        print 'id cell = ', cell.contentCell.id
                        if self.PressedCell.contentCell.id == cell.contentCell.id:
                            #if self.PressedCell.idCell != cell.idCell:
                            if self.PressedGrid == 2:
                                if self.inverse == False:
                                    print 'COINCIDEN LAS CELDAS!!'
                                    #self.desactGrid1.append(cell)
                                    #cell.contentCell.img = cell.contentCell.img2
                                    self.PressedCell.actualColorCell = Constants.colorCell
                                    cell.contentCell.img.fill(
                                        Constants.colorBackground)
                                    cell.contentCell.img = self.Grid3.Cells[
                                        cell.idCell].contentCell.img
                                    #anulamos valor de img2 para indicar k ta ok
                                    #cell.contentCell.img = Constants.colorCell
                                    cell.contentCell.img2 = None
                                    self.PressedCell = None
                                    self.desactGrid1.append(cell)
                                else:
                                    self.desactGrid2.append(self.PressedCell)
                                    self.PressedCell.actualColorCell = Constants.colorCell
                                    self.PressedCell.contentCell.img.fill(
                                        Constants.colorBackground)
                                    self.PressedCell.contentCell.img = self.Grid3.Cells[
                                        0].contentCell.img
                                    self.PressedCell.contentCell.img2 = None
                                    self.PressedCell = None
                        #los dos son diferentes..
                        else:

                            #self.changeSecondImage(self.PressedCell)
                            self.PressedCell.actualColorCell = Constants.colorCell
                            #self.changeSecondImage(cell)
                            self.PressedCell = None
                            self.PressedGrid = 1

                    #celda anterior no apretada
                    else:
                        self.PressedCell = cell
                        self.PressedGrid = 1
                        #self.changeSecondImage(cell)
                        self.PressedCell.actualColorCell = Constants.colorPressedCell

        for cell in self.Grid2.Cells:
            if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                #si la celda ya ha sido
                print self.desactGrid2.count(cell)
                if self.desactGrid2.count(cell) == 0:
                    # celda anterior apretada...
                    if self.PressedCell != None:
                        #los dos son iguales
                        print 'id PressedCell = ', self.PressedCell.contentCell.id
                        print 'id cell = ', cell.contentCell.id
                        if self.PressedCell.contentCell.id == cell.contentCell.id:
                            #if self.PressedCell.idCell != cell.idCell:
                            if self.PressedGrid == 1:
                                if self.inverse == False:
                                    print 'COINCIDEN LAS CELDAS!!'
                                    self.desactGrid1.append(self.PressedCell)
                                    #cell.contentCell.img = cell.contentCell.img2
                                    self.PressedCell.actualColorCell = Constants.colorCell
                                    self.PressedCell.contentCell.img.fill(
                                        Constants.colorBackground)
                                    self.PressedCell.contentCell.img = self.Grid3.Cells[
                                        self.PressedCell.
                                        idCell].contentCell.img
                                    self.PressedCell.contentCell.borders = False
                                    #anulamos valor de img2 para indicar k ta ok
                                    #cell.contentCell.img = Constants.colorCell
                                    self.PressedCell.contentCell.img2 = None
                                    self.PressedCell = None
                                else:
                                    self.desactGrid2.append(self.PressedCell)
                                    self.PressedCell.actualColorCell = Constants.colorCell
                                    cell.contentCell.img.fill(
                                        Constants.colorBackground)
                                    cell.contentCell.img = self.Grid3.Cells[
                                        0].contentCell.img
                                    cell.contentCell.img2 = None
                                    self.PressedCell = None
                        #los dos son diferentes..
                        else:

                            #self.changeSecondImage(self.PressedCell)
                            self.PressedCell.actualColorCell = Constants.colorCell
                            #self.changeSecondImage(cell)
                            self.PressedCell = None
                            self.PressedGrid = 2

                    #celda anterior no apretada
                    else:
                        self.PressedCell = cell
                        self.PressedGrid = 2
                        #self.changeSecondImage(cell)
                        self.PressedCell.actualColorCell = Constants.colorPressedCell

    def changeSecondImage(self, cell):
        tmpImg = cell.contentCell.img
        cell.contentCell.img = cell.contentCell.img2
        cell.contentCell.img2 = tmpImg

    def OnRender(self, display_surf):
        display_surf.blit(self.containerBg, (0, 0))
        #repintamos el grid...
        self.Grid1.OnRender(display_surf)
        self.Grid2.OnRender(display_surf)
        #si la celda se ha apretado, la pintamos ( por los bordes)
        if self.PressedCell != None:
            self.PressedCell.OnRender(display_surf)

    def isOverActivity(self, PointOfMouse):
        return True

    def isGameFinished(self):
        finish = False
        desact = len(self.desactGrid1)
        i = 0
        for id in self.Grid1.ids:
            print "id =", id
            if id != "-1":
                i += 1

        if i == 0:
            i = len(self.Grid1.Cells)

        if desact == i:
            finish = True

        print desact, " == ", i
        return finish
class DoublePuzzle(Activity):

    Grid1 = None

    activity = None
    PressedCell = None
    FinishGame = False

    def Load(self, display_surf):
        self.setBgColor(display_surf)
        '''Loading xml values'''
        xmlGrid1 = self.xmlActivity.getElementsByTagName('cells')[0]
        self.styleCell = StyleCell(xmlGrid1)
        self.Grid1 = Grid(xmlGrid1)
        self.Grid2 = Grid(xmlGrid1)
        ''' Calculate Real size'''
        height = self.Grid1.cellHeight * self.Grid1.numRows
        width = self.Grid1.cellWidth * self.Grid1.numCols
        print 'widht:', width, 'height:', height

        try:
            layout = self.xmlActivity.getElementsByTagName(
                'layout')[0].getAttribute('position')
        except:
            layout = 'AB'
        if layout == 'AB':
            width = width * 2 + 5
        else:
            height = height * 2 + 5
        '''Maximize size'''
        coef = self.calculateCoef(width, height)
        height = int(height * coef)
        width = int(width * coef)
        print 'maximized -> widht:', width, 'height:', height
        '''Loading constants for the activity'''

        xActual = Constants.MARGIN_TOP
        yActual = Constants.MARGIN_LEFT

        if layout == 'AB':
            width = (width - 5) / 2
            xGrid1 = (Constants.ACTIVITY_WIDTH - width * 2 - 10) / 2
            yGrid1 = (Constants.ACTIVITY_HEIGHT - height) / 2
        else:
            height = (height - 5) / 2
            xGrid1 = (Constants.ACTIVITY_WIDTH - width) / 2
            yGrid1 = (Constants.ACTIVITY_HEIGHT - height * 2 - 10) / 2

        xGrid1 = max(xGrid1, xActual)
        yGrid1 = max(yGrid1, yActual)
        ''' 1 Imagen de fondo '''
        self.Grid1.imagePath = self.mediaInformation[self.Grid1.imagePath]
        self.Grid1.LoadWithImage(self.Grid1.numRows, self.Grid1.numCols, width,
                                 height, xGrid1, yGrid1, display_surf,
                                 self.pathToMedia)

        if layout == 'AB':
            xGrid2 = xGrid1 + width + 10
            yGrid2 = yGrid1
        else:
            xGrid2 = xGrid1
            yGrid2 = yGrid1 + height + 10

        xGrid2 = max(xGrid2, xActual)
        yGrid2 = max(yGrid2, yActual)

        self.Grid2.imagePath = self.mediaInformation[self.Grid2.imagePath]
        self.Grid2.LoadWithImage(self.Grid1.numRows, self.Grid1.numCols, width,
                                 height, xGrid2, yGrid2, display_surf,
                                 self.pathToMedia)

        for cell in self.Grid1.Cells:
            cell.contentCell.img.fill(Constants.colorBackground)

        self.Grid2.unsort()
        ''' A image 1 le quitamos la imagen'''

    def OnEvent(self, PointOfMouse):
        '''
            -----------LOGICS OF THE GAME-----------
            self.PressedCell = celda anterior
            cell = celda actual
        '''
        '''Hay una celda apretada.. anteriormente'''
        if self.PressedCell != None:
            ''' si click en alguna celda izkierda '''
            for cell in self.Grid1.Cells:
                if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                    ''' si ids son iguales --> cambiamos celdas.. '''
                    if cell.contentCell.id == self.PressedCell.contentCell.id:
                        tmpImg = self.PressedCell.contentCell.img
                        self.PressedCell.contentCell.img = cell.contentCell.img
                        cell.contentCell.img = tmpImg
                        self.PressedCell.contentCell.id = -1
                        self.play_sound(Constants.Sounds.OK)
                    else:
                        self.play_sound(Constants.Sounds.ERROR)
                    ''' desmarcamos pressedCell'''
                    self.PressedCell.actualColorCell = Constants.colorCell
                    self.PressedCell = None
            '''si click en derecha.. '''
            for cell in self.Grid2.Cells:
                if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                    if cell.contentCell.id != -1:
                        ''' cambiamos el pressedCell '''
                        self.PressedCell.actualColorCell = Constants.colorCell
                        self.PressedCell = None
                        self.PressedCell = cell
                        self.PressedCell.actualColorCell = Constants.colorPressedCell

        else:
            self.play_sound(Constants.Sounds.CLICK)
            ''' si click en alguna celda izkierda nothing...--> no ponemos nada '''
            '''si click en derecha.. '''
            for cell in self.Grid2.Cells:
                if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                    ''' cambiamos el pressedCell a esta celda..-> si no ha sido ya cambiada...'''
                    if cell.contentCell.id != -1:

                        self.PressedCell = cell
                        self.PressedCell.actualColorCell = Constants.colorPressedCell

    def OnRender(self, display_surf):
        display_surf.blit(self.containerBg, (0, 0))
        '''repintamos el grid...'''
        self.Grid1.OnRender(display_surf)
        self.Grid2.OnRender(display_surf)
        '''si la celda anterior  se ha apretado, la repintamos para que se vean los margenes bien'''
        if self.PressedCell != None:
            self.PressedCell.OnRender(display_surf)

    def isGameFinished(self):
        for cell in self.Grid2.Cells:
            if cell.contentCell.id != -1:
                return False
        return True
class InformationScreen(Activity):

    Grid1 = None
    
    
 
    def Load(self, display_surf ):
        self.setBgColor(display_surf)

        '''Loading constants for the activity'''

        xActual=Constants.MARGIN_TOP
        yActual=Constants.MARGIN_LEFT
        xmlGrid1 = self.xmlActivity.getElementsByTagName('cells')[0]
        
        self.Grid1 = Grid(xmlGrid1)
        
        height = self.Grid1.cellHeight * self.Grid1.numRows
        width = self.Grid1.cellWidth * self.Grid1.numCols
        
        '''Maximize size'''
        
        coef = self.calculateCoef(width, height)
                 
        height = self.Grid1.cellHeight * self.Grid1.numRows * coef
        width = self.Grid1.cellWidth * self.Grid1.numCols * coef
            

        if self.Grid1.imagePath != None:
            width= Constants.ACTIVITY_WIDTH 
            height =Constants.ACTIVITY_HEIGHT
            self.Grid1.imagePath = self.mediaInformation[self.Grid1.imagePath]
            self.Grid1.LoadWithImage(1,1,width,height,xActual ,yActual, display_surf,self.pathToMedia)
        else:
            xGrid = (Constants.ACTIVITY_WIDTH - width) / 2
            yGrid = (Constants.ACTIVITY_HEIGHT - height) / 2
            xGrid = max(xGrid,xActual)
            yGrid = max(yGrid,yActual)
            
            self.Grid1.Load(self.Grid1.numRows, self.Grid1.numCols, width, height, xGrid, yGrid, display_surf)
            i = 0
            cells = xmlGrid1.getElementsByTagName('cell')
            self.styleCell = StyleCell(xmlGrid1)
            if cells.length !=0:
                for cell in cells: 
                    self.printxmlCellinCell(self.Grid1.Cells[i], cell, self.styleCell)
                    
                    i = i+1 
            else:
                self.Grid1 = None
      

    def OnEvent(self,PointOfMouse):
        '''
           Pantalla informativa, no hacemos nada...
        '''
        for cell in self.Grid1.Cells:
            if cell.isOverCell(PointOfMouse[0],PointOfMouse[1]):
                if cell.redirect != None:
                    print "redirecciona!!!!!!!!!!!!!!!!!!!", cell.redirect
                    return cell.redirect

    def OnRender(self,display_surf):
        display_surf.blit(self.containerBg,(0,0))
        '''repintamos el grid...'''
        if self.Grid1 != None:
            self.Grid1.OnRender(display_surf)
        

    def isGameFinished(self):
        '''Never will finish. This is a Information Activity '''
        return False

        
Esempio n. 5
0
class WordSearch(Activity):

    choiceChar = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    ]
    textGrid = None
    cellsGrid = None
    auxGrid = None
    clues = []
    cols = 0
    pressedCellIni = None
    pressedCellFi = None
    encerts = 0
    xmlCells = None
    xmlText = None
    render = False
    textHasBorder = False

    def Load(self, display_surf):
        self.setBgColor(display_surf)
        '''Loading xml values'''
        '''xml amb la sopa de lletres'''
        self.xmlText = self.xmlActivity.getElementsByTagName('textGrid')[0]
        self.styleCell = StyleCell(self.xmlText)
        '''xml amb els ids que correspon a cada paraula'''
        xmlClues = self.xmlActivity.getElementsByTagName('clues')[0]
        try:
            '''xml amb les imatges a mostrar per cada paraula'''
            self.xmlCells = self.xmlActivity.getElementsByTagName('cells')[0]
            self.hasCells = True
        except:
            pass
        '''Crea el grid de la sopa'''
        self.textGrid = Grid(self.xmlText)
        if self.hasCells:
            '''Crea el grid de les imatges'''
            self.cellsGrid = Grid(self.xmlCells)
            '''Grid auxiliar per anar mostrant les imatges'''
            self.auxGrid = Grid(self.xmlCells)
        '''Loading clues'''
        self.clues = []
        tmpClues = xmlClues.getElementsByTagName('clue')
        for i in range(0, len(tmpClues)):
            self.clues.append(tmpClues[i].firstChild.data)

        if self.hasCells:
            orientation = self.xmlActivity.getElementsByTagName(
                'layout')[0].getAttribute('position')
        else:
            orientation = 'AB'
        ''' Calculate Real size'''
        heightText = self.textGrid.cellHeight * self.textGrid.numRows
        widthText = self.textGrid.cellWidth * self.textGrid.numCols

        if self.hasCells:
            heightCells = self.cellsGrid.cellHeight * self.cellsGrid.numRows
            widthCells = self.cellsGrid.cellWidth * self.cellsGrid.numCols

            if heightCells < heightText:
                relation = heightText / heightCells
                heightCells = heightText
                widthCells = widthCells * relation
        else:
            heightCells = 0
            widthCells = 0

        if self.hasCells:
            '''Maximize size'''
            if orientation == 'AB' or 'BA':
                if heightText < heightCells:
                    coef = self.calculateCoef(widthText + widthCells,
                                              heightCells)
                else:
                    coef = self.calculateCoef(widthText + widthCells,
                                              heightText)
            else:
                if widthText < widthCells:
                    coef = self.calculateCoef(widthCells,
                                              heightText + heightCells)
                else:
                    coef = self.calculateCoef(widthText,
                                              heightText + heightCells)
        else:
            coef = self.calculateCoef(widthText, heightText)

        heightText = heightText * coef
        widthText = widthText * coef

        heightCells = heightCells * coef
        widthCells = widthCells * coef
        '''Loading constants for the activity'''

        xActual = Constants.MARGIN_TOP
        yActual = Constants.MARGIN_LEFT

        if self.hasCells:
            if self.cellsGrid.imagePath != None:
                self.cellsGrid.imagePath = self.mediaInformation[
                    self.cellsGrid.imagePath]
                self.cellsGrid.LoadWithImage(self.cellsGrid.numRows,
                                             self.cellsGrid.numCols,
                                             widthCells, heightCells, 1, 1,
                                             display_surf, self.pathToMedia)
            else:
                self.cellsGrid.Load(self.cellsGrid.numRows,
                                    self.cellsGrid.numCols, widthCells,
                                    heightCells, 1, 1, display_surf)

        if orientation == 'AB':
            xText = (Constants.ACTIVITY_WIDTH - widthText - widthCells -
                     10) / 2
            yText = (Constants.ACTIVITY_HEIGHT - heightText) / 2
            xText = max(xActual, xText)
            yText = max(yActual, yText)
            xCells = xText + widthText + 10
            yCells = (Constants.ACTIVITY_HEIGHT - heightCells) / 2
            yCells = max(yActual, yCells)
            self.textGrid.Load(self.textGrid.numRows, self.textGrid.numCols,
                               widthText, heightText, xText, yText,
                               display_surf)
            if self.hasCells:
                self.auxGrid.Load(self.cellsGrid.numRows,
                                  self.cellsGrid.numCols, widthCells,
                                  heightCells, xCells, yCells, display_surf)
        elif orientation == 'BA':
            xCells = (Constants.ACTIVITY_WIDTH - widthText - widthCells -
                      10) / 2
            yCells = (Constants.ACTIVITY_HEIGHT - heightCells) / 2
            xCells = max(xActual, xCells)
            yCells = max(yActual, yCells)
            xText = xCells + widthCells + 10
            yText = (Constants.ACTIVITY_HEIGHT - heightText) / 2
            yText = max(yActual, yText)
            if self.hasCells:
                self.auxGrid.Load(self.cellsGrid.numRows,
                                  self.cellsGrid.numCols, widthCells,
                                  heightCells, xCells, yCells, display_surf)
            self.textGrid.Load(self.textGrid.numRows, self.textGrid.numCols,
                               widthText, heightText, xText, yText,
                               display_surf)
        elif orientation == 'AUB':
            xText = (Constants.ACTIVITY_WIDTH - widthText) / 2
            yText = (Constants.ACTIVITY_HEIGHT - heightText - heightCells -
                     10) / 2
            xText = max(xActual, xText)
            yText = max(yActual, yText)
            xCells = (Constants.ACTIVITY_WIDTH - widthCells) / 2
            yCells = yText + heightText + 10
            xCells = max(xActual, xCells)
            self.textGrid.Load(self.textGrid.numRows, self.textGrid.numCols,
                               widthText, heightText, xText, yText,
                               display_surf)
            if self.hasCells:
                self.auxGrid.Load(self.cellsGrid.numRows,
                                  self.cellsGrid.numCols, widthCells,
                                  heightCells, xCells, yCells, display_surf)
        elif orientation == 'BUA':
            xCells = (Constants.ACTIVITY_WIDTH - widthCells) / 2
            yCells = (Constants.ACTIVITY_HEIGHT - heightText - heightCells -
                      10) / 2
            xCells = max(xActual, xCells)
            yCells = max(yActual, yCells)
            xText = (Constants.ACTIVITY_WIDTH - widthText) / 2
            yText = yCells + heightCells + 10
            xText = max(xActual, xText)
            self.textGrid.Load(self.textGrid.numRows, self.textGrid.numCols,
                               widthText, heightText, xText, yText,
                               display_surf)
            if self.hasCells:
                self.auxGrid.Load(self.cellsGrid.numRows,
                                  self.cellsGrid.numCols, widthCells,
                                  heightCells, xCells, yCells, display_surf)

        self.textHasBorder = self.textGrid.hasBorder

        text = []
        rows = self.xmlText.getElementsByTagName('row')
        for row in rows:
            row = row.firstChild.data
            self.cols = len(row)
            for i in range(0, self.cols):
                letter = row[i]
                if letter == '*':
                    letter = random.choice(self.choiceChar)
                text.append(letter)

        for i in range(0, len(text)):
            self.textGrid.Cells[i].contentCell.letter = text[i]
            self.printLetterinCell(self.textGrid.Cells[i], self.xmlText)

    def OnEvent(self, PointOfMouse):
        '''
            -----------LOGICS OF THE GAME-----------
            self.PressedCell = celda anterior
            cell = celda actual
        '''
        if self.pressedCellIni == None:
            for cell in self.textGrid.Cells:
                if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                    print cell.contentCell.id
                    self.pressedCellIni = cell
                    self.pressedCellIni.actualColorCell = Constants.colorPressedCell
        else:
            select = []
            for cell in self.textGrid.Cells:
                if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]):
                    print cell.contentCell.id
                    self.pressedCellFi = cell
            '''Agafa la paraula seleccionada'''
            if self.pressedCellFi != None and self.pressedCellIni != None:
                orientacio = self.orientacioParaula()

                if orientacio == 'horitzontal':
                    '''Paraula en horitzontal'''
                    if self.pressedCellFi.contentCell.id > self.pressedCellIni.contentCell.id:
                        for i in range(self.pressedCellIni.contentCell.id,
                                       self.pressedCellFi.contentCell.id + 1):
                            select.append(
                                self.textGrid.Cells[i].contentCell.letter)
                    else:
                        for i in range(self.pressedCellFi.contentCell.id,
                                       self.pressedCellIni.contentCell.id + 1):
                            select.append(
                                self.textGrid.Cells[i].contentCell.letter)

                elif orientacio == 'vertical':
                    '''Paraula en vertical'''
                    if self.pressedCellFi.contentCell.id > self.pressedCellIni.contentCell.id:
                        for i in range(self.pressedCellIni.contentCell.id,
                                       self.pressedCellFi.contentCell.id + 1,
                                       self.cols):
                            select.append(
                                self.textGrid.Cells[i].contentCell.letter)
                    else:
                        for i in range(self.pressedCellFi.contentCell.id,
                                       self.pressedCellIni.contentCell.id + 1,
                                       self.cols):
                            select.append(
                                self.textGrid.Cells[i].contentCell.letter)

                #Paraula en diagonal: diferents opcions
                elif orientacio == 'dretaBaix':
                    for i in range(self.pressedCellIni.contentCell.id,
                                   self.pressedCellFi.contentCell.id + 1,
                                   self.cols + 1):
                        select.append(
                            self.textGrid.Cells[i].contentCell.letter)
                elif orientacio == 'dretaDalt':
                    for i in range(self.pressedCellIni.contentCell.id,
                                   self.pressedCellFi.contentCell.id - 1,
                                   -self.cols + 1):
                        select.append(
                            self.textGrid.Cells[i].contentCell.letter)
                elif orientacio == 'esqBaix':
                    for i in range(self.pressedCellIni.contentCell.id,
                                   self.pressedCellFi.contentCell.id + 1,
                                   self.cols - 1):
                        select.append(
                            self.textGrid.Cells[i].contentCell.letter)
                elif orientacio == 'esqDalt':
                    for i in range(self.pressedCellIni.contentCell.id,
                                   self.pressedCellFi.contentCell.id - 1,
                                   -self.cols - 1):
                        select.append(
                            self.textGrid.Cells[i].contentCell.letter)

                self.pressedCellIni.actualColorCell = Constants.colorCell
                '''Comproba si es una paraula correcta'''
                parfw = ''  #Agafa la paraula de principi a fi (forward)
                parbw = ''  #Agafa la paraula al reves (backward)
                for l in select:
                    parfw = parfw + l
                    parbw = l + parbw
                print parfw
                clueID = self.isWordCorrect(parfw, parbw)
                if clueID >= 0 and self.clues[clueID] != None:
                    self.encerts += 1
                    self.clues[clueID] = None
                    if self.hasCells:
                        if self.cellsGrid.imagePath != None:
                            '''Una sola imatge dividida a les cells'''
                            self.auxGrid.Cells[
                                clueID].contentCell.img = self.cellsGrid.Cells[
                                    clueID].contentCell.img
                        else:
                            '''Una imatge per cada cell'''
                            xmlCell = self.xmlCells.getElementsByTagName(
                                'cell')[clueID]
                            self.printxmlCellinCell(self.auxGrid.Cells[clueID],
                                                    xmlCell)
                    print 'paraula trobada'
                    if orientacio == 'horitzontal':
                        if self.pressedCellFi.contentCell.id > self.pressedCellIni.contentCell.id:
                            for i in range(
                                    self.pressedCellIni.contentCell.id,
                                    self.pressedCellFi.contentCell.id + 1):
                                self.printLetterinCell(self.textGrid.Cells[i],
                                                       self.xmlText,
                                                       Constants.colorWhite,
                                                       Constants.colorBlack)
                        else:
                            for i in range(
                                    self.pressedCellFi.contentCell.id,
                                    self.pressedCellIni.contentCell.id + 1):
                                self.printLetterinCell(self.textGrid.Cells[i],
                                                       self.xmlText,
                                                       Constants.colorWhite,
                                                       Constants.colorBlack)
                    elif orientacio == 'vertical':
                        if self.pressedCellFi.contentCell.id > self.pressedCellIni.contentCell.id:
                            for i in range(
                                    self.pressedCellIni.contentCell.id,
                                    self.pressedCellFi.contentCell.id + 1,
                                    self.cols):
                                self.printLetterinCell(self.textGrid.Cells[i],
                                                       self.xmlText,
                                                       Constants.colorWhite,
                                                       Constants.colorBlack)
                        else:
                            for i in range(
                                    self.pressedCellFi.contentCell.id,
                                    self.pressedCellIni.contentCell.id + 1,
                                    self.cols):
                                self.printLetterinCell(self.textGrid.Cells[i],
                                                       self.xmlText,
                                                       Constants.colorWhite,
                                                       Constants.colorBlack)
                    elif orientacio == 'dretaBaix':
                        for i in range(self.pressedCellIni.contentCell.id,
                                       self.pressedCellFi.contentCell.id + 1,
                                       self.cols + 1):
                            self.printLetterinCell(self.textGrid.Cells[i],
                                                   self.xmlText,
                                                   Constants.colorWhite,
                                                   Constants.colorBlack)
                    elif orientacio == 'dretaDalt':
                        for i in range(self.pressedCellIni.contentCell.id,
                                       self.pressedCellFi.contentCell.id - 1,
                                       -self.cols + 1):
                            self.printLetterinCell(self.textGrid.Cells[i],
                                                   self.xmlText,
                                                   Constants.colorWhite,
                                                   Constants.colorBlack)
                    elif orientacio == 'esqBaix':
                        for i in range(self.pressedCellIni.contentCell.id,
                                       self.pressedCellFi.contentCell.id + 1,
                                       self.cols - 1):
                            self.printLetterinCell(self.textGrid.Cells[i],
                                                   self.xmlText,
                                                   Constants.colorWhite,
                                                   Constants.colorBlack)
                    elif orientacio == 'esqDalt':
                        for i in range(self.pressedCellIni.contentCell.id,
                                       self.pressedCellFi.contentCell.id - 1,
                                       -self.cols - 1):
                            self.printLetterinCell(self.textGrid.Cells[i],
                                                   self.xmlText,
                                                   Constants.colorWhite,
                                                   Constants.colorBlack)
                '''Reset pressedCells'''
                self.pressedCellIni.contentCell.border = self.textHasBorder
                self.pressedCellIni = None
                self.pressedCellFi = None

    def orientacioParaula(self):
        d = self.pressedCellFi.contentCell.id - self.pressedCellIni.contentCell.id

        if d > -self.cols and d < self.cols:
            return 'horitzontal'
        elif (d % self.cols) == 0:
            return 'vertical'
        else:
            '''Mira si es una diagonal correcta'''
            filaIni = self.pressedCellIni.contentCell.id // self.cols
            colIni = self.pressedCellIni.contentCell.id % self.cols
            filaFi = self.pressedCellFi.contentCell.id // self.cols
            colFi = self.pressedCellFi.contentCell.id % self.cols

            difFiles = filaIni - filaFi
            difCols = colIni - colFi
            if abs(difFiles) == abs(difCols):
                if filaIni < filaFi and colIni < colFi:
                    return 'dretaBaix'
                elif filaIni > filaFi and colIni > colFi:
                    return 'esqDalt'
                elif filaIni < filaFi and colIni > colFi:
                    return 'esqBaix'
                elif filaIni > filaFi and colIni < colFi:
                    return 'dretaDalt'

        return 'incorrecte'

    def isWordCorrect(self, parfw, parbw):
        result = -1
        for i in range(0, len(self.clues)):
            if self.clues[i] == parfw or self.clues[i] == parbw:
                result = i
        return result

    def OnRender(self, display_surf):
        display_surf.blit(self.containerBg, (0, 0))
        '''repintamos el grid...'''
        self.textGrid.OnRender(display_surf)
        if self.hasCells:
            self.auxGrid.OnRender(display_surf)
        if self.pressedCellIni != None:
            self.pressedCellIni.contentCell.border = True
            self.pressedCellIni.OnRenderPressedCell(display_surf)

        #self.cellsGrid.OnRender(display_surf)

    def isGameFinished(self):
        return self.encerts == len(self.clues)