-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
95 lines (87 loc) · 4.15 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
import pygame, random
import utils
from pygame.locals import (
K_ESCAPE,
KEYDOWN,
QUIT,
)
class Player():
def __init__(self, cards:list, id:int, sprites:pygame.sprite.Group):
assert len(cards) == 13, "Cards not equally distributed"
self.thulaReceived = 0
self.thulaGiven = 0
self.cards = cards
self.id = id
self.isTurn = False
self.cardThrownThisTurn = None
self.hasAceOfSpades = False
self.spriteGroup = sprites
for card in self.cards:
if card.suit == 'SPADES' and card.rank == 14:
self.hasAceOfSpades = True
break
def getCard(self, ongoingSuit: str, isFirstTurnInGame:bool):
assert self.isTurn, "Wait for your turn, please"
if ongoingSuit: # if round is ongoing
cards = filter(lambda c: c.suit == ongoingSuit, self.cards)
matchingCards = []
for card in cards:
matchingCards.append(card)
if len(matchingCards) == 0:
self.thulaGiven += 1
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
for card in self.spriteGroup:
if card.rect.collidepoint(event.pos):
self.cardThrownThisTurn = card
self.isTurn = False
return self.cardThrownThisTurn
if event.type == pygame.QUIT:
utils.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
utils.quit()
else:
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
for card in self.spriteGroup:
if card.rect.collidepoint(event.pos) and card.suit == ongoingSuit:
self.cardThrownThisTurn = card
self.isTurn = False
return self.cardThrownThisTurn
if event.type == pygame.QUIT:
utils.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
utils.quit()
else: # if round is to be started
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
for card in self.spriteGroup:
if isFirstTurnInGame: # if this is the first round in the game
if card.rect.collidepoint(event.pos) and card.suit == 'SPADES' and card.rank == 14:
# if card.rect.collidepoint(event.pos):
# in case of first round in game player should only throw the ace of spades
self.cardThrownThisTurn = card
self.isTurn = False
return self.cardThrownThisTurn
else:
if card.rect.collidepoint(event.pos):
# after a thula player may throw any card
self.cardThrownThisTurn = card
self.isTurn = False
return self.cardThrownThisTurn
if event.type == pygame.QUIT:
utils.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
utils.quit()
def insertCards(self, cards: list):
self.thulaReceived += 1 # cards are only inserted in case of thula
for card in cards:
self.cards.append(card)
self.spriteGroup.add(card)
self.cards = utils.sortCards(self.cards)