annotate frontends/wix/card_game.py @ 86:4b5f2d55b6ac

wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
author Goffi <goffi@goffi.org>
date Tue, 11 May 2010 13:06:05 +0930
parents 1ac759ea28c3
children 66d784082930
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 wix: a SAT frontend
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import wx
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import os.path, glob
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import random
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import pdb
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from logging import debug, info, error
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from tools.jid import JID
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from quick_frontend.quick_chat import QuickChat
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from contact_list import ContactList
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
33 CARD_WIDTH = 74
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
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
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
37
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 class Card():
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 """This class is used to represent a card, graphically and logically"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def __init__(self, file):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 """@param file: path of the PNG file"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.bitmap = wx.Image(file).ConvertToBitmap()
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 root_name = os.path.splitext(os.path.basename(file))[0]
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.family,self.value=root_name.split('_')
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 print "Carte:",self.family, self.value, self.bout
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
50
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def draw(self, dc, x, y):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 """Draw the card on the device context
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 @param dc: device context
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 @param x: abscissa
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 @param y: ordinate"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 dc.DrawBitmap(self.bitmap, x, y, True)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 class CardPanel(wx.Panel):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 """This class is used to display the cards"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def __init__(self, parent):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 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
64 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT))
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
65 self.load_cards("/home/goffi/dev/divers/images/cards/")
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
66 self.selected = None #contain the card to highlight
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
67 self.hand_size = 13 #number of cards in a hand
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
68 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
69 self.hand = random.sample(self.deck, self.hand_size)
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.SetBackgroundColour(wx.GREEN)
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
71 self.Bind(wx.EVT_SIZE, self.onResize)
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.Bind(wx.EVT_PAINT, self.onPaint)
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
73 self.Bind(wx.EVT_MOTION, self.onMouseMove)
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
74 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick)
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def load_cards(self, dir):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 """Load all the cards in memory
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @param dir: directory where the PNG files are"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.cards={}
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.deck=[]
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.cards["pique"]={} #spade
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.cards["coeur"]={} #heart
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self.cards["carreau"]={} #diamond
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.cards["trefle"]={} #club
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 for file in glob.glob(dir+'/*_*.png'):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 card = Card(file)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.cards[card.family, card.value]=card
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.deck.append(card)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 """for value in map(str,range(1,22))+['excuse']:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.idx_cards.append(self.cards["atout",value])
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 for family in ["pique", "coeur", "carreau", "trefle"]:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards !
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
96 def _is_on_hand(self, pos_x, pos_y):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
97 """Return True if the coordinate are on the hand cards"""
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
98 if pos_x > self.orig_x and pos_y > self.orig_y \
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
99 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
100 and pos_y < self.end_y:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
101 return True
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
102 return False
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
103
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
104 def onResize(self, event):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
105 self._recalc_ori()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
106
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
107 def _recalc_ori(self):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
108 """Recalculate origines, must be call when size change"""
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
109 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
110 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
111 self.end_y = self.orig_y + CARD_HEIGHT
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
112
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def onPaint(self, event):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 dc = wx.PaintDC(self)
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
115 x=self.orig_x
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
116 for card in self.hand:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
117 card.draw(dc,x,self.orig_y - 30 if card == self.selected else self.orig_y)
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
118 x+=self.visible_size
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
119
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
120 def onMouseMove(self, event):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
121 pos_x,pos_y = event.GetPosition()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
122 if self._is_on_hand(pos_x, pos_y):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
123 try:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
124 self.selected = self.hand[(pos_x-self.orig_x)/self.visible_size]
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
125 except IndexError:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
126 self.selected = self.hand[-1]
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
127 self.Refresh()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
128 else:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
129 self.selected = None
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
130 self.Refresh()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
131
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
132 def onMouseClick(self, event):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
133 print "mouse click:",event.GetPosition()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
134 pos_x,pos_y = event.GetPosition()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
135 if self._is_on_hand(pos_x, pos_y):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
136 idx = (pos_x-self.orig_x)/self.visible_size
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
137 if idx == len(self.hand):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
138 idx-=1
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
139 if self.hand[idx] == self.selected:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
140 del self.hand[idx]
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
141 self._recalc_ori()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
142 self.Refresh()
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 class CardGame(wx.Frame, QuickChat):
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
145 """The window used to play all kind of card games"""
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 def __init__(self, host):
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
148 wx.Frame.__init__(self, None, pos=(0,0), size=(950,500))
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 self.host = host
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.sizer = wx.BoxSizer(wx.VERTICAL)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 self.panel = CardPanel(self)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 self.sizer.Add(self.panel, 1, flag=wx.EXPAND)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 self.SetSizer(self.sizer)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 self.SetAutoLayout(True)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 #events
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160