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
|
import pygame
class DialogSprite(pygame.sprite.Sprite):
"""
DialogSprite is a custom Sprite class for text dialogs
text: list with text to display; each element can have \n to separate lines
font: font to use
xini: initial x position
yini: initial y position
"""
def __init__(self, text, font, xini, yini):
pygame.sprite.Sprite.__init__(self)
self._font = font
self._text = text
self._ntexts = len(text)
self.current_text = 0
self.x = xini
self.y = yini
self.width = 0
self.height = 0
for t in self._text:
lines = t.split("\n")
lh = 0
for l in lines:
(w, h) = self._font.size(l)
if w > self.width:
self.width = w
lh += h
if lh > self.height:
self.height = lh
self.width += 10
self.height += 10
self.bg = pygame.Surface((self.width, self.height))
self.bg.fill((255, 255, 255))
def update(self):
if self.current_text == self._ntexts:
return False
self.image = self.bg.copy()
yline = 5
for l in self._text[self.current_text].split("\n"):
text_img = self._font.render(l, 1, (0, 0, 0))
textrect = text_img.get_rect()
textrect.left = 5
textrect.top = yline
self.image.blit(text_img, textrect)
yline += self._font.get_height()
self.current_text += 1
return True
def render(self, screen):
screen.blit(self.image, (self.x, self.y))
def rect(self):
return (self.x, self.y, self.width, self.height)
def restart(self, rect):
self.x = rect[0] + int(rect[2]/2) - int(self.width/2)
self.y = rect[1] - self.height
self.current_text = 0
self.update()
if __name__ == "__main__":
print("You can't run this file.")
print("You have to import DialogSprite from another Python file.")
|