-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
160 lines (135 loc) · 5.01 KB
/
utils.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
import pygame
import os
import random
import math
import sys
# from card import Card, FOLDER_PATH, CARD_HEIGHT, CARD_WIDTH
from card import Card
import config
def sortCards(cards: list):
order = ['SPADES', 'HEARTS', 'CLUBS', 'DIAMONDS']
order = {key: i for i, key in enumerate(order)}
cards = sorted(cards, key=lambda k: k.rank, reverse=True) # sort by rank
# sort by suit by order specified by list 'order'
cards = sorted(cards, key=lambda d: order[d.suit])
# see: https://stackoverflow.com/a/34308981
return cards
def createDeck():
merged = []
images = [file_ for file_ in os.listdir(
config.FOLDER_PATH) if os.path.isfile(os.path.join(config.FOLDER_PATH, file_))]
for image in images:
rank, _, suit = os.path.splitext(image)[0].split('_')
image = os.path.join(config.FOLDER_PATH, image)
if rank == 'jack':
rank = 11
elif rank == 'queen':
rank = 12
elif rank == 'king':
rank = 13
elif rank == 'ace':
rank = 14
merged.append(
Card(image=image,
rank=int(rank), suit=suit.upper())
)
return merged
def renderCards(screen:pygame.Surface, decks:list, ongoingSuit:str, isFirstTurnInGame:bool, showEligible:bool):
# left, top, width, height
# The above order is used for rect, see http://www.pygame.org/docs/ref/rect.html
margin = lambda cardCount: (config.SCREEN_WIDTH - ((cardCount - 1) * config.CARD_DRAW_OFFSET + config.CARD_WIDTH)) // 2
for playerID, deck in enumerate(decks):
inDeck = []
mobile = []
for card in deck:
if not card.target:
inDeck.append(card)
else:
mobile.append(card)
if len(inDeck) == 0 and playerID == 0:
assert False, "render() called even though player has no cards"
for index, card in enumerate(inDeck):
if isFirstTurnInGame:
eligible = card.suit == 'SPADES' and card.rank == 14
else:
if not ongoingSuit:
eligible = True
else:
eligible = card.suit == ongoingSuit
if eligible and showEligible:
top = config.SCREEN_HEIGHT - config.CARD_HEIGHT - config.CARD_DRAW_PADDING_BOTTOM - config.ELIGIBLE_CARD_HEIGHT_OFFSET
else:
top = config.SCREEN_HEIGHT - config.CARD_HEIGHT - config.CARD_DRAW_PADDING_BOTTOM
height = config.CARD_HEIGHT
width = config.CARD_DRAW_OFFSET
if index == 0:
left = margin(len(inDeck))
elif index == len(inDeck) - 1:
left = left + config.CARD_DRAW_OFFSET
width = config.CARD_WIDTH
else:
left = left + config.CARD_DRAW_OFFSET
card.trueX = config.DECK_RECT[playerID][0]
card.trueY = config.DECK_RECT[playerID][1]
if playerID != 0:
continue
else: # since only the cards in player deck are clickable we dont care about other cards' rects
card.rect = pygame.Rect((left, top, width, height))
screen.blit(card.surf, card.rect.topleft)
for card in mobile:
card.update()
screen.blit(card.surf,card.rect.topleft)
def quit():
pygame.quit()
sys.exit()
def glowEdge(screen:pygame.Surface, playerID:int):
# Rect(left, top, width, height) -> Rect
if playerID == 0:
rect = (
0,
config.SCREEN_HEIGHT - config.GLOW_THICKNESS,
config.SCREEN_WIDTH,
config.GLOW_THICKNESS
)
elif playerID == 1:
rect = (
0,
0,
config.GLOW_THICKNESS,
config.SCREEN_HEIGHT
)
elif playerID == 2:
rect = (
0,
0,
config.SCREEN_WIDTH,
config.GLOW_THICKNESS
)
else:
rect = (
config.SCREEN_WIDTH - config.GLOW_THICKNESS,
0,
config.GLOW_THICKNESS,
config.SCREEN_HEIGHT
)
pygame.draw.rect(screen,config.GLOW_COLOR,rect)
def isThula(cards:list):
assert len(cards) == 2, "Cards provided for thula check must be only 2"
return cards[0].suit != cards[1].suit
def blit_text(surface, text, pos, font, color=pygame.Color('white')):
# This function is courtesy of Ted Klein Bergman. See: https://stackoverflow.com/a/42015712
words = [word.split(' ') for word in text.splitlines()]
space = font.size(' ')[0]
max_width, _ = surface.get_size()
x, y = pos
for line in words:
for word in line:
word_surface = font.render(word, 1, color)
word_width, word_height = word_surface.get_size()
if x + word_width >= max_width:
x = pos[0]
y += word_height
surface.blit(word_surface, (x, y))
x += word_width + space
x = pos[0]
y += word_height