class Menu(pygame.sprite.Sprite): def __init__(self, width, height): # We create the window self.width = width self.height = height FULLSCREEN = 0 self.dimension = (self.width, self.height) self.screen = pygame.display.set_mode(self.dimension, FULLSCREEN, 32) pygame.display.set_caption("TuxleTriad") elemText = ["Play", "Options", "Rules", "About", "Quit Game"] self.menu = [] for elem in elemText: self.menu.append(Button(elem, "Dearest.ttf", white)) posx = 400 posy = 400 - (60 * len(elemText)) for elem in self.menu: elem.rect.center = ((posx, posy)) posy += 100 self.bkgrnd, self.bkgrndRect = loadImage("background.jpg") self.bkgrndRect = self.bkgrnd.get_rect() # The Clock of the game, to manage the frame-rate self.clock = pygame.time.Clock() self.fps = 60 # We start the Sound object, playing music and sounds. self.sound = Sound() # Needed to keep track of the game if we do a pause during the game. self.app = None self.main() def main(self): pygame.event.clear() while 1: self.screen.blit(self.bkgrnd, self.bkgrndRect) for i in range(len(self.menu)): self.screen.blit(self.menu[i].surface, self.menu[i].rect) for event in pygame.event.get(): if event.type == MOUSEBUTTONUP: self.clicked() elif event.type == QUIT: self.quitGame() pygame.display.flip() self.clock.tick(self.fps) def play(self): """User clicked on "Play" """ if self.app != None: self.app.main() else: Application(800, 600, self.screen, self.sound, self) def options(self): pygame.event.clear() texts = ["Audio", "Sounds", "Music", "Back"] length = len(texts) textsPos = [(320, 100), (100, 200), (100, 300), (500, 450)] elements = [] for i in range(length): elements.append(Button(texts[i], "Dearest.ttf", white)) elements[i].rect.topleft = textsPos[i] bar1, bar1Rect = loadImage("barSound.jpg") bar2, bar2Rect = loadImage("barSound.jpg") bar1Rect.topleft = (300, 220) bar2Rect.topleft = (300, 320) bars = [bar1Rect, bar2Rect] # X coordinates, relative to the bar's, of beginning and ending # of each volume cursor. MIN_VOLUME = 15 MAX_VOLUME = 240 # X absolute coordinates of the volume cursor. MIN = bars[0].x + MIN_VOLUME MAX = bars[0].x + MAX_VOLUME cursor1, cursor1Rect = loadImage("cursorSound.png") cursor2, cursor2Rect = loadImage("cursorSound.png") cursor1Rect.topleft = \ (bar1Rect.x + 225 * self.sound.soundVolume, bar1Rect.y - 23) cursor2Rect.topleft = \ (bar2Rect.x + 225 * self.sound.musicVolume, bar2Rect.y - 23) cursors = [cursor1Rect, cursor2Rect] while 1: self.screen.blit(self.bkgrnd, self.bkgrndRect) self.screen.blit(bar1, bar1Rect) self.screen.blit(bar2, bar2Rect) self.screen.blit(cursor1, cursors[0]) self.screen.blit(cursor2, cursors[1]) for i in range(length): self.screen.blit(elements[i].surface, elements[i].rect) for event in pygame.event.get(): if event.type == QUIT: self.quitGame() elif event.type == MOUSEBUTTONDOWN: mousex, mousey = pygame.mouse.get_pos() for i in range(len(cursors)): if cursors[i].collidepoint((mousex, mousey)): while pygame.event.poll().type != MOUSEBUTTONUP: mousex, mousey = pygame.mouse.get_pos() if MIN <= mousex <= MAX: cursors[i].centerx = mousex elif mousex > bars[i].x + MAX_VOLUME: cursors[i].centerx = bars[i].x + MAX_VOLUME else: cursors[i].centerx = bars[i].x + MIN_VOLUME volume = cursors[i].centerx - MIN if volume != 0: volume = (volume / 2.25) / 100.0 assert (0.0 <= volume <= 1.0) if i == 0: self.sound.soundVolume = volume self.sound.playPutCard() elif i == 1: self.sound.musicVolume = volume self.sound.update() self.screen.blit(self.bkgrnd, self.bkgrndRect) self.screen.blit(bar1, bar1Rect) self.screen.blit(bar2, bar2Rect) self.screen.blit(cursor1, cursors[0]) self.screen.blit(cursor2, cursors[1]) for j in range(4): self.screen.blit(elements[j].surface,\ elements[j].rect) pygame.display.flip() if elements[3].rect.collidepoint((mousex, mousey)): self.main() pygame.display.update() def quitGame(self): setConfig("config.txt", self.sound.volume) pygame.quit() sys.exit() def clicked(self): for button in self.menu: if button.rect.collidepoint(pygame.mouse.get_pos()): if button.text == "Play": self.play() elif button.text == "Options": self.options() elif button.text == "Rules": print "Rules!" elif button.text == "About": print "About !" elif button.text == "Quit Game": self.quitGame() def __repr__(self): return "<Menu>"
class Application(): """Main class of the game, manage the window""" def __init__(self, width, height, screen=None, soundInstance=None, boss=None): # We create the window self.width = width self.height = height if screen == None: self.screen = pygame.display.set_mode((self.width, self.height)) else: self.screen = screen if soundInstance == None: self.Sound = Sound() else: self.Sound = soundInstance print self.Sound.soundVolume self.background, self.backgroundRect = loadImage("background.jpg") # We keep the Menu instance if we are running TuxleTriad from Menu.py if boss != None: self.boss = boss self.boss.app = self else: self.boss = None # The Clock of the game, to manage the frame rate self.clock = pygame.time.Clock() self.fps = 60 Cards1 = [] Cards2 = [] # We generate two and draw 5 cards from each # to have an entire Hand of Card list1 = [i for i in range(len(allCards))] random.shuffle(list1) list2 = [i for i in range(len(allCards))] random.shuffle(list2) for i in range(5): number = list1[0] Cards1.append(Card(number, 1)) list1.remove(number) for i in range(5): number = list2[0] Cards2.append(Card(number, -1)) list2.remove(number) # We create the Hands to manage the lists of cards self.player1Hand = Hand(Cards1, 1) self.player2Hand = Hand(Cards2, -1) # We create the Score self.scorePlayer1 = Score("5", 1, self.width, self.height) self.scorePlayer2 = Score("5", -1, self.width, self.height) # With this variable, we cannot do anything until the animation #played is finished. self.animation = 0 # If we have to play the animation in different directions self.sensAnimation = 0 self.player = 1 #self.Sound = Sound() self.position = None self.CARD = None # We create the field of the game, 3x3. sizeCard = self.player1Hand.cards[0].image.get_size() self.field = Field(self.width, self.height, sizeCard) self.alphaAnimation = 255 self.emptyCase = 9 # Manage the winner congratulations font self.winner = None self.winnerFont = pygame.font.Font(None, 60) # Manage the display of the name of the card selected self.cardFont = pygame.font.Font(None, 40) self.cardFontSurf = None # Manage the background for the name of the card selected self.backCard, self.backCardRect = loadImage("name.png") self.main() def update(self): self.screen.blit(self.background, self.background.get_rect()) for card in self.player1Hand.cards: self.screen.blit(card.image, card.rect) if card == self.CARD: name = self.CARD.name self.cardFontSurf = self.cardFont.render(name, True, white) self.cardFontRect = self.cardFontSurf.get_rect() self.cardFontRect.midbottom = self.backgroundRect.midbottom self.cardFontRect.y -= 10 self.backCardRect.center = self.cardFontRect.center self.backCardRect.centery -= 0 for card in self.player2Hand.cards: if card == self.CARD: name = self.CARD.name self.cardFontSurf = self.cardFont.render(name, True, white) self.cardFontRect = self.cardFontSurf.get_rect() self.cardFontRect.midbottom = self.backgroundRect.midbottom self.cardFontRect.y -= 10 self.screen.blit(card.image, card.rect) self.scorePlayer1.update() self.scorePlayer2.update() self.screen.blit(self.scorePlayer1.surface, self.scorePlayer1.rect) self.screen.blit(self.scorePlayer2.surface, self.scorePlayer2.rect) if self.winner != None: self.screen.blit(self.winnerSurface, self.winnerRect) if self.cardFontSurf != None: self.screen.blit(self.backCard, self.backCardRect) self.screen.blit(self.cardFontSurf, self.cardFontRect) self.cardFontSurf = None pygame.display.flip() self.clock.tick(self.fps) def main(self): self.cardsOwner() self.update() while 1: if self.animation == 1: # We continue the animation self.putCard() self.update() else: # We over the animation and now the next player have to play. if self.sensAnimation == 1: self.player = self.player * -1 self.sensAnimation = 0 for event in pygame.event.get(): if event.type == MOUSEBUTTONUP and self.animation == 0: if self.winner == None: self.playCard() elif event.type == QUIT: audio = [self.Sound.soundVolume, self.Sound.musicVolume] setConfig("config.txt", audio) pygame.quit() sys.exit() else: # We get the status of all key on keyboard. # Then we select the one at place 27: Escape. # We can do this only if we ran the game # with Menu.py and not directly from main.py if pygame.key.get_pressed()[27] and self.boss != None: self.boss.main() pygame.display.flip() self.clock.tick(self.fps) def playCard(self): """When player has to play a card""" coords = pygame.mouse.get_pos() if self.player == 1: # Player 1 for card in self.player1Hand.cards: if card.rect.collidepoint(coords) and card.inHand: self.CARD = card self.selectedCard() break if not self.CARD == None: # If we clicked on a card. # We wait for the event 'MOUSEBUTTONUP', so first we clean the #queue event. Then we deactivate the MOUSEMOTION event, because #it causes the card to be put randomly on the field! #We wait an event, for example a touch on the keyboard #pressed, or MOUSEBUTTONUP, but not only, and we reactivate #MOUSEMOTION, we could need it later. self.deactivate() if not self.animation: pygame.event.wait() while pygame.event.peek(MOUSEBUTTONUP) and not self.animation: pass self.reactivate() # If the player clicked on the field this time, we test #each cases of the Field. if self.field.rect.collidepoint(pygame.mouse.get_pos()): for case in self.field.fieldRects: if case.collidepoint(pygame.mouse.get_pos()): self.position = case.topleft if not self.caseFilled(): self.animation = 1 self.putCard() self.cardsOwner() return else: self.deselectedCard() self.CARD = None if self.player == -1: # Player -1...I mean Player 2 for card in self.player2Hand.cards: if (card.rect.collidepoint(coords) and card.inHand): self.CARD = card self.selectedCard() break if not self.CARD == None: # Same as before self.deactivate() if not self.animation: pygame.event.wait() while pygame.event.peek(MOUSEBUTTONUP) and not self.animation: pass self.reactivate() if self.field.rect.collidepoint(pygame.mouse.get_pos()): for case in self.field.fieldRects: if case.collidepoint(pygame.mouse.get_pos()): self.position = case.topleft if not self.caseFilled(): self.animation = 1 self.putCard() self.cardsOwner() return else: self.deselectedCard() self.CARD = None def putCard(self): """Animation of a card put on the field""" if self.CARD.inHand == 1: # We check if self..CARD is in the player's Hand self.Sound.playPutCard() # We drop the card off the Hand if self.CARD.inHand == 1: self.CARD.inHand = 0 # Depending of the direction of the animation, we make the card # being invisible or visible again. if self.sensAnimation == 0: self.alphaAnimation -= 25 self.CARD.image.set_alpha(self.alphaAnimation) elif self.sensAnimation == 1: self.alphaAnimation += 25 self.CARD.image.set_alpha(self.alphaAnimation) # We change the position of the card and the animation's direction if self.CARD.image.get_alpha() == 5: self.CARD.rect.topleft = self.position self.sensAnimation = 1 if self.CARD.image.get_alpha() == 255 and self.sensAnimation == 1: # We have put the card on the field and the animation is over #Now we have to look if that card captured some of the ennemy's self.animation = 0 adjacentCards = self.getAdjacent() capturedCard = adjacent(self.CARD, adjacentCards) self.changeOwner(capturedCard) self.emptyCase -= 1 self.CARD = None if self.emptyCase == 0: self.winAnimation() def selectedCard(self): """Player has selected a card But not yet a place on the field""" for i in range(5): self.CARD.rect.centerx += 4 * self.player self.update() def deselectedCard(self): """Finally, the player wants an other card""" for i in range(5): self.CARD.rect.centerx -= 4 * self.player self.CARD = None self.update() def deactivate(self): """Deactivate MOUSEMOTION event and clean the queue""" pygame.event.set_blocked(MOUSEMOTION) pygame.event.clear() def reactivate(self): """Get back MOUSEMOTION""" pygame.event.set_allowed(MOUSEMOTION) def caseFilled(self): """Say if there is already a card in the case""" for card in self.player1Hand.cards: if card.rect.topleft == self.position: return 1 for card in self.player2Hand.cards: if card.rect.topleft == self.position: return 1 return 0 def cardsOwner(self): """Which cards is owned by who?""" cardPlayer = 0 for card in self.player1Hand.cards: if card.owner == self.player: cardPlayer += 1 for cards in self.player2Hand.cards: if cards.owner == self.player: cardPlayer += 1 if self.player == 1: self.scorePlayer1.updateScore(cardPlayer) self.scorePlayer2.updateScore(10 - cardPlayer) elif self.player == -1: self.scorePlayer1.updateScore(10 - cardPlayer) self.scorePlayer2.updateScore(cardPlayer) def getAdjacent(self): """Get all the adjacent cards of the first one put""" posx, posy = self.CARD.rect.topleft adjacentCards = [None, None, None, None] if self.player == 1: for card in self.player2Hand.cards: if card.inHand == 0: if card.rect.collidepoint((posx, posy - 144)): # We first look at the card on the top adjacentCards[0] = card if card.rect.collidepoint((posx + 113, posy)): # We look at the card on the right adjacentCards[1] = card if card.rect.collidepoint((posx, posy + 144)): # We look at the card on the bottom adjacentCards[2] = card if card.rect.collidepoint((posx - 113, posy)): # We look at the card on the left adjacentCards[3] = card elif self.player == -1: for card in self.player1Hand.cards: if card.inHand == 0: if card.rect.collidepoint((posx, posy - 144)): # We first look at the card on the top adjacentCards[0] = card if card.rect.collidepoint((posx + 113, posy)): # We look at the card on the right adjacentCards[1] = card if card.rect.collidepoint((posx, posy + 144)): # We look at the card on the bottom adjacentCards[2] = card if card.rect.collidepoint((posx - 113, posy)): # We look at the card on the left adjacentCards[3] = card return adjacentCards def changeOwner(self, cards): for card in cards: if card.owner == 1: self.player1Hand.cards.remove(card) self.player2Hand.cards.append(card) if card.owner == -1: self.player2Hand.cards.remove(card) self.player1Hand.cards.append(card) self.capturedAnimation(card) self.cardsOwner() def capturedAnimation(self, card): # We want the sound of the card put played before doing anything more self.update() while (pygame.mixer.get_busy()): pass # self.Sound.capturedCard.play() width = card.rect.width # we expect 113. If not please change format. height = card.image.get_rect().height # Here we expect 139. topleft = list(card.rect.topleft) print height, "\t", topleft step = 16 while(width != 1): width -= step topleft[0] += step / 2 getCard(card) card.image = pygame.transform.scale(card.image, (width, height)) card.rect = card.image.get_rect() card.rect.topleft = topleft self.update() card.owner *= -1 card.changeOwner(card.rect.center) while (width != 113): width += step topleft[0] -= step / 2 getCard(card) card.image = pygame.transform.scale(card.image, (width, height)) card.rect = card.image.get_rect() card.rect.topleft = topleft self.update() def winAnimation(self): if self.scorePlayer1.score > self.scorePlayer2.score: self.winner = u"Blue win!" elif self.scorePlayer2.score > self.scorePlayer1.score: self.winner = u"Red win!" else: self.winner = u"Equality!" self.winnerSurface = self.winnerFont.render(self.winner, True, white) self.winnerRect = self.winnerSurface.get_rect() self.winnerRect.midtop = self.backgroundRect.midtop self.winnerRect.y += 10
class Application(): """Main class of the game, manage the window""" def __init__(self, width, height, screen=None, soundInstance=None, boss=None): # We create the window self.width = width self.height = height if screen == None: self.screen = pygame.display.set_mode((self.width, self.height)) else: self.screen = screen if soundInstance == None: self.Sound = Sound() else: self.Sound = soundInstance self.background, self.backgroundRect = loadImage("background.jpg") # We keep the Menu instance if we are running TuxleTriad from Menu.py if boss != None: self.boss = boss self.boss.app = self self.FONT = self.boss.FONT else: self.boss = None self.FONT = "Dearest.ttf" # The Clock of the game, to manage the frame rate self.clock = pygame.time.Clock() self.fps = 60 # Creation of two players self.player1 = Player(1) self.player2 = Player(-1) self.players = {1 : self.player1, -1 : self.player2} self.player1Hand = self.player1.hand self.player2Hand = self.player2.hand # We create the Score self.scorePlayer1 = Score("5", 1, self.width, self.height) self.scorePlayer2 = Score("5", -1, self.width, self.height) # With this variable, we cannot do anything until the animation # played is finished. self.animation = 0 # If we have to play the animation in different directions self.sensAnimation = 0 self.player = 1 self.position = None self.CARD = None self.infoCARD = None # We create the field of the game, 3x3. sizeCard = self.player1Hand.cards[0].image.get_size() self.field = Field(self.width, self.height, sizeCard, self) self.emptySquare = 9 self.alphaAnimation = 255 # Manage the winner congratulations font self.winner = Text("", self.FONT, white, 60) # Manage the display of the name of the card selected self.cardName = None # Do we show the name of the card selected? self.selectedCardName = 1 def update(self): """Updates all the sprites on the window""" self.screen.blit(self.background, self.background.get_rect()) self.screen.blit(self.field.surface, self.field.rect) for card in self.player1Hand.cards: self.screen.blit(card.image, card.rect) if card == self.CARD: self.CARD.borderRect.topleft = self.CARD.rect.topleft self.screen.blit(self.CARD.border, self.CARD.borderRect) for card in self.player2Hand.cards: self.screen.blit(card.image, card.rect) if card == self.CARD: self.CARD.borderRect.topleft = self.CARD.rect.topleft self.screen.blit(self.CARD.border, self.CARD.borderRect) self.scorePlayer1.update() self.scorePlayer2.update() self.screen.blit(self.scorePlayer1.surface, self.scorePlayer1.rect) self.screen.blit(self.scorePlayer2.surface, self.scorePlayer2.rect) if self.winner.text != "": self.winner.changeText() self.screen.blit(self.winner.surface, self.winner.rect) if self.selectedCardName != 0: self.showName(1) self.showName(-1) if self.cardName != None: self.screen.blit(self.backCard, self.backCardRect) self.screen.blit(self.cardName.surface, self.cardName.rect) self.cardName = None if self.infoCARD == None: # If we aren't showing the about popup. Because About need to blit one # more thing before doing the following commands. pygame.display.flip() self.clock.tick(self.fps) def main(self): """Main part handling the game""" self.cardsOwner() self.update() while 1: if self.animation == 1: # We continue the animation self.putCard() self.update() else: # We over the animation and now the next player have to play. if self.sensAnimation == 1: self.player = self.player * -1 self.sensAnimation = 0 for event in pygame.event.get(): if event.type == MOUSEBUTTONUP and self.animation == 0: if event.button == 3 and self.winner.text == "": if self.getCard(0): self.showAbout() if self.winner.text == "" and event.button == 1: self.infoCARD = None self.players[self.player].playCard(self) elif event.type == QUIT: audio = [self.Sound.soundVolume, self.Sound.musicVolume] setConfig(audio) self.field.saveState() pygame.quit() sys.exit() else: # We get the status of all key on keyboard. # Then we select the one at place 27: Escape. # We can do this only if we ran the game # with Menu.py and not directly from main.py if pygame.key.get_pressed()[27] and self.boss != None: self.boss.main() pygame.display.flip() self.clock.tick(self.fps) def putCard(self): """Animation of a card put on the field""" if self.CARD.inHand == 1: # We check if self..CARD is in the player's Hand self.Sound.playPutCard() # We drop the card off the Hand if self.CARD.inHand == 1: self.CARD.inHand = 0 # Depending of the direction of the animation, we make the card # being invisible or visible again. if self.sensAnimation == 0: self.alphaAnimation -= 25 + (self.fps / 30.0 * 5) if self.alphaAnimation < 0: self.alphaAnimation = 0 self.CARD.image.set_alpha(self.alphaAnimation) self.CARD.rect.centerx += 2 * self.player elif self.sensAnimation == 1: self.alphaAnimation += 25 + (self.fps / 30.0 * 5) if self.alphaAnimation > 255: self.alphaAnimation = 255 self.CARD.image.set_alpha(self.alphaAnimation) # We change the position of the card and the animation's direction if self.CARD.image.get_alpha() <= 25: self.CARD.rect = self.Square self.sensAnimation = 1 if self.CARD.image.get_alpha() == 255 and self.sensAnimation == 1: # We have put the card on the field and the animation is over. # We compare the elements to give potential malus/bonus. # And we have to look if that card captured some of the # ennemy's. self.animation = 0 squareElement = self.field.elementName[self.numberSquare] if squareElement != None: self.Sound.playElement(squareElement) if self.CARD.elementName == squareElement \ and squareElement != None: self.CARD.addModifier(1) else: if squareElement != None: self.CARD.addModifier(-1) adjacentCards = self.getAdjacent() capturedCard = adjacent(self.CARD, adjacentCards) self.changeOwner(capturedCard) self.emptySquare -= 1 self.CARD = None if self.emptySquare == 0: self.winAnimation() def selectedCard(self): """Player has selected a card But not yet a place on the field""" for i in [1, 2, 3, 4, 5]: self.CARD.rect.centerx += 4 * self.player self.update() def deselectedCard(self): """Finally, the player wants an other card""" for i in [1, 2, 3, 4, 5]: self.CARD.rect.centerx -= 4 * self.player self.CARD = None self.update() def squareFilled(self): """Say if there is already a card in the square""" for card in self.player1Hand.cards: if card.rect == self.Square: return 1 for card in self.player2Hand.cards: if card.rect == self.Square: return 1 return 0 def cardsOwner(self): """Which cards is owned by who?""" cardPlayer = 0 cardPlayer += self.player1Hand.cardsOwner() self.scorePlayer1.updateScore(cardPlayer) self.scorePlayer2.updateScore(10 - cardPlayer) def getAdjacent(self): """Get all the adjacent cards of the first one put""" posx, posy = self.CARD.rect.topleft adjacentCards = [None, None, None, None] if self.player == 1: for card in self.player2Hand.cards: if card.inHand == 0: if card.rect.collidepoint((posx, posy - 144)): # We first look at the card on the top adjacentCards[0] = card if card.rect.collidepoint((posx + 113, posy)): # We look at the card on the right adjacentCards[1] = card if card.rect.collidepoint((posx, posy + 144)): # We look at the card on the bottom adjacentCards[2] = card if card.rect.collidepoint((posx - 113, posy)): # We look at the card on the left adjacentCards[3] = card elif self.player == -1: for card in self.player1Hand.cards: if card.inHand == 0: if card.rect.collidepoint((posx, posy - 144)): # We first look at the card on the top adjacentCards[0] = card if card.rect.collidepoint((posx + 113, posy)): # We look at the card on the right adjacentCards[1] = card if card.rect.collidepoint((posx, posy + 144)): # We look at the card on the bottom adjacentCards[2] = card if card.rect.collidepoint((posx - 113, posy)): # We look at the card on the left adjacentCards[3] = card return adjacentCards def changeOwner(self, cards): for card in cards: if card.owner == 1: self.player1Hand.cards.remove(card) self.player2Hand.cards.append(card) if card.owner == -1: self.player2Hand.cards.remove(card) self.player1Hand.cards.append(card) self.capturedAnimation(card) self.cardsOwner() def capturedAnimation(self, card): """Shows a little animation when capturing card""" # We want the sound of the card put played before doing anything more self.update() while (pygame.mixer.get_busy()): pass # self.Sound.capturedCard.play() width = card.rect.width # we expect 113. If not please change format. height = card.image.get_rect().height # Here we expect 139. topleft = list(card.rect.topleft) step = 30 - (self.fps / 30 * 3) while(width != 10): width -= step if width < 10: width = 10 topleft[0] += step / 2 getCard(card) card.image = pygame.transform.scale(card.image, (width, height)) card.rect = card.image.get_rect() card.rect.topleft = topleft self.update() card.owner *= -1 card.changeOwner() while (width != 113): width += step if width > 113: width = 113 topleft[0] -= step / 2 getCard(card) card.image = pygame.transform.scale(card.image, (width, height)) card.rect = card.image.get_rect() card.rect.topleft = topleft self.update() # If card has a bonus or malus, we have to re-draw it on the card if card.modifierValue != 0: card.image.blit(card.modifierBack.surface, card.modifierBack.rect) card.image.blit(card.modifier.surface, card.modifier.rect) def winAnimation(self): """Show who won the game""" if self.scorePlayer1.score > self.scorePlayer2.score: self.winner.text = _("Blue win!") self.winner.rect.topleft = self.backgroundRect.midtop self.winner.rect.x -= 20 self.winner.color = blue elif self.scorePlayer2.score > self.scorePlayer1.score: self.winner.text = _("Red win!") self.winner.rect.topright = self.backgroundRect.midtop self.winner.rect.x += 20 self.winner.color = red else: self.winner.text = _("Equality!") self.winner.rect.topleft = self.backgroundRect.midtop self.winner.color = white self.winner.changeColor() self.winner.rect.y += 10 def getCard(self, player): """Return the card at pygame.mouse.get_pos() coordinates """ coords = pygame.mouse.get_pos() if player == 1: card = self.player1Hand.getCard(coords) if card >= 0: if self.CARD != None: self.deselectedCard() self.CARD = self.player1Hand.cards[card] elif player == -1: card = self.player2Hand.getCard(coords) if card >= 0: if self.CARD != None: self.deselectedCard() self.CARD = self.player2Hand.cards[card] elif player == 0: #If we get a right-click, then we want to print the About #popup even if it is an ennemy's card card = self.player1Hand.getCard(coords) if card != None: self.infoCARD = self.player1Hand.cards[card].About else: card = self.player2Hand.getCard(coords) if card != None: self.infoCARD = self.player2Hand.cards[card].About if card != None: return 1 return 0 def showAbout(self): """Show some info on the card if we do a right-click on it""" width = 0 quit = 0 maxWidth = 450 COLORRED = (200,0,0,125) COLORBLUE = (0,0,200,125) event = None if self.infoCARD.boss.owner == 1: COLOR = COLORBLUE elif self.infoCARD.boss.owner == -1: COLOR = COLORRED background = pygame.Surface((width, 140), SRCALPHA) rect = background.get_rect() background.fill(COLOR) if self.infoCARD.boss.owner == 1: rect.topleft = self.infoCARD.boss.rect.topright elif self.infoCARD.boss.owner == -1: rect.topright = self.infoCARD.boss.rect.topleft while 1: self.update() self.screen.blit(background,rect) pygame.display.flip() self.clock.tick(self.fps) if width < maxWidth and quit == 0: width += 40 - (self.fps / 30.0 * 5) if width > maxWidth: width = maxWidth background = pygame.Surface((width, 140), SRCALPHA) rect = background.get_rect() background.fill(COLOR) if self.infoCARD.boss.owner == 1: rect.topleft = self.infoCARD.boss.rect.topright elif self.infoCARD.boss.owner == -1: rect.topright = self.infoCARD.boss.rect.topleft if quit == 1: width -= 40 - (self.fps / 30.0 * 5) if width < 0: width = 0 background = pygame.Surface((width, 140), SRCALPHA) rect = background.get_rect() background.fill(COLOR) if self.infoCARD.boss.owner == 1: rect.topleft = self.infoCARD.boss.rect.topright elif self.infoCARD.boss.owner == -1: rect.topright = self.infoCARD.boss.rect.topleft if width == 0: if quit == 1: self.update() return quit = 1 if width == maxWidth and quit == 0: background.fill(COLOR) background.blit(self.infoCARD.surface, self.infoCARD.rect) self.update() self.screen.blit(background,rect) pygame.display.flip() self.clock.tick(self.fps) event = pygame.event.wait() if width == 0: self.infoCARD = None self.update() return 0 if event and event.type == MOUSEBUTTONUP: quit = 1 elif event and event.type == QUIT: audio = [self.Sound.soundVolume, self.Sound.musicVolume] setConfig(audio) pygame.quit() sys.exit() return 0 def showName(self, player): """Show the name of the card selected at the bottom of the window""" self.backCard, self.backCardRect = loadImage("name.png") if player == 1: for card in self.player1Hand.cards: if card == self.CARD: name = self.CARD.name self.cardName = Text(name, self.FONT, white, 40) elif player == -1: for card in self.player2Hand.cards: if card == self.CARD: name = self.CARD.name self.cardName = Text(name, self.FONT, white, 40) if self.cardName != None: self.cardName.rect.midbottom = self.backgroundRect.midbottom self.cardName.rect.y -= 10 self.backCardRect.center = self.cardName.rect.center
class Menu(pygame.sprite.Sprite): def __init__(self, width, height): self.FONT = "Playball.ttf" # We create the window self.width = width self.height = height fullscreen = pygame.NOFRAME self.dimension = (self.width, self.height) self.screen = pygame.display.set_mode(self.dimension, fullscreen) pygame.display.set_caption("TuxleTriad") self._load_translation() self.bkgrnd, self.bkgrndRect = loadImage("background.jpg") self.bkgrndRect = self.bkgrnd.get_rect() # The Clock of the game, to manage the frame-rate self.clock = pygame.time.Clock() self.fps = 30 # We start the Sound object, playing music and sounds. self.sound = Sound() # Needed to keep track of the game if we do a pause during the game. self.app = None self.main() def main(self): elemText = [_("Play"), _("Options"), _("Rules"), _("About"), _("Quit Game")] self.menu = [] for elem in elemText: self.menu.append(Text(elem, self.FONT, white, 40)) posx = 400 posy = 400 - (60 * len(elemText)) for elem in self.menu: elem.rect.center = ((posx, posy)) posy += 100 pygame.event.clear() self.updateMenu() while 1: pygame.display.flip() deactivate() event = pygame.event.wait() if event.type == MOUSEBUTTONUP: self.clicked() elif event.type == QUIT: self.quitGame() self.clock.tick(self.fps) def updateMenu(self): self.screen.blit(self.bkgrnd, self.bkgrndRect) for i in range(len(self.menu)): self.screen.blit(self.menu[i].surface, self.menu[i].rect) self.clock.tick(self.fps) def quitGame(self): setConfig(self.sound.volume) pygame.quit() sys.exit() def oldMenu(self): while(1): for button in self.menu: button.rect.centerx -= 100 - self.fps if (button.rect.centerx <= - 500): return; self.updateMenu() pygame.display.flip() def clicked(self): for button in self.menu: if button.rect.collidepoint(pygame.mouse.get_pos()): self.sound.clicMenu.play() if button.text == _(u"Quit Game"): self.quitGame() self.oldMenu() if button.text == _(u"Play"): self.play() elif button.text == _(u"Options"): self.options() elif button.text == _(u"Rules"): self.rules() elif button.text == _(u"About"): self.about() self.main() def play(self): """User clicked on "Play" """ if self.app != None: texts = [_("Continue"),_("Adventure"), _("Solo"), _("Hot Seat"), _("Back")] else: texts = [_("Adventure"), _("Solo"), _("Hot Seat"), _("Back")] length = len(texts) if self.app != None: textPos = [(250, 100), (250,200), (250, 300), (250,400), (550, 500)] else: textPos = [(250, 100), (250,200), (250, 300), (550, 500)] self.menu = [] for i in range(length): self.menu.append(Text(texts[i], self.FONT, white, 45)) self.menu[i].rect.topleft = textPos[i] self.updateMenu() pygame.display.flip() self.clock.tick(self.fps) while 1: event = pygame.event.wait() if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONUP: coordinates = pygame.mouse.get_pos() for i in range(length): if self.menu[i].rect.collidepoint(coordinates): self.sound.clicMenu.play() self.oldMenu() if self.menu[i].text == _("Adventure"): return elif self.menu[i].text == _("Solo"): return elif self.menu[i].text == _("Hot Seat"): self.hotSeat() elif self.menu[i].text == _("Back"): return elif self.menu[i].text == _("Continue"): self.app.main() def options(self): texts = [_("Audio"), _("Sounds"), _("Music"), _("Back")] length = len(texts) textsPos = [(320, 100), (100, 200), (100, 300), (550, 500)] self.menu = [] for i in range(length): self.menu.append(Text(texts[i], self.FONT, white, 50)) self.menu[i].rect.topleft = textsPos[i] bar1, bar1Rect = loadImage("barSound.jpg") bar2, bar2Rect = loadImage("barSound.jpg") bar1Rect.topleft = (300, 220) bar2Rect.topleft = (300, 320) bars = [bar1Rect, bar2Rect] # X coordinates, relative to the bar's, of beginning and ending # of each volume cursor. MIN_VOLUME = 15 MAX_VOLUME = 240 # X absolute coordinates of the volume cursor. MIN = bars[0].x + MIN_VOLUME MAX = bars[0].x + MAX_VOLUME cursor1, cursor1Rect = loadImage("cursorSound.png") cursor2, cursor2Rect = loadImage("cursorSound.png") cursor1Rect.topleft = \ (bar1Rect.x + 225 * self.sound.soundVolume, bar1Rect.y - 23) cursor2Rect.topleft = \ (bar2Rect.x + 225 * self.sound.musicVolume, bar2Rect.y - 23) cursors = [cursor1Rect, cursor2Rect] self.screen.blit(self.bkgrnd, self.bkgrndRect) self.screen.blit(bar1, bar1Rect) self.screen.blit(bar2, bar2Rect) self.screen.blit(cursor1, cursors[0]) self.screen.blit(cursor2, cursors[1]) for i in range(length): self.screen.blit(self.menu[i].surface, self.menu[i].rect) pygame.display.update() move = 0 while 1: event = pygame.event.wait() mousex, mousey = pygame.mouse.get_pos() if event.type == QUIT: self.quitGame() elif event.type == MOUSEBUTTONDOWN: move = 1 reactivate() elif event.type == MOUSEBUTTONUP: move = 0 deactivate() for i in range(len(bars)): if move == 1 and bars[i].collidepoint((mousex, mousey)): if MIN <= mousex <= MAX: cursors[i].centerx = mousex elif mousex > bars[i].x + MAX_VOLUME: cursors[i].centerx = bars[i].x + MAX_VOLUME else: cursors[i].centerx = bars[i].x + MIN_VOLUME volume = cursors[i].centerx - MIN if volume != 0: volume = (volume / 2.25) / 100.0 assert (0.0 <= volume <= 1.0) if i == 0: self.sound.soundVolume = volume self.sound.playPutCard() self.sound.update() elif i == 1: self.sound.musicVolume = volume self.sound.update() self.screen.blit(self.bkgrnd, self.bkgrndRect) self.screen.blit(bar1, bar1Rect) self.screen.blit(bar2, bar2Rect) self.screen.blit(cursor1, cursors[0]) self.screen.blit(cursor2, cursors[1]) for j in range(4): self.screen.blit(self.menu[j].surface,\ self.menu[j].rect) pygame.display.update() self.clock.tick(self.fps) if move and self.menu[3].rect.collidepoint((mousex, mousey)): del bar1, bar2, bars, cursor1, cursor2, cursors self.oldMenu() self.sound.clicMenu.play() return def about(self): page = 1 allPage = [] pageList = [] index = 0 for number in range(len(allCards)): pageList.append(Card(number, 1)) index += 1 if index == 3 or number == (len(allCards) or len(allCards)-1): allPage.append(pageList) del pageList pageList = [] index = 0 maxPage = len(allPage) txtPage = str(page) + "/" + str(maxPage) navigation = [_("Back"), _("Next"), _("Quit"), "Programming:", "Kevin \"Ilphrin\" Pellet", "Graphics:", "Yunero Kisapsodos", txtPage] navigationPos = [(80,550), (650,550), (660,40), (630, 100), (640, 130), (630, 200), (640, 230), (350,550)] self.menu = [] for i in range(len(navigation)): if 2 < i < 7: size = 12 font = "rimouski sb.ttf" else: font = self.FONT size = 30 self.menu.append(Text(navigation[i], font, white, size)) self.menu[i].rect.topleft = navigationPos[i] cardPos = [(50,50), (50,200), (50, 350)] self.screen.blit(self.bkgrnd, self.bkgrndRect) for element in self.menu: self.screen.blit(element.surface,element.rect) for elem in range(len(allPage[page-1])): card = allPage[page-1][elem] card.rect.topleft = cardPos[elem] card.About.rect.topleft = card.rect.topright for elem in allPage[page-1]: self.screen.blit(elem.image, elem.rect) self.screen.blit(elem.About.surface, elem.About.rect) while 1: self.clock.tick(self.fps) pygame.display.flip() event = pygame.event.wait() if event.type == MOUSEBUTTONUP: coords = pygame.mouse.get_pos() for button in self.menu: if button.rect.collidepoint(coords): if button.text == _("Back"): if page > 1: page -= 1 self.sound.putcard.play() if button.text == _("Next"): if page < maxPage: page += 1 self.sound.putcard.play() if button.text == _("Quit"): self.oldMenu() return txtPage = str(page) + "/" + str(maxPage) self.menu[7] = Text(txtPage, self.FONT, white, 30) self.menu[7].rect.topleft = navigationPos[7] self.screen.blit(self.bkgrnd, self.bkgrndRect) for element in self.menu: self.screen.blit(element.surface,element.rect) for elem in range(len(allPage[page-1])): card = allPage[page-1][elem] card.rect.topleft = cardPos[elem] card.About.rect.topleft = card.rect.topright for elem in allPage[page-1]: self.screen.blit(elem.image, elem.rect) self.screen.blit(elem.About.surface, elem.About.rect) if event.type == QUIT: self.quitGame() def rules(self): tutorialButton = Button(_(u"Tutorial"), self.FONT, white) howtoButton = Button(_(u"How To"), self.FONT, white) backButton = Button(_(u"Back"), self.FONT, white) tutorialButton.rect.topleft = (250, 100) howtoButton.rect.topleft = (250, 200) backButton.rect.topleft = (550, 500) self.menu = [] self.menu.append(tutorialButton) self.menu.append(howtoButton) self.menu.append(backButton) self.updateMenu() while (1): self.clock.tick(self.fps) pygame.display.flip() event = pygame.event.wait() if event.type == MOUSEBUTTONUP: coords = pygame.mouse.get_pos() for i in range(len(self.menu)): if self.menu[i].rect.collidepoint(coords): self.oldMenu() if self.menu[i].text == _(u"Tutorial"): self.main() elif self.menu[i].text == _(u"How To"): self.HowTo() return elif self.menu[i].text == _(u"Back"): self.main() elif event.type == QUIT: self.quitGame() def HowTo(self): backButton = Button(_("Back"), self.FONT, white) prevButton = Button(_("Prev"), self.FONT, white) nextButton = Button(_("Next"), self.FONT, white) page = 1 maxPage = 2 pageList = [] for i in range(maxPage): pageList.append(pygame.image.load(getHowTo(i))) pageRect = pageList[i - 1].get_rect() pageRect.topleft = (-20, 0) backButton.rect.topleft = (600, 40) prevButton.rect.topleft = (80, 550) nextButton.rect.topleft = (660, 550) self.menu = [] self.menu.append(backButton) self.menu.append(prevButton) self.menu.append(nextButton) self.updateMenu() self.screen.blit(pageList[page - 1], pageRect) while (1): self.clock.tick(self.fps) pygame.display.flip() event = pygame.event.wait() if event.type == MOUSEBUTTONUP: coords = pygame.mouse.get_pos() if backButton.rect.collidepoint(coords): self.oldMenu() return elif prevButton.rect.collidepoint(coords) and page > 1: page -= 1 elif nextButton.rect.collidepoint(coords) and page < maxPage: page += 1 self.updateMenu() self.screen.blit(pageList[page - 1], pageRect) elif event.type == QUIT: self.quitGame() def _load_translation(self): base_path = os.getcwd() directory = os.path.join(base_path, 'translations') print "Loading translations at: ", directory params = { 'domain': 'tuxle-triad', 'fallback': True } if os.path.isdir(directory): params.update({'localedir': directory}) translation = gettext.translation(**params) translation.install("ngettext") def solo(self): """1vsIA mode""" print "Solo!" def adventure(self): """Adventure mode against IA""" print "Adventure!" def hotSeat(self): """1vs1 mode""" if self.app != None: del self.app Application(800, 600, self.screen, self.sound, self).main() else: Application(800, 600, self.screen, self.sound, self).main()