annotate frontends/wix/card_game.py @ 81:104a815bb23f

Tarot game: first draft - wix: first draft of cards window - shell script to split cards from Tarot game found on wikimedia commons
author Goffi <goffi@goffi.org>
date Sun, 18 Apr 2010 15:47:10 +1000
parents
children 1ac759ea28c3
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
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 class Card():
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 """This class is used to represent a card, graphically and logically"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 def __init__(self, file):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """@param file: path of the PNG file"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.bitmap = wx.Image(file).ConvertToBitmap()
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 root_name = os.path.splitext(os.path.basename(file))[0]
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 self.family,self.value=root_name.split('_')
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 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
42
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 print "Carte:",self.family, self.value, self.bout
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 def draw(self, dc, x, y):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 """Draw the card on the device context
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @param dc: device context
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 @param x: abscissa
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @param y: ordinate"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 dc.DrawBitmap(self.bitmap, x, y, True)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 class CardPanel(wx.Panel):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 """This class is used to display the cards"""
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 def __init__(self, parent):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 wx.Panel.__init__(self, parent)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.SetBackgroundColour(wx.GREEN)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.Bind(wx.EVT_PAINT, self.onPaint)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.load_cards("/home/goffi/dev/divers/images/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 load_cards(self, dir):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """Load all the cards in memory
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 @param dir: directory where the PNG files are"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.cards={}
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.deck=[]
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 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
68 self.cards["pique"]={} #spade
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.cards["coeur"]={} #heart
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.cards["carreau"]={} #diamond
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.cards["trefle"]={} #club
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 for file in glob.glob(dir+'/*_*.png'):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 card = Card(file)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.cards[card.family, card.value]=card
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.deck.append(card)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 """for value in map(str,range(1,22))+['excuse']:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.idx_cards.append(self.cards["atout",value])
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 for family in ["pique", "coeur", "carreau", "trefle"]:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 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
81
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def onPaint(self, event):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 print "toto", event
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 dc = wx.PaintDC(self)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 x=0
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 for card in random.sample(self.deck,13):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 card.draw(dc,x,0)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 x+=37
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 class CardGame(wx.Frame, QuickChat):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 """The chat Window for one to one conversations"""
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def __init__(self, host):
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 wx.Frame.__init__(self, None, pos=(0,0), size=(800,550))
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self.host = host
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 self.sizer = wx.BoxSizer(wx.VERTICAL)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 self.panel = CardPanel(self)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.sizer.Add(self.panel, 1, flag=wx.EXPAND)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.SetSizer(self.sizer)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 self.SetAutoLayout(True)
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 #events
104a815bb23f Tarot game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106