-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets.py
More file actions
59 lines (42 loc) · 1.68 KB
/
Copy pathassets.py
File metadata and controls
59 lines (42 loc) · 1.68 KB
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
import os
import pygame
from models import Card
from settings import CARD_RADIUS, CARD_W, CARD_H
def apply_rounded_corners(img: pygame.Surface, radius: int) -> pygame.Surface:
img = img.convert_alpha()
w, h = img.get_size()
mask = pygame.Surface((w, h), pygame.SRCALPHA)
pygame.draw.rect(mask, (255, 255, 255, 255), (0, 0, w, h), border_radius=radius)
out = pygame.Surface((w, h), pygame.SRCALPHA)
out.blit(img, (0, 0))
out.blit(mask, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
return out
def load_deck_from_folder_unique(folder="cards") -> list[Card]:
if not os.path.isdir(folder):
raise FileNotFoundError("Missing 'cards' folder. Put your PNG cards in ./cards")
deck = []
for f in os.listdir(folder):
if f.lower().endswith(".png") and f.lower() != "back.png":
deck.append(Card(f))
if len(deck) < 20:
raise RuntimeError("No card images found (except back.png). Check ./cards")
return deck
def load_images_scaled(folder="cards") -> tuple[dict[str, pygame.Surface], pygame.Surface]:
images = {}
back_img = None
for f in os.listdir(folder):
if not f.lower().endswith(".png"):
continue
path = os.path.join(folder, f)
img = pygame.image.load(path).convert_alpha()
img = pygame.transform.smoothscale(img, (CARD_W, CARD_H))
img = apply_rounded_corners(img, CARD_RADIUS)
if f.lower() == "back.png":
back_img = img
else:
images[f] = img
if not images:
raise RuntimeError("No card images found in ./cards")
if back_img is None:
back_img = next(iter(images.values()))
return images, back_img