-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.py
55 lines (44 loc) · 1.62 KB
/
card.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
import pygame
from utils import *
from constants import *
from assetloader import AssetLoader
class Card:
def __init__(self, x: int, y: int, number: int, anim: float = 0, ogin=(430+64, -148)):
self.x, self.y = x, y
self.anim = anim
self.origin = ogin
self.grabbed = False
self.number = number
self._gp = (0, 0)
@property
def image(self) -> pygame.Surface:
return AssetLoader.card_images[self.number - 1]
@property
def pos(self) -> tuple[float, float]:
return self.x, self.y
@pos.setter
def pos(self, value) -> None:
self.x, self.y = value
def hash(self):
return sha256(f"<{self.number}>".encode("utf-8")).hexdigest()
def copy(self):
return Card(self.x, self.y, self.number, self.anim, self.origin)
@property
def rect(self) -> pygame.Rect:
return self.image.get_rect(topleft=self.pos)
def draw(self, screen: pygame.Surface):
self.anim += 4/FRAMERATE
if self.anim > 1:
self.anim = 1
s_ = self.rect.inflate(4, 4)[:2]
oawihef = lerp_pos(self.origin, s_[:2], self.anim)
mand = pygame.Rect(*oawihef, 94, 152)
pygame.draw.rect(screen, (0, 0, 0), mand.inflate(2, 2))
screen.blit(self.image, self.image.get_rect(center=mand.center))
if self.grabbed:
self.pos = mp()[0]-self._gp[0], mp()[1]-self._gp[1]
def grab(self):
self.grabbed = True
self._gp = mp()[0]-self.pos[0], mp()[1]-self.pos[1]
def __repr__(self):
return f"<Card(pos={self.pos}, number={self.number}, grabbed={self.grabbed})>"