Mercurial > libervia-web
diff browser_side/card_game.py @ 36:1d406077b49b
Tarot Game: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 17 May 2011 01:33:12 +0200 |
parents | |
children | b306aa090438 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/browser_side/card_game.py Tue May 17 01:33:12 2011 +0200 @@ -0,0 +1,110 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" +Libervia: a Salut à Toi frontend +Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +""" + +import pyjd # this is dummy in pyjs +from pyjamas.ui.AbsolutePanel import AbsolutePanel +from pyjamas.ui.Image import Image + +from pyjamas.dnd import makeDraggable +from pyjamas.ui.DragWidget import DragWidget, DragContainer +from jid import JID +from tools import html_sanitize +from datetime import datetime +from time import time +from games import TarotCard + + + +CARD_WIDTH = 74 +CARD_HEIGHT = 136 +MIN_WIDTH = 950 #Minimum size of the panel +MIN_HEIGHT = 500 + + +class CardWidget(TarotCard, Image): + """This class is used to represent a card, graphically and logically""" + + def __init__(self, file): + """@param file: path of the PNG file""" + Image.__init__(self,file) + root_name = file[file.rfind("/")+1:-4] + 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 + @param dc: device context + @param x: abscissa + @param y: ordinate""" + pass + #dc.DrawBitmap(self.bitmap, x, y, True) + +class CardPanel(AbsolutePanel): + + def __init__(self, parent, referee, players, player_nick): + self._parent = parent + self._autoplay = None #XXX: use 0 to activate fake play, None else + 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.selected = [] #Card choosed by the player (e.g. during ecart) + self.hand_size = 13 #number of cards in a hand + self.hand = [] + self.to_show = [] + self.state = None + AbsolutePanel.__init__(self) + self.setSize("%spx" % MIN_WIDTH, "%spx" % MIN_HEIGHT) + self.setStyleName("cardPanel") + self.loadCards() + self.mouse_over_card = None #contain the card to highlight + self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards + + def _getTarotCardsPathsCb(self, paths): + for file in paths: + card = CardWidget(file) + self.cards[(card.suit, card.value)]=card + self.deck.append(card) + + 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 + self._parent.host.bridge.call('getTarotCardsPaths', self._getTarotCardsPathsCb) +