annotate 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
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
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
35
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class Card():
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """This class is used to represent a card, graphically and logically"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 def __init__(self, file):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 """@param file: path of the PNG file"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.bitmap = wx.Image(file).ConvertToBitmap()
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 root_name = os.path.splitext(os.path.basename(file))[0]
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.family,self.value=root_name.split('_')
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 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
45
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 print "Carte:",self.family, self.value, self.bout
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
48
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def draw(self, dc, x, y):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 """Draw the card on the device context
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 @param dc: device context
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 @param x: abscissa
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 @param y: ordinate"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 dc.DrawBitmap(self.bitmap, x, y, True)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class CardPanel(wx.Panel):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """This class is used to display the cards"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def __init__(self, parent):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 wx.Panel.__init__(self, parent)
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
62 self.load_cards("/home/goffi/dev/divers/images/cards/")
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
63 self.selected = None #contain the card to highlight
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
64 self.hand_size = 13 #number of cards in a hand
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
65 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
66 self.hand = random.sample(self.deck, self.hand_size)
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.SetBackgroundColour(wx.GREEN)
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
68 self.Bind(wx.EVT_SIZE, self.onResize)
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.Bind(wx.EVT_PAINT, self.onPaint)
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
70 self.Bind(wx.EVT_MOTION, self.onMouseMove)
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
71 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick)
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def load_cards(self, dir):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 """Load all the cards in memory
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @param dir: directory where the PNG files are"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.cards={}
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.deck=[]
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 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
79 self.cards["pique"]={} #spade
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.cards["coeur"]={} #heart
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.cards["carreau"]={} #diamond
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.cards["trefle"]={} #club
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 for file in glob.glob(dir+'/*_*.png'):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 card = Card(file)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.cards[card.family, card.value]=card
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.deck.append(card)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 """for value in map(str,range(1,22))+['excuse']:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.idx_cards.append(self.cards["atout",value])
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 for family in ["pique", "coeur", "carreau", "trefle"]:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 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
92
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
93 def _is_on_hand(self, pos_x, pos_y):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
94 """Return True if the coordinate are on the hand cards"""
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
95 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
96 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
97 and pos_y < self.end_y:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
98 return True
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
99 return False
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
100
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
101 def onResize(self, event):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
102 self._recalc_ori()
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 _recalc_ori(self):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
105 """Recalculate origines, must be call when size change"""
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
106 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
107 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
108 self.end_y = self.orig_y + CARD_HEIGHT
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
109
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def onPaint(self, event):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 dc = wx.PaintDC(self)
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
112 x=self.orig_x
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
113 for card in self.hand:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
114 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
115 x+=self.visible_size
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
116
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
117 def onMouseMove(self, event):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
118 pos_x,pos_y = event.GetPosition()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
119 if self._is_on_hand(pos_x, pos_y):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
120 try:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
121 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
122 except IndexError:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
123 self.selected = self.hand[-1]
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
124 self.Refresh()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
125 else:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
126 self.selected = None
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
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
129 def onMouseClick(self, event):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
130 print "mouse click:",event.GetPosition()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
131 pos_x,pos_y = event.GetPosition()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
132 if self._is_on_hand(pos_x, pos_y):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
133 idx = (pos_x-self.orig_x)/self.visible_size
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
134 if idx == len(self.hand):
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
135 idx-=1
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
136 if self.hand[idx] == self.selected:
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
137 del self.hand[idx]
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
138 self._recalc_ori()
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
139 self.Refresh()
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 class CardGame(wx.Frame, QuickChat):
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
142 """The window used to play all kind of card games"""
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 def __init__(self, host):
83
1ac759ea28c3 wix: tarot game preparation step
Goffi <goffi@goffi.org>
parents: 81
diff changeset
145 wx.Frame.__init__(self, None, pos=(0,0), size=(950,500))
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 self.host = host
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 self.sizer = wx.BoxSizer(wx.VERTICAL)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 self.panel = CardPanel(self)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 self.sizer.Add(self.panel, 1, flag=wx.EXPAND)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.SetSizer(self.sizer)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 self.SetAutoLayout(True)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 #events
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157