Mercurial > libervia-backend
annotate frontends/wix/card_game.py @ 150:63d20bda5754
Primitivus: Tarot game
Tarot game is now playable, scores are not showed yet.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 28 Jul 2010 19:58:10 +0800 |
parents | 80661755ea8d |
children | 74aaf230a7c3 |
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 | |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
133
diff
changeset
|
29 from tools.games import TarotCard |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
30 from quick_frontend.quick_card_game import QuickCardGame |
103 | 31 from xmlui import XMLUI |
81 | 32 |
83 | 33 CARD_WIDTH = 74 |
34 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
|
35 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
|
36 MIN_HEIGHT = 500 |
83 | 37 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
38 class WxCard(TarotCard): |
81 | 39 """This class is used to represent a card, graphically and logically""" |
40 | |
41 def __init__(self, file): | |
42 """@param file: path of the PNG file""" | |
43 self.bitmap = wx.Image(file).ConvertToBitmap() | |
44 root_name = os.path.splitext(os.path.basename(file))[0] | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
45 suit,value = root_name.split('_') |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
46 TarotCard.__init__(self, (suit, value)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
47 print "Carte:",suit, value #, self.bout |
81 | 48 |
49 def draw(self, dc, x, y): | |
50 """Draw the card on the device context | |
51 @param dc: device context | |
52 @param x: abscissa | |
53 @param y: ordinate""" | |
54 dc.DrawBitmap(self.bitmap, x, y, True) | |
55 | |
56 | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
57 class CardPanel(QuickCardGame,wx.Panel): |
81 | 58 """This class is used to display the cards""" |
59 | |
92 | 60 def __init__(self, parent, referee, players, player_nick): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
61 QuickCardGame.__init__(self, parent, referee, players, player_nick) |
81 | 62 wx.Panel.__init__(self, parent) |
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
|
63 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
64 self.loadCards("/home/goffi/dev/divers/images/cards/") |
92 | 65 self.mouse_over_card = None #contain the card to highlight |
83 | 66 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards |
87 | 67 self.hand = [] |
92 | 68 self.to_show = [] |
69 self.state = None | |
81 | 70 self.SetBackgroundColour(wx.GREEN) |
83 | 71 self.Bind(wx.EVT_SIZE, self.onResize) |
81 | 72 self.Bind(wx.EVT_PAINT, self.onPaint) |
83 | 73 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
74 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
92 | 75 self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile) |
81 | 76 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
77 def loadCards(self, dir): |
81 | 78 """Load all the cards in memory |
79 @param dir: directory where the PNG files are""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
80 QuickCardGame.loadCards(self) |
81 | 81 for file in glob.glob(dir+'/*_*.png'): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
82 card = WxCard(file) |
92 | 83 self.cards[card.suit, card.value]=card |
81 | 84 self.deck.append(card) |
85 | |
87 | 86 def newGame(self, hand): |
87 """Start a new game, with given hand""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
88 QuickCardGame.newGame(self, hand) |
92 | 89 self._recalc_ori() |
90 self.Refresh() | |
87 | 91 |
91 | 92 def contratSelected(self, data): |
93 """Called when the contrat has been choosed | |
94 @param data: form result""" | |
95 debug (_("Contrat choosed")) | |
96 contrat = data[0][1] | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
97 QuickCardGame.contratSelected(self, contrat) |
91 | 98 |
99 def chooseContrat(self, xml_data): | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
100 """Called when the player as to select his contrat |
91 | 101 @param xml_data: SàT xml representation of the form""" |
102 misc = {'callback': self.contratSelected} | |
133
d998adb62d1a
wix: fixed wrong class Name in card_game
Goffi <goffi@goffi.org>
parents:
103
diff
changeset
|
103 form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) |
91 | 104 |
95 | 105 def showScores(self, xml_data, winners, loosers): |
106 """Called when the player as to select hist contrat | |
107 @param xml_data: SàT xml representation of the form""" | |
133
d998adb62d1a
wix: fixed wrong class Name in card_game
Goffi <goffi@goffi.org>
parents:
103
diff
changeset
|
108 form = XMLUI(self.parent.host, xml_data, title = _('You win \o/') if self.player_nick in winners else _('You loose :('), options = ['NO_CANCEL']) |
95 | 109 |
93 | 110 def cardsPlayed(self, player, cards): |
111 """A card has been played by player""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
112 QuickCardGame.cardsPlayed(self, player, cards) |
93 | 113 self.Refresh() |
114 | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
115 def invalidCards(self, phase, played_cards, invalid_cards): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
116 """Invalid cards have been played |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
117 @param phase: phase of the game |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
118 @param played_cards: all the cards played |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
119 @param invalid_cards: cards which are invalid""" |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
120 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
121 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
122 self._recalc_ori() |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
123 self.Refresh() |
150 | 124 #gof: wx.MessageDialog(self, _("Cards played are invalid !"), _("Error"), wx.OK | wx.ICON_ERROR).ShowModal() |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
125 |
83 | 126 def _is_on_hand(self, pos_x, pos_y): |
127 """Return True if the coordinate are on the hand cards""" | |
128 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
129 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
130 and pos_y < self.end_y: | |
131 return True | |
132 return False | |
133 | |
134 def onResize(self, event): | |
135 self._recalc_ori() | |
136 | |
137 def _recalc_ori(self): | |
92 | 138 """Recalculate origins of hand, must be call when hand size change""" |
83 | 139 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards |
140 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
141 self.end_y = self.orig_y + CARD_HEIGHT | |
142 | |
81 | 143 def onPaint(self, event): |
144 dc = wx.PaintDC(self) | |
87 | 145 |
146 #We print the names to know who play where TODO: print avatars when available | |
147 max_x, max_y = self.GetSize() | |
148 border = 10 #border between nick and end of panel | |
149 right_y = left_y = 200 | |
150 right_width, right_height = dc.GetTextExtent(self.right_nick) | |
151 right_x = max_x - right_width - border | |
152 left_x = border | |
153 top_width, top_height = dc.GetTextExtent(self.top_nick) | |
154 top_x = (max_x - top_width) / 2 | |
155 top_y = border | |
156 dc.DrawText(self.right_nick, right_x, right_y) | |
157 dc.DrawText(self.top_nick, top_x, top_y) | |
158 dc.DrawText(self.left_nick, left_x, left_y) | |
159 | |
93 | 160 #We draw the played cards: |
161 center_y = 200 #ordinate used as center point | |
162 left_x = (max_x - CARD_WIDTH)/2 - CARD_WIDTH - 5 | |
163 right_x = (max_x/2) + (CARD_WIDTH/2) + 5 | |
164 left_y = right_y = center_y - CARD_HEIGHT/2 | |
165 top_x = bottom_x = (max_x - CARD_WIDTH)/2 | |
166 top_y = center_y - CARD_HEIGHT - 5 | |
167 bottom_y = center_y + 5 | |
168 for side in ['left', 'top', 'right', 'bottom']: | |
169 card = self.played[getattr(self, side+'_nick')] | |
170 if card != None: | |
171 card.draw(dc,locals()[side+'_x'], locals()[side+'_y']) | |
172 | |
83 | 173 x=self.orig_x |
174 for card in self.hand: | |
92 | 175 if (self.state == "play" or self.state == "ecart") and card == self.mouse_over_card \ |
176 or self.state == "ecart" and card in self.selected: | |
177 y = self.orig_y - 30 | |
178 else: | |
179 y = self.orig_y | |
180 | |
181 card.draw(dc,x,y) | |
83 | 182 x+=self.visible_size |
183 | |
92 | 184 if self.to_show: |
185 """There are cards to display in the middle""" | |
186 size = len(self.to_show)*(CARD_WIDTH+10)-10 | |
187 x = (max_x - size)/2 | |
188 for card in self.to_show: | |
189 card.draw(dc, x, 150) | |
190 x+=CARD_WIDTH+10 | |
191 | |
83 | 192 def onMouseMove(self, event): |
193 pos_x,pos_y = event.GetPosition() | |
194 if self._is_on_hand(pos_x, pos_y): | |
195 try: | |
92 | 196 self.mouse_over_card = self.hand[(pos_x-self.orig_x)/self.visible_size] |
83 | 197 except IndexError: |
92 | 198 self.mouse_over_card = self.hand[-1] |
83 | 199 self.Refresh() |
200 else: | |
92 | 201 self.mouse_over_card = None |
83 | 202 self.Refresh() |
203 | |
204 def onMouseClick(self, event): | |
205 print "mouse click:",event.GetPosition() | |
206 pos_x,pos_y = event.GetPosition() | |
92 | 207 |
208 if self.state == "chien": | |
209 self.to_show = [] | |
210 self.state = "wait" | |
211 return | |
212 elif self.state == "wait_for_ecart": | |
213 self.state = "ecart" | |
214 self.hand.extend(self.to_show) | |
215 self.hand.sort() | |
216 self.to_show = [] | |
217 self._recalc_ori() | |
218 self.Refresh() | |
219 return | |
220 | |
83 | 221 if self._is_on_hand(pos_x, pos_y): |
222 idx = (pos_x-self.orig_x)/self.visible_size | |
223 if idx == len(self.hand): | |
224 idx-=1 | |
92 | 225 if self.hand[idx] == self.mouse_over_card: |
226 if self.state == "ecart": | |
227 if self.hand[idx] in self.selected: | |
228 self.selected.remove(self.hand[idx]) | |
229 else: | |
230 self.selected.append(self.hand[idx]) | |
231 if len(self.selected) == 6: #TODO: use variable here, as chien len can change with variants | |
232 dlg = wx.MessageDialog(self, _("Do you put these cards in chien ?"), _(u"Écart"), wx.YES_NO | wx.ICON_QUESTION) | |
233 answer = dlg.ShowModal() | |
234 if answer == wx.ID_YES: | |
235 ecart = [] | |
236 for card in self.selected: | |
237 ecart.append((card.suit, card.value)) | |
238 self.hand.remove(card) | |
239 print "gof: Cartes envoyes au chien:", ecart | |
240 del self.selected[:] | |
241 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, profile_key = self.parent.host.profile) | |
242 self.state = "wait" | |
243 | |
244 self._recalc_ori() | |
245 self.Refresh() | |
93 | 246 if self.state == "play": |
247 card = self.hand[idx] | |
248 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile) | |
249 del self.hand[idx] | |
250 self.state = "wait" | |
251 self._recalc_ori() | |
252 self.Refresh() | |
253 | |
254 |