-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.py
89 lines (78 loc) · 2.66 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
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
import pygame
import typing
import constants
import abstract_sprite
import preload
_symbol_images = {
}
class Symbol(abstract_sprite.AbstractSprite):
"""
One symbol sprite.
"""
def __init__(self,
filename: str,
coordinates: typing.Tuple[int, int] = (0, 0),
n: int = 1,
group: pygame.sprite.Group = None,
) -> None:
"""
Initialization of the sprite.
:param filename: path to image.
:param coordinates: initial coordinates (top left).
:param n:
:param group:
"""
super().__init__(group)
if (filename, n) not in _symbol_images:
_symbol_images[(filename, n)] = preload.load_image(filename, constants.COLOR_KEY, n)
self.image = _symbol_images[(filename, n)]
self.rect = self.image.get_rect().move(*coordinates)
class String(abstract_sprite.AbstractSprite):
"""
One string sprite.
"""
def __init__(self,
left: int, bottom: int,
alphabet: dict,
line: str,
group: pygame.sprite.Group = None,
n: int = 1,
surface_size: typing.Tuple[int, int] = constants.WINDOW_SIZE,
indent: typing.Tuple[int, int] = (0, 0),
) -> None:
"""
Initialization of the string.
:param left: left x border.
:param bottom: lower border y coordinate.
:param alphabet: alphabet.
:param line: string which to show.
:param group: sprite group where to put the sprite.
:param n: scaling factor.
:param surface_size: surface_size.
:param indent: indent from left border and lower border.
"""
super().__init__(group)
self.image = pygame.Surface(surface_size, pygame.SRCALPHA).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left = left
self.rect.bottom = bottom
self.line = line
self.n = n
self.alphabet = alphabet
self.indent = indent
self.show()
def show(self, indent=None) -> typing.Tuple[int, int]:
"""
Renders the string.
:param indent: indent from left border and lower border.
:return: new indent from left border and lower border.
"""
if indent is None:
indent = self.indent
ix, iy = indent
for char in self.line:
sb = Symbol(self.alphabet[char], n=self.n).image
width, height = sb.get_rect().size
self.image.blit(sb, (ix, self.rect.height - iy - height))
ix += width
return ix, iy