-
Notifications
You must be signed in to change notification settings - Fork 5
/
text.py
46 lines (35 loc) · 1.36 KB
/
text.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
from pygame import Color, Rect, Surface
from pygame.font import Font
from pacman.data_core import Colors, FontCfg, IDrawable
from pacman.misc import RectObj
class Text(RectObj, IDrawable):
def __init__(self, text: str, size: int, rect: Rect = Rect(0, 0, 0, 0), color=Colors.WHITE, font=FontCfg.DEFAULT):
super().__init__(rect)
self.__text = ""
self.__color = color
self.__font = Font(font, size)
self.__surface = self.__font.render(self.__text, False, self.__color)
self.text = text
@property
def text(self) -> str:
return self.__text
@text.setter
def text(self, text: str) -> None:
self.__text = text if text else self.__text
self.__surface = self.__font.render(self.__text, False, self.__color)
topleft = self.rect.topleft
self.rect = self.__surface.get_rect()
self.rect.topleft = topleft
@property
def color(self) -> Color:
return self.__color
@color.setter
def color(self, color: Color) -> None:
self.__color = color
self.__surface = self.__font.render(self.__text, False, self.__color)
def draw(self, screen: Surface) -> None:
screen.blit(self.__surface, self.rect)
def set_alpha(self, alpha: int) -> None:
self.__surface.set_alpha(alpha)
def __repr__(self):
return f"Text: {self.__text}"