Mercurial > libervia-backend
comparison frontends/wix/card_game.py @ 83:1ac759ea28c3
wix: tarot game preparation step
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 06 May 2010 20:31:37 +0930 |
parents | 104a815bb23f |
children | 4b5f2d55b6ac |
comparison
equal
deleted
inserted
replaced
82:21f83796a293 | 83:1ac759ea28c3 |
---|---|
28 from logging import debug, info, error | 28 from logging import debug, info, error |
29 from tools.jid import JID | 29 from tools.jid import JID |
30 from quick_frontend.quick_chat import QuickChat | 30 from quick_frontend.quick_chat import QuickChat |
31 from contact_list import ContactList | 31 from contact_list import ContactList |
32 | 32 |
33 CARD_WIDTH = 74 | |
34 CARD_HEIGHT = 136 | |
35 | |
33 class Card(): | 36 class Card(): |
34 """This class is used to represent a card, graphically and logically""" | 37 """This class is used to represent a card, graphically and logically""" |
35 | 38 |
36 def __init__(self, file): | 39 def __init__(self, file): |
37 """@param file: path of the PNG file""" | 40 """@param file: path of the PNG file""" |
39 root_name = os.path.splitext(os.path.basename(file))[0] | 42 root_name = os.path.splitext(os.path.basename(file))[0] |
40 self.family,self.value=root_name.split('_') | 43 self.family,self.value=root_name.split('_') |
41 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False | 44 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False |
42 | 45 |
43 print "Carte:",self.family, self.value, self.bout | 46 print "Carte:",self.family, self.value, self.bout |
47 | |
44 | 48 |
45 def draw(self, dc, x, y): | 49 def draw(self, dc, x, y): |
46 """Draw the card on the device context | 50 """Draw the card on the device context |
47 @param dc: device context | 51 @param dc: device context |
48 @param x: abscissa | 52 @param x: abscissa |
53 class CardPanel(wx.Panel): | 57 class CardPanel(wx.Panel): |
54 """This class is used to display the cards""" | 58 """This class is used to display the cards""" |
55 | 59 |
56 def __init__(self, parent): | 60 def __init__(self, parent): |
57 wx.Panel.__init__(self, parent) | 61 wx.Panel.__init__(self, parent) |
62 self.load_cards("/home/goffi/dev/divers/images/cards/") | |
63 self.selected = None #contain the card to highlight | |
64 self.hand_size = 13 #number of cards in a hand | |
65 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards | |
66 self.hand = random.sample(self.deck, self.hand_size) | |
58 self.SetBackgroundColour(wx.GREEN) | 67 self.SetBackgroundColour(wx.GREEN) |
68 self.Bind(wx.EVT_SIZE, self.onResize) | |
59 self.Bind(wx.EVT_PAINT, self.onPaint) | 69 self.Bind(wx.EVT_PAINT, self.onPaint) |
60 self.load_cards("/home/goffi/dev/divers/images/cards/") | 70 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
71 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
61 | 72 |
62 def load_cards(self, dir): | 73 def load_cards(self, dir): |
63 """Load all the cards in memory | 74 """Load all the cards in memory |
64 @param dir: directory where the PNG files are""" | 75 @param dir: directory where the PNG files are""" |
65 self.cards={} | 76 self.cards={} |
77 self.idx_cards.append(self.cards["atout",value]) | 88 self.idx_cards.append(self.cards["atout",value]) |
78 for family in ["pique", "coeur", "carreau", "trefle"]: | 89 for family in ["pique", "coeur", "carreau", "trefle"]: |
79 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: | 90 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
80 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards ! | 91 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards ! |
81 | 92 |
93 def _is_on_hand(self, pos_x, pos_y): | |
94 """Return True if the coordinate are on the hand cards""" | |
95 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
96 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
97 and pos_y < self.end_y: | |
98 return True | |
99 return False | |
100 | |
101 def onResize(self, event): | |
102 self._recalc_ori() | |
103 | |
104 def _recalc_ori(self): | |
105 """Recalculate origines, must be call when size change""" | |
106 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards | |
107 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
108 self.end_y = self.orig_y + CARD_HEIGHT | |
109 | |
82 def onPaint(self, event): | 110 def onPaint(self, event): |
83 print "toto", event | |
84 dc = wx.PaintDC(self) | 111 dc = wx.PaintDC(self) |
85 x=0 | 112 x=self.orig_x |
86 for card in random.sample(self.deck,13): | 113 for card in self.hand: |
87 card.draw(dc,x,0) | 114 card.draw(dc,x,self.orig_y - 30 if card == self.selected else self.orig_y) |
88 x+=37 | 115 x+=self.visible_size |
116 | |
117 def onMouseMove(self, event): | |
118 pos_x,pos_y = event.GetPosition() | |
119 if self._is_on_hand(pos_x, pos_y): | |
120 try: | |
121 self.selected = self.hand[(pos_x-self.orig_x)/self.visible_size] | |
122 except IndexError: | |
123 self.selected = self.hand[-1] | |
124 self.Refresh() | |
125 else: | |
126 self.selected = None | |
127 self.Refresh() | |
128 | |
129 def onMouseClick(self, event): | |
130 print "mouse click:",event.GetPosition() | |
131 pos_x,pos_y = event.GetPosition() | |
132 if self._is_on_hand(pos_x, pos_y): | |
133 idx = (pos_x-self.orig_x)/self.visible_size | |
134 if idx == len(self.hand): | |
135 idx-=1 | |
136 if self.hand[idx] == self.selected: | |
137 del self.hand[idx] | |
138 self._recalc_ori() | |
139 self.Refresh() | |
89 | 140 |
90 class CardGame(wx.Frame, QuickChat): | 141 class CardGame(wx.Frame, QuickChat): |
91 """The chat Window for one to one conversations""" | 142 """The window used to play all kind of card games""" |
92 | 143 |
93 def __init__(self, host): | 144 def __init__(self, host): |
94 wx.Frame.__init__(self, None, pos=(0,0), size=(800,550)) | 145 wx.Frame.__init__(self, None, pos=(0,0), size=(950,500)) |
95 | 146 |
96 self.host = host | 147 self.host = host |
97 | 148 |
98 self.sizer = wx.BoxSizer(wx.VERTICAL) | 149 self.sizer = wx.BoxSizer(wx.VERTICAL) |
99 self.panel = CardPanel(self) | 150 self.panel = CardPanel(self) |