-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.py
212 lines (121 loc) · 5.5 KB
/
Player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from ContractLoader import ContractLoader
import pygame, sys
from Card import Card
class Player():
def __init__(self, name: str):
self.name = name
self.deck = []
self.points = 0
self.rank = 1
self.contractList = ContractLoader().loadContracts()
def setName(self, screen: pygame.Surface, bgColor: tuple, font: pygame.font.Font, width: int, height: int, players: list) -> str:
"""
Définit le nom du joueur via les inputs du clavier.
"""
current_string = []
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if (pygame.K_0 <= event.key and event.key <= pygame.K_9) or (pygame.K_a <= event.key and event.key <= pygame.K_z):
current_string.append(pygame.key.name(event.key))
elif event.key == pygame.K_BACKSPACE:
current_string = current_string[0:-1]
elif event.key == pygame.K_RETURN:
if len(current_string) == 0 or "".join(current_string) in [p.name for p in players] or len(current_string) > 12:
pass
else:
done = True
elif event.key == pygame.K_SPACE:
current_string.append(" ")
screen.fill(bgColor)
chooseTxt = font.render(self.name + "'s name: " + "".join(current_string), True, (0,0,0))
widthText, heightText = font.size(self.name + "'s name: " + "".join(current_string))
screen.blit(chooseTxt, (width//2 - widthText//2, height//2 - heightText//2))
pygame.display.update()
self.name = "".join(current_string)
return self.name
def take(self, card: Card) -> Card:
"""
Prend une carte.
"""
self.deck.append(card)
def throw(self, card: Card) -> Card:
self.deck.remove(card)
return card
def chooseCardTrick(self, deckThrow: tuple, i: int) -> Card:
"""
Permet de choisir ca carte.
"""
if len(deckThrow)==0:
return self.throw(self.deck[i])
else:
testColor=0
colorTrick = deckThrow[0][0].couleur
for j in range(len(self.deck)):
if self.deck[j].couleur==colorTrick:
testColor+=1
if testColor!=0:
while True:
try:
assert self.deck[i].couleur==colorTrick
return self.throw(self.deck[i])
except:
return None
else:
return self.throw(self.deck[i])
def addPoints(self, points: int):
"""
Permet d'ajouter des points.
"""
self.points += points
def removePoints(self, points: int):
"""
Permet de retirer des points.
"""
self.points -= points
def chooseContract(self, i: int) -> dict:
"""
Retourne le contrat choisie.
"""
return self.contractList.pop(i)
def waitingScreen(self, screen: pygame.Surface, bgColor: tuple, font: pygame.font.Font, width: int, height: int) -> pygame.Surface:
"""
Affiche l'écran d'attente.
"""
screen.fill(bgColor)
waitingTxt = font.render(self.name + ", press Enter to play", True, (0,0,0))
widthTxt, heightTxt = font.size(self.name + ", press Enter to play")
screen.blit(waitingTxt, (width//2 - widthTxt//2, height//2 - heightTxt//2))
return screen
def showCards(self, screen: pygame.Surface, bgColor: tuple, font: pygame.font.Font, width: int, height: int, currentContract: dict) -> tuple:
"""
Affiche le nom du jouer et son deck.
"""
screen.fill(bgColor)
nameTxt = font.render(self.name + "'s turn (" + currentContract["name"] + ")" if currentContract != None else self.name + "'s turn", True, (0,0,0))
widthText, heightText = font.size(self.name + "'s turn (" + currentContract["name"] + ")" if currentContract != None else self.name + "'s turn")
screen.blit(nameTxt, (width//2 - widthText//2, 10))
cardRects = []
i = 0
for card in self.deck:
cardRect = card.aff.get_rect()
cardRect.move_ip(i, height - 285)
cardRects.append(cardRect)
screen.blit(card.aff, cardRect)
i += width//len(self.deck)
return screen, cardRects
def showContracts(self, screen: pygame.Surface, bgColor: tuple, font: pygame.font.Font, width: int, height: int) -> tuple:
"""
Affiche des cartes correspondant au contrat.
"""
cardRects = []
for i in range(len(self.contractList)):
el = self.contractList[i]
card = Card(el["cardToDisplay"]["value"], el["cardToDisplay"]["couleur"], el["cardToDisplay"]["n"])
cardRect = card.aff.get_rect()
cardRect.move_ip(i*(width//len(self.contractList)) + 192//3, height//2 - 280//2)
cardRects.append(cardRect)
screen.blit(card.aff, cardRect)
return screen, cardRects