class IdentifyPanels(Activity): Grid1 = None def Load(self, display_surf): self.setBgColor(display_surf) '''Loading xml values''' xmlGrid1 = self.xmlActivity.getElementsByTagName('cells')[0] self.Grid1 = Grid(xmlGrid1) try: xmlGrid2 = self.xmlActivity.getElementsByTagName('cells')[1] self.Grid2 = Grid(xmlGrid2) self.styleCell2 = StyleCell(xmlGrid2) except: self.Grid2 = Grid() '''only 1 Grid''' self.styleCell = StyleCell(xmlGrid1) ''' Calculate Real size''' width = self.Grid1.cellWidth * self.Grid1.numCols height = self.Grid1.cellHeight * self.Grid1.numRows ''' Calculamos porcentaje...''' '''Maximize size''' coef = self.calculateCoef(width, height) height = self.Grid1.cellHeight * self.Grid1.numRows * coef width = self.Grid1.cellWidth * self.Grid1.numCols * coef '''Loading constants for the activity''' xActual = Constants.MARGIN_TOP yActual = Constants.MARGIN_LEFT xGrid = (Constants.ACTIVITY_WIDTH - width) / 2 yGrid = (Constants.ACTIVITY_HEIGHT - height) / 2 xGrid = max(xGrid, xActual) yGrid = max(yGrid, yActual) '''Cargamos grupo de celdas comunes...''' ''' 1 Imagen por cada celda ( tipo texto)''' self.Grid1.Load(self.Grid1.numRows, self.Grid1.numCols, width, height, xGrid, yGrid, display_surf) cells = xmlGrid1.getElementsByTagName('cell') i = 0 for cell in cells: self.printxmlCellinCell(self.Grid1.Cells[i], cell, self.styleCell) id = int(cell.getAttribute('id')) self.Grid1.Cells[i].contentCell.id = id i = i + 1 try: '''if cells 2 not exists, only create an empty Grid''' self.Grid2.Load(self.Grid1.numRows, self.Grid1.numCols, width, height, xActual, yActual, display_surf) cells = xmlGrid2.getElementsByTagName('cell') i = 0 for cell in cells: self.printxmlCellinCell(self.Grid2.Cells[i], cell, self.styleCell2) i = i + 1 except: pass def OnEvent(self, PointOfMouse): ''' -----------LOGICS OF THE GAME----------- ''' for cell in self.Grid1.Cells: if cell.isOverCell(PointOfMouse[0], PointOfMouse[1]): if cell.contentCell.id != 0: print cell.contentCell.id if cell.contentCell.id == 1: cell.contentCell.img = self.Grid2.Cells[ cell.idCell].contentCell.img cell.contentCell.id = -1 self.play_sound(Constants.Sounds.OK) else: self.play_sound(Constants.Sounds.ERROR) '''pygame.mixer.music.play()''' def OnRender(self, display_surf): display_surf.blit(self.containerBg, (0, 0)) '''repintamos el grid...''' self.Grid1.OnRender(display_surf) def isGameFinished(self): for cell in self.Grid1.Cells: if cell.contentCell.id == 1: 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 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 MemoryGame(Activity): Grid1 = None PressedCell = None color = None def Load(self, display_surf): self.setBgColor(display_surf) ''' ----Dos posibilidades en XML---- -1 Grid: Hay que doblar el tamano del Grid para duplicar las posibilidades -2 Grids: Hay que printar todas las posibilidades ''' '''Loading xml values''' xmlGrid1 = self.xmlActivity.getElementsByTagName('cells')[0] self.Grid1 = Grid(xmlGrid1) self.styleCell = StyleCell(xmlGrid1) orientation = self.xmlActivity.getElementsByTagName( 'layout')[0].getAttribute('position') ''' Calculate Real size''' if orientation == 'AUB' or orientation == 'BUA': '''Sumamos el height al tamano''' self.Grid1.numRows = self.Grid1.numRows * 2 else: '''Sumamos el width al tamano total''' self.Grid1.numCols = self.Grid1.numCols * 2 width = self.Grid1.cellWidth * self.Grid1.numCols height = self.Grid1.cellHeight * self.Grid1.numRows '''Maximize size''' coef = self.calculateCoef(width, height) height = self.Grid1.cellHeight * self.Grid1.numRows * coef width = self.Grid1.cellWidth * self.Grid1.numCols * coef '''Loading constants for the activity''' xActual = Constants.MARGIN_TOP yActual = Constants.MARGIN_LEFT '''Calculate position to be centered''' xCenter = (Constants.ACTIVITY_WIDTH - width) / 2 xCenter = max(xActual, xCenter) yCenter = (Constants.ACTIVITY_HEIGHT - height) / 2 yCenter = max(yActual, yCenter) '''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... ''' self.Grid1.Load(self.Grid1.numRows, self.Grid1.numCols, width, height, xCenter, yCenter, display_surf) '''Caso de 2 Grids:''' 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') '''indexCell = Numero de Celda que ocupa:''' indexCell = 0 indexCell = self.doBucle(cells, indexCell, self.styleCell2) '''Cargamos segundo Grid del XML''' cells = cellsSecondary.getElementsByTagName('cell') self.doBucle(cells, indexCell, self.styleCell2) else: '''indexCell = Numero de Celda que ocupa:''' indexCell = 0 '''Cargamos primer Grid del XML''' cells = cellsPrimary.getElementsByTagName('cell') indexCell = self.doBucle(cells, indexCell, self.styleCell) '''Recargamos el primer Grid del XML''' indexCell = self.doBucle(cells, indexCell, self.styleCell) self.Grid1.unsort() def doBucle(self, cells, i, style): id = 0 for cell in cells: copy = self.Grid1.Cells[i].contentCell.img.copy() self.printxmlCellinCell(self.Grid1.Cells[i], cell, style) '''Guardamos las imagenes en el Grid''' self.Grid1.Cells[i].contentCell.img2 = self.Grid1.Cells[ i].contentCell.img self.Grid1.Cells[i].contentCell.img = copy self.Grid1.Cells[i].contentCell.id = id id = id + 1 i = i + 1 return i def OnEvent(self, PointOfMouse): for cell in self.Grid1.Cells: print 'antes de la instruccion pointOfMouse=', PointOfMouse x = PointOfMouse[0] y = PointOfMouse[1] if cell.isOverCell(x, y): #si la celda ya ha sido if cell.contentCell.img2 != None: # 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: print 'COINCIDEN LAS CELDAS!!' cell.contentCell.img = cell.contentCell.img2 #anulamos valor de img2 para indicar k ta ok cell.contentCell.img2 = None self.PressedCell.contentCell.img2 = None self.PressedCell = None self.play_sound(Constants.Sounds.OK) #los dos son diferentes.. else: self.play_sound(Constants.Sounds.ERROR) self.changeSecondImage(self.PressedCell) self.PressedCell.actualColorCell = Constants.colorCell self.changeSecondImage(cell) self.PressedCell = cell #celda anterior no apretada else: self.PressedCell = cell self.changeSecondImage(cell) 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) #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 = True for cell in self.Grid1.Cells: if cell.contentCell.img2 != None: finish = False return finish
class FillInBlanks(Activity): TextGrid = None PrevGrid = None targets = { } #Diccionari amb parells (idCell,encert) util quan hi ha optionLists options = None pressedCell = None idPressed = -1 #indica el numero de cell seleccionada respecte al response cell previous = False def Load(self, display_surf): self.setBgColor(display_surf) try: xmlPrevious = self.xmlActivity.getElementsByTagName( 'prevScreen')[0] self.previous = True self.PrevGrid = Grid() self.PrevGrid.Load(1, 1, Constants.ACTIVITY_WIDTH, Constants.ACTIVITY_HEIGHT, Constants.MARGIN_TOP, Constants.MARGIN_LEFT, display_surf) self.styleCell = StyleCell(xmlPrevious) self.printxmlCellinCell(self.PrevGrid.Cells[0], xmlPrevious, self.styleCell) except: pass '''Loading constants for the activity''' xmlTextGrid = self.xmlActivity.getElementsByTagName('document')[0] self.TextGrid = TextGrid(xmlTextGrid, self.mediaInformation, self.pathToMedia) self.targets = self.TextGrid.Load(display_surf, xmlTextGrid) opt = xmlTextGrid.getElementsByTagName('optionList') if len(opt) > 0: self.options = True else: self.options = False def OnEvent(self, PointOfMouse): if self.previous: '''Quan es fa el primer clic es passa a resoldre l'activitat''' self.previous = False else: for i in self.targets.keys(): print self.TextGrid.textCells[i].type if self.TextGrid.textCells[i].type == 'option': encert = self.TextGrid.textCells[i].isOverCell( PointOfMouse[0], PointOfMouse[1]) if encert: self.targets[i] = encert print self.targets else: id = self.TextGrid.textCells[i].isOverCell( PointOfMouse[0], PointOfMouse[1]) if id != None: self.pressedCell = self.TextGrid.textCells[i] self.idPressed = id print 'he asignado el pressedcell', self.pressedCell.idCell return else: self.pressedCell = None def OnKeyEvent(self, key): print 'idpressed', self.idPressed if self.pressedCell != None: if key == 'backspace' or key == 'delete': self.pressedCell.contentCell.writed[self.idPressed] = '_' self.pressedCell.contentCell.Cells[ self.idPressed].contentCell.letter = '_' else: self.pressedCell.contentCell.writed[self.idPressed] = key self.pressedCell.contentCell.Cells[ self.idPressed].contentCell.letter = key self.idPressed = self.pressedCell.contentCell.printLetter() def OnRender(self, display_surf): display_surf.blit(self.containerBg, (0, 0)) if self.previous: self.PrevGrid.OnRender(display_surf) else: '''repintamos el grid''' self.TextGrid.OnRender(display_surf) def isGameFinished(self): j = 0 for i in self.targets.keys(): if self.TextGrid.textCells[i].type == 'option': status = self.targets.values() '''Mira si els targets estan correctes o no''' if not status[j]: return False else: if self.TextGrid.textCells[ i].contentCell.answer != self.TextGrid.textCells[ i].contentCell.writed: return False j += 1 '''Si no ha retornat abans, tot esta correcte''' 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
class CrossWord(Activity): textGrid = None cellsAcrossGrid = None cellsDownGrid = None acrossClues = [] downClues = [] text = [] #Solucio per comprovar correctesa acrossItems = [] #item que li correspon a la cela downItems = [] blancs = 0 cols = 0 pressedTextCell = None #Cell of textGrid where to type pressedDirectionCell = None #Cell across or down acrossCell = None downCell = None direction = None #'across' or 'down' xmlCellsAcross = None xmlCellsDown = None xmlText = None textHasBorder = False def Load(self, display_surf ): self.setBgColor(display_surf) '''Loading xml values''' '''xml amb la solucio del crossword''' self.xmlText = self.xmlActivity.getElementsByTagName('textGrid')[0] '''xml amb les definicions de les paraules''' self.xmlCellsAcross = self.xmlActivity.getElementsByTagName('cells')[0] self.xmlCellsDown = self.xmlActivity.getElementsByTagName('cells')[1] '''Crea el grid del crossword''' self.textGrid = Grid(self.xmlText) self.styleCell = StyleCell(self.xmlText) '''Crea el grid de les definicions horitzontals''' self.cellsAcrossGrid = Grid(self.xmlCellsAcross) '''Crea el grid de les definicions verticals''' self.cellsDownGrid = Grid(self.xmlCellsDown) orientation = self.xmlActivity.getElementsByTagName('layout')[0].getAttribute('position') ''' Calculate Real size''' heightText = self.textGrid.cellHeight * self.textGrid.numRows widthText = self.textGrid.cellWidth * self.textGrid.numCols '''Dimensions de acrossGrid = downGrid, nomes agafo tamany amb un''' heightCells = self.cellsAcrossGrid.cellHeight widthCells = self.cellsAcrossGrid.cellWidth #+ self.cellsAcrossGrid.cellHeight #el cell de l'orientacio es igual d'alt, que d'ample '''Maximize size''' if orientation == 'AB' or 'BA': coef = self.calculateCoef(widthText+widthCells,heightText) else: if widthText >= (widthCells*2): coef = self.calculateCoef(widthText,heightText+heightCells) else: coef = self.calculateCoef(widthCells*2,heightText+heightCells) heightText = heightText * coef widthText = widthText * coef heightCells = heightCells * coef widthCells = widthCells * coef print 'heightCell: ' , heightCells print 'widthCell: ', widthCells '''Loading constants for the activity''' xActual=Constants.MARGIN_TOP yActual=Constants.MARGIN_LEFT if orientation == 'AB': xText = (Constants.ACTIVITY_WIDTH - widthText - widthCells - 10) / 2 xText = max(xActual,xText) yText = (Constants.ACTIVITY_HEIGHT - heightText) / 2 xCells = xText + widthText + 10 yCells = (Constants.ACTIVITY_HEIGHT - heightCells*2) / 2 self.textGrid.Load(self.textGrid.numRows,self.textGrid.numCols,widthText,heightText,xText,yText, display_surf) self.cellsAcrossGrid.Load(self.cellsAcrossGrid.numRows,self.cellsAcrossGrid.numCols,widthCells,heightCells,xCells,yCells, display_surf) self.cellsDownGrid.Load(self.cellsDownGrid.numRows,self.cellsDownGrid.numCols,widthCells,heightCells,xCells,yCells+heightCells, display_surf) elif orientation == 'BA': xCells = (Constants.ACTIVITY_WIDTH - widthText - widthCells - 10) / 2 yCells = (Constants.ACTIVITY_HEIGHT - heightCells*2) / 2 xCells = max(xCells,xActual) xText = xCells + widthCells + 10 yText = (Constants.ACTIVITY_HEIGHT - heightText) / 2 self.textGrid.Load(self.textGrid.numRows,self.textGrid.numCols,widthText,heightText,xText,yText, display_surf) self.cellsAcrossGrid.Load(self.cellsAcrossGrid.numRows,self.cellsAcrossGrid.numCols,widthCells,heightCells,xCells,yCells, display_surf) self.cellsDownGrid.Load(self.cellsDownGrid.numRows,self.cellsDownGrid.numCols,widthCells,heightCells,xCells,yCells+heightCells, display_surf) elif orientation == 'AUB': xText = (Constants.ACTIVITY_WIDTH - widthText) / 2 yText = (Constants.ACTIVITY_HEIGHT - heightText - heightCells - 10) / 2 xCells = (Constants.ACTIVITY_WIDTH - widthCells*2) / 2 yCells = yText + heightText + 10 self.textGrid.Load(self.textGrid.numRows,self.textGrid.numCols,widthText,heightText,xText,yText, display_surf) self.cellsAcrossGrid.Load(self.cellsAcrossGrid.numRows,self.cellsAcrossGrid.numCols,widthCells,heightCells,xCells,yCells, display_surf) self.cellsDownGrid.Load(self.cellsDownGrid.numRows,self.cellsDownGrid.numCols,widthCells,heightCells,xCells+widthCells,yCells, display_surf) elif orientation == 'BUA': xCells = (Constants.ACTIVITY_WIDTH - widthCells*2) / 2 yCells = (Constants.ACTIVITY_HEIGHT - heightText - heightCells - 10) / 2 xText = (Constants.ACTIVITY_WIDTH - widthText) / 2 yText = yCells + heightCells + 10 self.textGrid.Load(self.textGrid.numRows,self.textGrid.numCols,widthText,heightText,xText,yText, display_surf) self.cellsAcrossGrid.Load(self.cellsAcrossGrid.numRows,self.cellsAcrossGrid.numCols,widthCells,heightCells,xCells,yCells, display_surf) self.cellsDownGrid.Load(self.cellsDownGrid.numRows,self.cellsDownGrid.numCols,widthCells,heightCells,xCells+widthCells,yCells, display_surf) self.textHasBorder = self.textGrid.hasBorder self.text = [] '''Agafem la solucio del textGrid per comprovar al final la correctesa''' rows = self.xmlText.getElementsByTagName('row') for row in rows: row = row.firstChild.data self.cols = len(row) for i in range(0,self.cols): self.text.append(row[i]) item = 0 for i in range(0,len(self.text)): if i % self.cols == self.cols: item = 0 if self.text[i] == '*': self.textGrid.Cells[i].contentCell.letter = self.text[i] self.textGrid.Cells[i].contentCell.img.fill(Constants.colorBlack) else: self.acrossItems = item item += 1 self.textGrid.Cells[i].contentCell.letter = ' ' #imprimeixo en blanc per pintar la cell, sino dixa color per defecte self.printLetterinCell(self.textGrid.Cells[i],self.xmlText) self.blancs += 1 #per saber quants espais hi ha per omplir '''Carreguem les definicions''' self.loadClues() '''Per defecte la direccio es across i seleccionem la primera cell del text disponible''' self.direction = 'across' self.pressedDirectionCell = self.cellsAcrossGrid.Cells[0] self.pressedDirectionCell.actualColorCell = Constants.colorPressedCell self.pressedTextCell = self.textGrid.Cells[0] self.pressedTextCell.contentCell.id = -1 #per que seleccioni la primera possible self.acrossCell = self.cellsAcrossGrid.Cells[1] self.downCell = self.cellsDownGrid.Cells[1] self.selectNextCell() self.showClues() def OnEvent(self,PointOfMouse): '''Si click al textGrid''' for cell in self.textGrid.Cells: if cell.isOverCell(PointOfMouse[0],PointOfMouse[1]): print cell.contentCell.id if cell.contentCell.letter != '*': '''Reset actual pressedCell''' self.pressedTextCell.contentCell.border = self.textHasBorder self.pressedTextCell.actualColorCell = Constants.colorCell '''Select new pressedCell''' self.pressedTextCell = cell self.pressedTextCell.actualColorCell = Constants.colorPressedCell '''Show definitions''' self.showClues() '''Si click al acrossGrid''' if self.cellsAcrossGrid.Cells[0].isOverCell(PointOfMouse[0],PointOfMouse[1]): self.pressedDirectionCell.actualColorCell = Constants.colorCell self.direction = 'across' self.pressedDirectionCell = self.cellsAcrossGrid.Cells[0] self.pressedDirectionCell.actualColorCell = Constants.colorPressedCell '''Si click al downGrid''' if self.cellsDownGrid.Cells[0].isOverCell(PointOfMouse[0],PointOfMouse[1]): self.pressedDirectionCell.actualColorCell = Constants.colorCell self.direction = 'down' self.pressedDirectionCell = self.cellsDownGrid.Cells[0] self.pressedDirectionCell.actualColorCell = Constants.colorPressedCell def OnKeyEvent(self,keyPressed): ''' keyPressed is the identifier of the key, like a letter,"delete","backspace"...''' print 'Entry in KeyEvent!!' if keyPressed == 'delete' or keyPressed == 'backspace': self.pressedTextCell.contentCell.letter = ' ' self.blancs += 1 else: #Si es una lletra l'escrivim a la cell selccionada if self.pressedTextCell.contentCell.letter == ' ': '''Si no hi havia cap lletra restem en 1 els blancs, si ja hi havia alguna lletra, es sobreescriu i no resta blancs''' self.blancs -= 1 self.pressedTextCell.contentCell.letter = keyPressed print 'letra grabada: ',self.pressedTextCell.contentCell.letter '''Imprimeix el canvi i selecciona la seguent''' self.printLetterinCell(self.pressedTextCell,self.xmlText) self.selectNextCell() def selectNextCell(self): actualId = self.pressedTextCell.contentCell.id ok = False if self.direction == 'across': while(not ok): actualId += 1 if actualId >= len(self.text): actualId = 0 if self.text[actualId] != '*': ok = True else: #direction == down while(not ok): if actualId == len(self.text) - 1: '''estava a la ultima cell->tornem al principi''' actualId = 0 elif actualId+self.cols >= len(self.text): '''estava al final d'una columna -> saltem a la seguent''' saltCol = len(self.text) - self.cols actualId = actualId - saltCol + 1 else: actualId += self.cols if self.text[actualId] != '*': ok = True '''Reset actual pressedCell''' if self.pressedTextCell.contentCell.id == -1: self.pressedTextCell.contentCell.id = 0 self.pressedTextCell.contentCell.border = self.textHasBorder self.pressedTextCell.actualColorCell = Constants.colorCell '''Select new pressedCell''' self.pressedTextCell = self.textGrid.Cells[actualId] self.pressedTextCell.actualColorCell = Constants.colorPressedCell self.showClues() def loadClues(self): self.acrossClues = [] self.downClues = [] for i in range(self.textGrid.numRows): self.acrossClues.append('') for i in range(self.cols): self.downClues.append('') across = self.xmlCellsAcross.getElementsByTagName('cell') for cell in across: try: definition = cell.getElementsByTagName('p')[0].firstChild.data except: definition = None if definition != None: id = int(cell.getAttribute('id')) #item = int(cell.getAttribute('item')) self.acrossClues[id] = definition down = self.xmlCellsDown.getElementsByTagName('cell') for cell in down: try: definition = cell.getElementsByTagName('p')[0].firstChild.data except: definition = None if definition != None: id = int(cell.getAttribute('id')) #item = int(cell.getAttribute('item')) self.downClues[id] = definition for i in range(0,12): try: print i print self.acrossClues[i] except: pass for i in range(0,12): try: print i print self.downClues[i] except: pass def showClues(self): fila = int(self.pressedTextCell.contentCell.id // self.cols) columna = self.pressedTextCell.contentCell.id % self.cols self.acrossCell.contentCell.letter = self.acrossClues[fila] self.downCell.contentCell.letter = self.downClues[columna] self.printLetterinCell(self.acrossCell,self.xmlCellsAcross) self.printLetterinCell(self.downCell,self.xmlCellsAcross) def OnRender(self,display_surf): display_surf.blit(self.containerBg,(0,0)) '''repintamos el grid...''' self.textGrid.OnRender(display_surf) self.cellsAcrossGrid.OnRender(display_surf) self.cellsDownGrid.OnRender(display_surf) if self.pressedTextCell != None: self.pressedTextCell.contentCell.border = True self.pressedTextCell.OnRenderPressedCell(display_surf) def isGameFinished(self): return self.completeAndCorrect() def completeAndCorrect(self): if self.blancs > 0: return False else: for i in range(0,len(self.text)): if self.textGrid.Cells[i].contentCell.letter != self.text[i]: return False '''Si no retorna abans, significa que esta tot correcte i acaba l'activitat''' return True
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 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)
class PanelsExplore(Activity): Grid1 = None PressedCell = None def Load(self, display_surf): self.setBgColor(display_surf) '''Loading xml values''' xmlGrid1 = self.xmlActivity.getElementsByTagName('cells')[0] xmlGrid2 = self.xmlActivity.getElementsByTagName('cells')[1] self.Grid1 = Grid(xmlGrid1) self.Grid2 = Grid(xmlGrid2) self.Grid3 = Grid(xmlGrid2) self.styleCell = StyleCell(xmlGrid1) self.styleCell2 = StyleCell(xmlGrid2) orientation = self.xmlActivity.getElementsByTagName( 'layout')[0].getAttribute('position') ''' Calculate Real size''' height = self.Grid1.cellHeight * self.Grid1.numRows width = self.Grid1.cellWidth * self.Grid1.numCols 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 ''' Calculamos porcentaje...''' '''Maximize size''' coef = self.calculateCoef(width, height) height = self.Grid1.cellHeight * self.Grid1.numRows * coef width = self.Grid1.cellWidth * self.Grid1.numCols * coef '''Loading constants for the activity''' xActual = Constants.MARGIN_TOP yActual = Constants.MARGIN_LEFT 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''' newHeight = self.Grid2.cellHeight * coef yGrid1 = (Constants.ACTIVITY_HEIGHT - height - newHeight) / 2 yGrid1 = max(yGrid1, yActual) self.Grid2.Load(1, 1, width, newHeight, xGrid1, yGrid1 + height, display_surf) '''Grid auxiliar...''' self.Grid3.Load(self.Grid1.numRows, self.Grid1.numCols, width * self.Grid1.numCols, newHeight, 0, 0, display_surf) else: '''Sumamos el width al tamano total''' newWidth = self.Grid2.cellWidth * coef xGrid1 = (Constants.ACTIVITY_WIDTH - width - newWidth) / 2 xGrid1 = max(xGrid1, xActual) self.Grid2.Load(1, 1, newWidth, height, xGrid1 + width, yGrid1, display_surf) '''Grid auxiliar...''' self.Grid3.Load(self.Grid1.numRows, self.Grid1.numCols, newWidth, height * self.Grid1.numRows, 0, 0, display_surf) self.Grid1.Load(self.Grid1.numRows, self.Grid1.numCols, width, height, xGrid1, yGrid1, display_surf) self.Grid2.Cells[0].contentCell.img2 = self.Grid2.Cells[ 0].contentCell.img.copy() cells = xmlGrid1.getElementsByTagName('cell') i = 0 for cell in cells: self.printxmlCellinCell(self.Grid1.Cells[i], cell, self.styleCell) idCell = cell.getAttribute('id') if len(idCell) > 0: self.Grid1.Cells[i].contentCell.id = int(idCell) else: self.Grid1.Cells[i].contentCell.id = -1 i = i + 1 cells = xmlGrid2.getElementsByTagName('cell') i = 0 copia = self.Grid2.Cells[0].contentCell.img.copy() for i in range(len(self.Grid1.Cells)): copia = self.Grid2.Cells[0].contentCell.img.copy() self.Grid3.Cells[i].contentCell.img = copia self.printxmlCellinCell(self.Grid3.Cells[i], cells[i], self.styleCell2) 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]): print cell.contentCell.id if cell.contentCell.id != -1: self.Grid2.Cells[0].contentCell.img = self.Grid3.Cells[ cell.contentCell.id].contentCell.img else: self.Grid2.Cells[0].contentCell.img = self.Grid2.Cells[ 0].contentCell.img2 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) def isGameFinished(self): return False