diff frontends/wix/card_game.py @ 87:66d784082930

Tarot game - Tarot plugin: game session start, first draft - wix: Tarot: names of players are now printed - wix: Tarot: game session start first draft - wix : Tarot: card can be compared and sorted
author Goffi <goffi@goffi.org>
date Tue, 11 May 2010 23:58:32 +0930
parents 4b5f2d55b6ac
children 4020931569b8
line wrap: on
line diff
--- a/frontends/wix/card_game.py	Tue May 11 13:06:05 2010 +0930
+++ b/frontends/wix/card_game.py	Tue May 11 23:58:32 2010 +0930
@@ -23,7 +23,6 @@
 
 import wx
 import os.path, glob
-import random
 import pdb
 from logging import debug, info, error
 from tools.jid  import JID
@@ -35,6 +34,9 @@
 MIN_WIDTH = 950 #Minimum size of the panel
 MIN_HEIGHT = 500
 
+families_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have swith the usual order 'trefle' and 'carreau' because card are more easy to see if couleur change (black, red, black, red)
+values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
+
 class Card():
     """This class is used to represent a card, graphically and logically"""
 
@@ -47,6 +49,28 @@
 
         print "Carte:",self.family, self.value, self.bout
 
+    def __cmp__(self, other):
+        if other == None:
+            return 1
+        if self.family != other.family:
+            idx1 = families_order.index(self.family)
+            idx2 = families_order.index(other.family)
+            return idx1.__cmp__(idx2)
+        if self.family == 'atout':
+            if self.value == other.value == 'excuse':
+                return 0
+            if self.value == 'excuse':
+                return -1
+            if other.value == 'excuse':
+                return 1
+            return int(self.value).__cmp__(int(other.value))
+        #at this point we have the same family which is not 'atout'
+        idx1 = values_order.index(self.value)
+        idx2 = values_order.index(other.value)
+        return idx1.__cmp__(idx2)
+
+    def __str__(self):
+        return "[%s,%s]" % (self.family, self.value)
 
     def draw(self, dc, x, y):
         """Draw the card on the device context
@@ -59,14 +83,25 @@
 class CardPanel(wx.Panel):
     """This class is used to display the cards"""
 
-    def __init__(self, parent):
+    def __init__(self, parent, players, user):
         wx.Panel.__init__(self, parent)
+        self.players = players
+        self.user = user
+        self.bottom_nick = self.user
+        idx = self.players.index(self.user)
+        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.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT))
         self.load_cards("/home/goffi/dev/divers/images/cards/")
         self.selected = None #contain the card to highlight
         self.hand_size = 13 #number of cards in a hand
         self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards
-        self.hand = random.sample(self.deck, self.hand_size)
+        self.hand = []
+        self.my_turn = False
         self.SetBackgroundColour(wx.GREEN)
         self.Bind(wx.EVT_SIZE, self.onResize)
         self.Bind(wx.EVT_PAINT, self.onPaint)
@@ -93,6 +128,14 @@
             for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
                 self.idx_cards.append(self.cards[family, 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 family, value in hand:
+            self.hand.append(self.cards[family, value])
+        self.hand.sort()
+        self.my_turn = True
+
     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 \
@@ -112,9 +155,24 @@
 
     def onPaint(self, event):
         dc = wx.PaintDC(self)
+        
+        #We print the names to know who play where TODO: print avatars when available
+        max_x, max_y = self.GetSize()
+        border = 10 #border between nick and end of panel
+        right_y = left_y = 200
+        right_width, right_height = dc.GetTextExtent(self.right_nick)
+        right_x = max_x - right_width - border
+        left_x = border
+        top_width, top_height = dc.GetTextExtent(self.top_nick)
+        top_x = (max_x - top_width) / 2
+        top_y = border
+        dc.DrawText(self.right_nick, right_x, right_y)
+        dc.DrawText(self.top_nick, top_x, top_y)
+        dc.DrawText(self.left_nick, left_x, left_y)
+
         x=self.orig_x
         for card in self.hand:
-            card.draw(dc,x,self.orig_y - 30 if card == self.selected else self.orig_y)
+            card.draw(dc,x,self.orig_y - 30 if self.my_turn and card == self.selected else self.orig_y)
             x+=self.visible_size
 
     def onMouseMove(self, event):