Mercurial > libervia-backend
diff frontends/wix/card_game.py @ 144:80661755ea8d
Primitivus: Tarot card game implementation
- quick frontend: card_game added
- wix: card_game splitted with quick frontend
- tools: new game library
- primitivus: new card_game widget (not finished yet)
- primitivus: SàT XML UI management: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 26 Jul 2010 19:43:44 +0800 |
parents | 8c80d4dec7a8 |
children | 63d20bda5754 |
line wrap: on
line diff
--- a/frontends/wix/card_game.py Thu Jul 22 22:47:29 2010 +0800 +++ b/frontends/wix/card_game.py Mon Jul 26 19:43:44 2010 +0800 @@ -27,6 +27,7 @@ from logging import debug, info, error from tools.jid import JID from tools.games import TarotCard +from quick_frontend.quick_card_game import QuickCardGame from xmlui import XMLUI CARD_WIDTH = 74 @@ -34,16 +35,16 @@ MIN_WIDTH = 950 #Minimum size of the panel MIN_HEIGHT = 500 -class wxCard(TarotCard): +class WxCard(TarotCard): """This class is used to represent a card, graphically and logically""" def __init__(self, file): """@param file: path of the PNG file""" self.bitmap = wx.Image(file).ConvertToBitmap() root_name = os.path.splitext(os.path.basename(file))[0] - self.suit,self.value=root_name.split('_') - TarotCard.__init__(self, (self.suit, self.value)) - print "Carte:",self.suit, self.value #, self.bout + suit,value = root_name.split('_') + TarotCard.__init__(self, (suit, value)) + print "Carte:",suit, value #, self.bout def draw(self, dc, x, y): """Draw the card on the device context @@ -53,32 +54,15 @@ dc.DrawBitmap(self.bitmap, x, y, True) -class CardPanel(wx.Panel): +class CardPanel(QuickCardGame,wx.Panel): """This class is used to display the cards""" def __init__(self, parent, referee, players, player_nick): + QuickCardGame.__init__(self, parent, referee, players, player_nick) wx.Panel.__init__(self, parent) - self.parent = parent - self.referee = referee - self.players = players - self.played = {} - for player in players: - self.played[player] = None - self.player_nick = player_nick - self.bottom_nick = self.player_nick - idx = self.players.index(self.player_nick) - idx = (idx + 1) % len(self.players) - self.right_nick = self.players[idx] - idx = (idx + 1) % len(self.players) - self.top_nick = self.players[idx] - idx = (idx + 1) % len(self.players) - self.left_nick = self.players[idx] - self.bottom_nick = player_nick self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) - self.load_cards("/home/goffi/dev/divers/images/cards/") + self.loadCards("/home/goffi/dev/divers/images/cards/") self.mouse_over_card = None #contain the card to highlight - self.selected = [] #Card choosed by the player (e.g. during ecart) - self.hand_size = 13 #number of cards in a hand self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards self.hand = [] self.to_show = [] @@ -90,33 +74,18 @@ self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile) - def load_cards(self, dir): + def loadCards(self, dir): """Load all the cards in memory @param dir: directory where the PNG files are""" - self.cards={} - self.deck=[] - self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names - self.cards["pique"]={} #spade - self.cards["coeur"]={} #heart - self.cards["carreau"]={} #diamond - self.cards["trefle"]={} #club + QuickCardGame.loadCards(self) for file in glob.glob(dir+'/*_*.png'): - card = wxCard(file) + card = WxCard(file) self.cards[card.suit, card.value]=card self.deck.append(card) - """for value in map(str,range(1,22))+['excuse']: - self.idx_cards.append(self.cards["atout",value]) - for suit in ["pique", "coeur", "carreau", "trefle"]: - for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: - self.idx_cards.append(self.cards[suit, value])""" #XXX: no need to sort the cards ! def newGame(self, hand): """Start a new game, with given hand""" - assert (len(self.hand) == 0) - for suit, value in hand: - self.hand.append(self.cards[suit, value]) - self.hand.sort() - self.state = "init" + QuickCardGame.newGame(self, hand) self._recalc_ori() self.Refresh() @@ -125,30 +94,14 @@ @param data: form result""" debug (_("Contrat choosed")) contrat = data[0][1] - self.parent.host.bridge.tarotGameContratChoosed(self.player_nick, self.referee, contrat or 'Passe', self.parent.host.profile) + QuickCardGame.contratSelected(self, contrat) def chooseContrat(self, xml_data): - """Called when the player as to select hist contrat + """Called when the player as to select his contrat @param xml_data: SàT xml representation of the form""" misc = {'callback': self.contratSelected} form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) - def showCards(self, game_stage, cards, data): - """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" - self.to_show = [] - for suit, value in cards: - self.to_show.append(self.cards[suit, value]) - if game_stage == "chien" and data['attaquant'] == self.player_nick: - self.state = "wait_for_ecart" - else: - self.state = "chien" - - def MyTurn(self): - """Called when we have to play :)""" - if self.state == "chien": - self.to_show = [] - self.state = "play" - def showScores(self, xml_data, winners, loosers): """Called when the player as to select hist contrat @param xml_data: SàT xml representation of the form""" @@ -156,15 +109,7 @@ def cardsPlayed(self, player, cards): """A card has been played by player""" - if self.to_show: - self.to_show = [] - pl_cards = [] - if self.played[player] != None: #gof: à supprimer - for pl in self.played: - self.played[pl] = None - for suit, value in cards: - pl_cards.append(self.cards[suit, value]) - self.played[player] = pl_cards[0] + QuickCardGame.cardsPlayed(self, player, cards) self.Refresh() def invalidCards(self, phase, played_cards, invalid_cards): @@ -172,25 +117,12 @@ @param phase: phase of the game @param played_cards: all the cards played @param invalid_cards: cards which are invalid""" + QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) - if phase == "play": - self.state = "play" - elif phase == "ecart": - self.state = "ecart" - else: - error ('INTERNAL ERROR: unmanaged game phase') - - for suit, value in played_cards: - self.hand.append(self.cards[suit, value]) - self._recalc_ori() self.Refresh() - self.hand.sort() wx.MessageDialog(self, _("Cards played are invalid !"), _("Error"), wx.OK | wx.ICON_ERROR).ShowModal() - - - def _is_on_hand(self, pos_x, pos_y): """Return True if the coordinate are on the hand cards""" if pos_x > self.orig_x and pos_y > self.orig_y \