DialogBox(window, npc: str, name: str, texts: list, talkType: TalkType, fps: int = 30, fontSize: int = DialogSettings.textSize, fontColor: Tuple[int, int, int] = (255, 255, 255), bgColor: Tuple[int, int, int, int] = (0, 0, 0, 255))
Dialog Box with npc showed on the left bottom side of the window
Initialize the Dialog Box
:param window: pygame.Surface
:param npc: str, Path to the resource of npc
:param texts: list, list of text
:param fps: int, fps of the animation
:param talkType: TalkType, type of the dialog
:param fontSize: int
:param fontColor: Tuple[int, int, int]
:param bgColor: Tuple[int, int, int, int]
Source code in Tairitsu/PopUpBox.py
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 | def __init__(self, window, npc: str, name: str, texts: list, talkType: TalkType, fps: int = 30,
fontSize: int = DialogSettings.textSize,
fontColor: Tuple[int, int, int] = (255, 255, 255),
bgColor: Tuple[int, int, int, int] = (0, 0, 0, 255)):
"""
Initialize the Dialog Box
:param window: pygame.Surface
:param npc: str, Path to the resource of npc
:param texts: list, list of text
:param fps: int, fps of the animation
:param talkType: TalkType, type of the dialog
:param fontSize: int
:param fontColor: Tuple[int, int, int]
:param bgColor: Tuple[int, int, int, int]
"""
self.window = window
self.texts = texts
self.fps = fps
self.talkType = talkType
self.name = name
self.fontd = fontSize
self.fontColor = fontColor
# self.font = pygame.font.Font(None, self.fontSize)
self.font = pygame.font.Font(GlobalSettings.fontPath, fontSize)
# self.font.set_bold(True)
self.bg = pygame.Surface((DialogSettings.boxWidth,
DialogSettings.boxHeight), pygame.SRCALPHA)
self.bg.fill(bgColor)
self.npc = pygame.image.load(npc)
self.npc = pygame.transform.scale(self.npc, (DialogSettings.npcWidth,
DialogSettings.npcHeight))
|
bg = pygame.Surface((DialogSettings.boxWidth, DialogSettings.boxHeight), pygame.SRCALPHA)
font = pygame.font.Font(GlobalSettings.fontPath, fontSize)
npc = pygame.transform.scale(self.npc, (DialogSettings.npcWidth, DialogSettings.npcHeight))
Source code in Tairitsu/PopUpBox.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 | def draw(self):
if(self.talkType == TalkType.ENDED):
self.fps = -1
self.window.blit(self.bg, (DialogSettings.boxStartX,
DialogSettings.boxStartY))
self.window.blit(self.npc, (DialogSettings.npcCoordX,
DialogSettings.npcCoordY))
self.window.blit(self.font.render(self.name, True,
self.fontColor), (DialogSettings.npcCoordX,
DialogSettings.npcCoordY - DialogSettings.textVerticalDist * 1.5))
offset = 0
clock = pygame.time.Clock()
self.talkType = TalkType.TALKING
for text in self.texts:
# self.window.blit(self.font
# .render(text,
# True, self.fontColor),
# (DialogSettings.textStartX, DialogSettings.textStartY + offset))
# offset += DialogSettings.textVerticalDist * 1.5
# Initialize the offset and height offset
hoffset = 0
_, text_height = self.font.size(text)
# print("Start at" + str(star) , text)
if((offset + text_height > DialogSettings.boxHeight * 0.95) and text != ""):
offset = 0
# clock.tick(1)
# self.talkType = TalkType.WAITING
# flag = True
# while flag:
# # clock.tick(30)
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# running = False
# elif event.type == pygame.KEYDOWN:
# flag = False
# self.window.blit(self.bg, (DialogSettings.boxStartX,
# DialogSettings.boxStartY))
# break
# self.talkType = TalkType.TALKING
# Start to talk by word
# if(text == "\n"): continue
for word in text:
word_width, _ = self.font.size(word)
if(word_width + hoffset > DialogSettings.boxWidth * 0.95):
offset += DialogSettings.textVerticalDist * 0.9
hoffset = 0
self.window.blit(self.font.render(word, True,
self.fontColor), (DialogSettings.textStartX + hoffset,
DialogSettings.textStartY + offset))
hoffset += word_width
if(self.talkType == TalkType.TALKING and self.fps != -1):
pygame.display.flip()
SoundFX(GamePath.sound["Speak"])
clock.tick(30)
offset += DialogSettings.textVerticalDist * 0.9
self.talkType = TalkType.ENDED
|