comparison frontends/primitivus/card_game.py @ 144:80661755ea8d

Primitivus: Tarot card game implementation - quick frontend: card_game added - wix: card_game splitted with quick frontend - tools: new game library - primitivus: new card_game widget (not finished yet) - primitivus: SàT XML UI management: first draft
author Goffi <goffi@goffi.org>
date Mon, 26 Jul 2010 19:43:44 +0800
parents
children 63d20bda5754
comparison
equal deleted inserted replaced
143:119f45746fde 144:80661755ea8d
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Primitivus: 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 import urwid
23 from tools.games import TarotCard
24 from quick_frontend.quick_card_game import QuickCardGame
25 from xmlui import XMLUI
26
27 class Hand(urwid.WidgetWrap):
28 """Used to display several cards, and manage a hand"""
29
30 def __init__(self):
31 self.columns = urwid.Columns([])
32 urwid.WidgetWrap.__init__(self, self.columns)
33
34 def update(self, hand):
35 """Update the hand displayed in this widget"""
36 del self.columns.widget_list[:]
37 del self.columns.column_types[:]
38 for card in hand:
39 self.columns.widget_list.append(urwid.Text(card.getAttrText()))
40 self.columns.column_types.append(('weight',1))
41
42
43
44
45 class Card(TarotCard):
46 """This class is used to represent a card, logically
47 and give a text representation with attributes"""
48
49 def __init__(self, suit, value):
50 """@param file: path of the PNG file"""
51 TarotCard.__init__(self, (suit, value))
52
53 def getAttrText(self):
54 """return text representation of the card with attributes"""
55 try:
56 value = "%02i" % int(self.value)
57 except ValueError:
58 value = self.value[0].upper()+self.value[1]
59 if self.suit == "atout":
60 if self.value == "excuse":
61 suit = 'c'
62 else:
63 suit = 'A'
64 color = 'neutral'
65 elif self.suit == "pique":
66 suit = u'♠'
67 color = 'black'
68 elif self.suit == "trefle":
69 suit = u'♣'
70 color = 'black'
71 elif self.suit == "coeur":
72 suit = u'♥'
73 color = 'red'
74 elif self.suit == "carreau":
75 suit = u'♦'
76 color = 'red'
77 if self.bout:
78 color = 'special'
79 return ('card_%s' % color,u"%s%s" % (value,suit))
80
81 class CardGame(QuickCardGame,urwid.WidgetWrap):
82 """Widget for card games"""
83
84 def __init__(self, parent, referee, players, player_nick):
85 QuickCardGame.__init__(self, parent, referee, players, player_nick)
86 self.loadCards()
87 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), 'center')])
88 self.top_card_wid = urwid.Text('')
89 self.center_cards_wid = urwid.Text(' - ')
90 self.bottom_card_wid = urwid.Text('')
91 center = urwid.Pile([urwid.Padding(self.top_card_wid,'center'),
92 urwid.Columns([('fixed',len(self.left_nick),urwid.Text(self.left_nick)),
93 urwid.Padding(self.center_cards_wid,'center'),
94 ('fixed',len(self.right_nick),urwid.Text(self.right_nick))
95 ]),
96 urwid.Padding(self.bottom_card_wid,'center')
97 ])
98 body = urwid.Filler(center)
99 self.hand_wid = Hand()
100 self.main_frame = urwid.Frame(body,header=self.top, footer=self.hand_wid)
101 urwid.WidgetWrap.__init__(self,self.main_frame)
102 self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile)
103
104 def loadCards(self):
105 """Load all the cards in memory"""
106 QuickCardGame.loadCards(self)
107 for value in map(str,range(1,22))+['excuse']:
108 card = Card('atout',value)
109 self.cards[card.suit, card.value]=card
110 self.deck.append(card)
111 for suit in ["pique", "coeur", "carreau", "trefle"]:
112 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
113 card = Card(suit,value)
114 self.cards[card.suit, card.value]=card
115 self.deck.append(card)
116
117 def newGame(self, hand):
118 """Start a new game, with given hand"""
119 QuickCardGame.newGame(self, hand)
120 self.hand_wid.update(self.hand)
121 self.parent.host.redraw()
122
123 def contratSelected(self, data):
124 """Called when the contrat has been choosed
125 @param data: form result"""
126 contrat = data[0][1]
127 QuickCardGame.contratSelected(self, contrat)
128
129 def chooseContrat(self, xml_data):
130 """Called when the player as to select his contrat
131 @param xml_data: SàT xml representation of the form"""
132 misc = {'callback': self.contratSelected}
133 form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc)
134 form.show()
135
136 """def selectable(self):
137 return True
138
139 def keypress(self, size, key):
140 return key
141
142 def render(self, size, focus=False):
143 return self.display_widget(size, focus).render(size, focus)
144
145 def display_widget(self, size, focus):
146 (maxcol,maxrow) = size
147
148 return self.main_frame"""
149