comparison frontends/wix/card_game.py @ 87:66d784082930

Tarot game - Tarot plugin: game session start, first draft - wix: Tarot: names of players are now printed - wix: Tarot: game session start first draft - wix : Tarot: card can be compared and sorted
author Goffi <goffi@goffi.org>
date Tue, 11 May 2010 23:58:32 +0930
parents 4b5f2d55b6ac
children 4020931569b8
comparison
equal deleted inserted replaced
86:4b5f2d55b6ac 87:66d784082930
21 21
22 22
23 23
24 import wx 24 import wx
25 import os.path, glob 25 import os.path, glob
26 import random
27 import pdb 26 import pdb
28 from logging import debug, info, error 27 from logging import debug, info, error
29 from tools.jid import JID 28 from tools.jid import JID
30 from quick_frontend.quick_chat import QuickChat 29 from quick_frontend.quick_chat import QuickChat
31 from contact_list import ContactList 30 from contact_list import ContactList
32 31
33 CARD_WIDTH = 74 32 CARD_WIDTH = 74
34 CARD_HEIGHT = 136 33 CARD_HEIGHT = 136
35 MIN_WIDTH = 950 #Minimum size of the panel 34 MIN_WIDTH = 950 #Minimum size of the panel
36 MIN_HEIGHT = 500 35 MIN_HEIGHT = 500
36
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"]
37 39
38 class Card(): 40 class Card():
39 """This class is used to represent a card, graphically and logically""" 41 """This class is used to represent a card, graphically and logically"""
40 42
41 def __init__(self, file): 43 def __init__(self, file):
45 self.family,self.value=root_name.split('_') 47 self.family,self.value=root_name.split('_')
46 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False 48 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False
47 49
48 print "Carte:",self.family, self.value, self.bout 50 print "Carte:",self.family, self.value, self.bout
49 51
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)
50 74
51 def draw(self, dc, x, y): 75 def draw(self, dc, x, y):
52 """Draw the card on the device context 76 """Draw the card on the device context
53 @param dc: device context 77 @param dc: device context
54 @param x: abscissa 78 @param x: abscissa
57 81
58 82
59 class CardPanel(wx.Panel): 83 class CardPanel(wx.Panel):
60 """This class is used to display the cards""" 84 """This class is used to display the cards"""
61 85
62 def __init__(self, parent): 86 def __init__(self, parent, players, user):
63 wx.Panel.__init__(self, parent) 87 wx.Panel.__init__(self, parent)
88 self.players = players
89 self.user = user
90 self.bottom_nick = self.user
91 idx = self.players.index(self.user)
92 idx = (idx + 1) % len(self.players)
93 self.right_nick = self.players[idx]
94 idx = (idx + 1) % len(self.players)
95 self.top_nick = self.players[idx]
96 idx = (idx + 1) % len(self.players)
97 self.left_nick = self.players[idx]
64 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) 98 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT))
65 self.load_cards("/home/goffi/dev/divers/images/cards/") 99 self.load_cards("/home/goffi/dev/divers/images/cards/")
66 self.selected = None #contain the card to highlight 100 self.selected = None #contain the card to highlight
67 self.hand_size = 13 #number of cards in a hand 101 self.hand_size = 13 #number of cards in a hand
68 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards 102 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards
69 self.hand = random.sample(self.deck, self.hand_size) 103 self.hand = []
104 self.my_turn = False
70 self.SetBackgroundColour(wx.GREEN) 105 self.SetBackgroundColour(wx.GREEN)
71 self.Bind(wx.EVT_SIZE, self.onResize) 106 self.Bind(wx.EVT_SIZE, self.onResize)
72 self.Bind(wx.EVT_PAINT, self.onPaint) 107 self.Bind(wx.EVT_PAINT, self.onPaint)
73 self.Bind(wx.EVT_MOTION, self.onMouseMove) 108 self.Bind(wx.EVT_MOTION, self.onMouseMove)
74 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) 109 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick)
91 self.idx_cards.append(self.cards["atout",value]) 126 self.idx_cards.append(self.cards["atout",value])
92 for family in ["pique", "coeur", "carreau", "trefle"]: 127 for family in ["pique", "coeur", "carreau", "trefle"]:
93 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: 128 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
94 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards ! 129 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards !
95 130
131 def newGame(self, hand):
132 """Start a new game, with given hand"""
133 assert (len(self.hand) == 0)
134 for family, value in hand:
135 self.hand.append(self.cards[family, value])
136 self.hand.sort()
137 self.my_turn = True
138
96 def _is_on_hand(self, pos_x, pos_y): 139 def _is_on_hand(self, pos_x, pos_y):
97 """Return True if the coordinate are on the hand cards""" 140 """Return True if the coordinate are on the hand cards"""
98 if pos_x > self.orig_x and pos_y > self.orig_y \ 141 if pos_x > self.orig_x and pos_y > self.orig_y \
99 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ 142 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \
100 and pos_y < self.end_y: 143 and pos_y < self.end_y:
110 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 153 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20
111 self.end_y = self.orig_y + CARD_HEIGHT 154 self.end_y = self.orig_y + CARD_HEIGHT
112 155
113 def onPaint(self, event): 156 def onPaint(self, event):
114 dc = wx.PaintDC(self) 157 dc = wx.PaintDC(self)
158
159 #We print the names to know who play where TODO: print avatars when available
160 max_x, max_y = self.GetSize()
161 border = 10 #border between nick and end of panel
162 right_y = left_y = 200
163 right_width, right_height = dc.GetTextExtent(self.right_nick)
164 right_x = max_x - right_width - border
165 left_x = border
166 top_width, top_height = dc.GetTextExtent(self.top_nick)
167 top_x = (max_x - top_width) / 2
168 top_y = border
169 dc.DrawText(self.right_nick, right_x, right_y)
170 dc.DrawText(self.top_nick, top_x, top_y)
171 dc.DrawText(self.left_nick, left_x, left_y)
172
115 x=self.orig_x 173 x=self.orig_x
116 for card in self.hand: 174 for card in self.hand:
117 card.draw(dc,x,self.orig_y - 30 if card == self.selected else self.orig_y) 175 card.draw(dc,x,self.orig_y - 30 if self.my_turn and card == self.selected else self.orig_y)
118 x+=self.visible_size 176 x+=self.visible_size
119 177
120 def onMouseMove(self, event): 178 def onMouseMove(self, event):
121 pos_x,pos_y = event.GetPosition() 179 pos_x,pos_y = event.GetPosition()
122 if self._is_on_hand(pos_x, pos_y): 180 if self._is_on_hand(pos_x, pos_y):