changeset 83:1ac759ea28c3

wix: tarot game preparation step
author Goffi <goffi@goffi.org>
date Thu, 06 May 2010 20:31:37 +0930
parents 21f83796a293
children 7471ffcda33b
files frontends/wix/card_game.py
diffstat 1 files changed, 59 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/wix/card_game.py	Thu May 06 20:30:08 2010 +0930
+++ b/frontends/wix/card_game.py	Thu May 06 20:31:37 2010 +0930
@@ -30,6 +30,9 @@
 from quick_frontend.quick_chat import QuickChat
 from contact_list import ContactList
 
+CARD_WIDTH = 74
+CARD_HEIGHT = 136
+
 class Card():
     """This class is used to represent a card, graphically and logically"""
 
@@ -42,6 +45,7 @@
 
         print "Carte:",self.family, self.value, self.bout
 
+
     def draw(self, dc, x, y):
         """Draw the card on the device context
         @param dc: device context
@@ -55,9 +59,16 @@
 
     def __init__(self, parent):
         wx.Panel.__init__(self, parent)
+        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.SetBackgroundColour(wx.GREEN)
+        self.Bind(wx.EVT_SIZE, self.onResize)
         self.Bind(wx.EVT_PAINT, self.onPaint)
-        self.load_cards("/home/goffi/dev/divers/images/cards/")
+        self.Bind(wx.EVT_MOTION, self.onMouseMove)
+        self.Bind(wx.EVT_LEFT_UP, self.onMouseClick)
 
     def load_cards(self, dir):
         """Load all the cards in memory
@@ -79,19 +90,59 @@
             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 _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 \
+           and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \
+           and pos_y < self.end_y:
+           return True
+        return False
+
+    def onResize(self, event):
+        self._recalc_ori()
+
+    def _recalc_ori(self):
+        """Recalculate origines, must be call when size change"""
+        self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards
+        self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20
+        self.end_y = self.orig_y + CARD_HEIGHT
+
     def onPaint(self, event):
-        print "toto", event
         dc = wx.PaintDC(self)
-        x=0
-        for card in random.sample(self.deck,13):
-            card.draw(dc,x,0)
-            x+=37
+        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)
+            x+=self.visible_size
+
+    def onMouseMove(self, event):
+        pos_x,pos_y = event.GetPosition()
+        if self._is_on_hand(pos_x, pos_y):
+           try:
+               self.selected = self.hand[(pos_x-self.orig_x)/self.visible_size]
+           except IndexError:
+               self.selected = self.hand[-1]
+           self.Refresh()
+        else:
+            self.selected = None
+            self.Refresh()
+
+    def onMouseClick(self, event):
+        print "mouse click:",event.GetPosition()
+        pos_x,pos_y = event.GetPosition()
+        if self._is_on_hand(pos_x, pos_y):
+           idx = (pos_x-self.orig_x)/self.visible_size
+           if idx == len(self.hand):
+               idx-=1
+           if self.hand[idx] == self.selected:
+               del self.hand[idx]
+               self._recalc_ori()
+               self.Refresh()
 
 class CardGame(wx.Frame, QuickChat):
-    """The chat Window for one to one conversations"""
+    """The window used to play all kind of card games"""
 
     def __init__(self, host):
-        wx.Frame.__init__(self, None, pos=(0,0), size=(800,550))
+        wx.Frame.__init__(self, None, pos=(0,0), size=(950,500))
 
         self.host = host