Mercurial > libervia-backend
annotate frontends/wix/card_game.py @ 90:4020931569b8
Tarot Game: session initialization
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 23 May 2010 16:39:05 +0930 |
parents | 66d784082930 |
children | 39c672544593 |
rev | line source |
---|---|
81 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 wix: a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 | |
23 | |
24 import wx | |
25 import os.path, glob | |
26 import pdb | |
27 from logging import debug, info, error | |
28 from tools.jid import JID | |
29 from quick_frontend.quick_chat import QuickChat | |
30 from contact_list import ContactList | |
31 | |
83 | 32 CARD_WIDTH = 74 |
33 CARD_HEIGHT = 136 | |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
34 MIN_WIDTH = 950 #Minimum size of the panel |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
35 MIN_HEIGHT = 500 |
83 | 36 |
87 | 37 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) |
38 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"] | |
39 | |
81 | 40 class Card(): |
41 """This class is used to represent a card, graphically and logically""" | |
42 | |
43 def __init__(self, file): | |
44 """@param file: path of the PNG file""" | |
45 self.bitmap = wx.Image(file).ConvertToBitmap() | |
46 root_name = os.path.splitext(os.path.basename(file))[0] | |
47 self.family,self.value=root_name.split('_') | |
48 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False | |
49 | |
50 print "Carte:",self.family, self.value, self.bout | |
51 | |
87 | 52 def __cmp__(self, other): |
53 if other == None: | |
54 return 1 | |
55 if self.family != other.family: | |
56 idx1 = families_order.index(self.family) | |
57 idx2 = families_order.index(other.family) | |
58 return idx1.__cmp__(idx2) | |
59 if self.family == 'atout': | |
60 if self.value == other.value == 'excuse': | |
61 return 0 | |
62 if self.value == 'excuse': | |
63 return -1 | |
64 if other.value == 'excuse': | |
65 return 1 | |
66 return int(self.value).__cmp__(int(other.value)) | |
67 #at this point we have the same family which is not 'atout' | |
68 idx1 = values_order.index(self.value) | |
69 idx2 = values_order.index(other.value) | |
70 return idx1.__cmp__(idx2) | |
71 | |
72 def __str__(self): | |
73 return "[%s,%s]" % (self.family, self.value) | |
83 | 74 |
81 | 75 def draw(self, dc, x, y): |
76 """Draw the card on the device context | |
77 @param dc: device context | |
78 @param x: abscissa | |
79 @param y: ordinate""" | |
80 dc.DrawBitmap(self.bitmap, x, y, True) | |
81 | |
82 | |
83 class CardPanel(wx.Panel): | |
84 """This class is used to display the cards""" | |
85 | |
90 | 86 def __init__(self, parent, referee, players, user): |
81 | 87 wx.Panel.__init__(self, parent) |
90 | 88 self.parent = parent |
89 self.referee = referee | |
87 | 90 self.players = players |
91 self.user = user | |
92 self.bottom_nick = self.user | |
93 idx = self.players.index(self.user) | |
94 idx = (idx + 1) % len(self.players) | |
95 self.right_nick = self.players[idx] | |
96 idx = (idx + 1) % len(self.players) | |
97 self.top_nick = self.players[idx] | |
98 idx = (idx + 1) % len(self.players) | |
99 self.left_nick = self.players[idx] | |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
100 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) |
83 | 101 self.load_cards("/home/goffi/dev/divers/images/cards/") |
102 self.selected = None #contain the card to highlight | |
103 self.hand_size = 13 #number of cards in a hand | |
104 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards | |
87 | 105 self.hand = [] |
106 self.my_turn = False | |
81 | 107 self.SetBackgroundColour(wx.GREEN) |
83 | 108 self.Bind(wx.EVT_SIZE, self.onResize) |
81 | 109 self.Bind(wx.EVT_PAINT, self.onPaint) |
83 | 110 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
111 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
90 | 112 self.parent.host.bridge.tarotGameReady(user, referee, profile_key = self.parent.host.profile) |
81 | 113 |
114 def load_cards(self, dir): | |
115 """Load all the cards in memory | |
116 @param dir: directory where the PNG files are""" | |
117 self.cards={} | |
118 self.deck=[] | |
119 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names | |
120 self.cards["pique"]={} #spade | |
121 self.cards["coeur"]={} #heart | |
122 self.cards["carreau"]={} #diamond | |
123 self.cards["trefle"]={} #club | |
124 for file in glob.glob(dir+'/*_*.png'): | |
125 card = Card(file) | |
126 self.cards[card.family, card.value]=card | |
127 self.deck.append(card) | |
128 """for value in map(str,range(1,22))+['excuse']: | |
129 self.idx_cards.append(self.cards["atout",value]) | |
130 for family in ["pique", "coeur", "carreau", "trefle"]: | |
131 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: | |
132 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards ! | |
133 | |
87 | 134 def newGame(self, hand): |
135 """Start a new game, with given hand""" | |
90 | 136 print "gof: new game ici avec",hand |
87 | 137 assert (len(self.hand) == 0) |
138 for family, value in hand: | |
139 self.hand.append(self.cards[family, value]) | |
140 self.hand.sort() | |
141 self.my_turn = True | |
142 | |
83 | 143 def _is_on_hand(self, pos_x, pos_y): |
144 """Return True if the coordinate are on the hand cards""" | |
145 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
146 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
147 and pos_y < self.end_y: | |
148 return True | |
149 return False | |
150 | |
151 def onResize(self, event): | |
152 self._recalc_ori() | |
153 | |
154 def _recalc_ori(self): | |
155 """Recalculate origines, must be call when size change""" | |
156 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards | |
157 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
158 self.end_y = self.orig_y + CARD_HEIGHT | |
159 | |
81 | 160 def onPaint(self, event): |
161 dc = wx.PaintDC(self) | |
87 | 162 |
163 #We print the names to know who play where TODO: print avatars when available | |
164 max_x, max_y = self.GetSize() | |
165 border = 10 #border between nick and end of panel | |
166 right_y = left_y = 200 | |
167 right_width, right_height = dc.GetTextExtent(self.right_nick) | |
168 right_x = max_x - right_width - border | |
169 left_x = border | |
170 top_width, top_height = dc.GetTextExtent(self.top_nick) | |
171 top_x = (max_x - top_width) / 2 | |
172 top_y = border | |
173 dc.DrawText(self.right_nick, right_x, right_y) | |
174 dc.DrawText(self.top_nick, top_x, top_y) | |
175 dc.DrawText(self.left_nick, left_x, left_y) | |
176 | |
83 | 177 x=self.orig_x |
178 for card in self.hand: | |
87 | 179 card.draw(dc,x,self.orig_y - 30 if self.my_turn and card == self.selected else self.orig_y) |
83 | 180 x+=self.visible_size |
181 | |
182 def onMouseMove(self, event): | |
183 pos_x,pos_y = event.GetPosition() | |
184 if self._is_on_hand(pos_x, pos_y): | |
185 try: | |
186 self.selected = self.hand[(pos_x-self.orig_x)/self.visible_size] | |
187 except IndexError: | |
188 self.selected = self.hand[-1] | |
189 self.Refresh() | |
190 else: | |
191 self.selected = None | |
192 self.Refresh() | |
193 | |
194 def onMouseClick(self, event): | |
195 print "mouse click:",event.GetPosition() | |
196 pos_x,pos_y = event.GetPosition() | |
197 if self._is_on_hand(pos_x, pos_y): | |
198 idx = (pos_x-self.orig_x)/self.visible_size | |
199 if idx == len(self.hand): | |
200 idx-=1 | |
201 if self.hand[idx] == self.selected: | |
202 del self.hand[idx] | |
203 self._recalc_ori() | |
204 self.Refresh() |