data/components/textbox.py (view raw)
1__author__ = 'justinarmstrong'
2import copy
3import pygame as pg
4from .. import setup
5from .. import constants as c
6
7
8
9class NextArrow(pg.sprite.Sprite):
10 """Flashing arrow indicating more dialogue"""
11 def __init__(self):
12 super(NextArrow, self).__init__()
13 self.image = setup.GFX['fancyarrow']
14 self.rect = self.image.get_rect(right=780,
15 bottom=135)
16
17
18class DialogueBox(object):
19 """Text box used for dialogue"""
20 def __init__(self, dialogue, index=0, image_key='dialoguebox'):
21 self.bground = setup.GFX[image_key]
22 self.rect = self.bground.get_rect(centerx=400)
23 self.timer = 0.0
24 self.arrow_timer = 0.0
25 self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)
26 self.dialogue_list = dialogue
27 self.index = index
28 self.image = self.make_dialogue_box_image()
29 self.arrow = NextArrow()
30 self.check_to_draw_arrow()
31 self.done = False
32 self.name = image_key
33
34
35 def make_dialogue_box_image(self):
36 """Make the image of the dialogue box"""
37 image = pg.Surface(self.rect.size)
38 image.set_colorkey(c.BLACK)
39 image.blit(self.bground, (0, 0))
40
41 dialogue_image = self.font.render(self.dialogue_list[self.index],
42 False,
43 c.NEAR_BLACK)
44 dialogue_rect = dialogue_image.get_rect(left=50, top=50)
45 image.blit(dialogue_image, dialogue_rect)
46
47 return image
48
49
50
51 def update(self, current_time, keys):
52 """Updates scrolling text"""
53 self.current_time = current_time
54 self.draw_box(current_time)
55 self.terminate_check(keys)
56
57
58 def draw_box(self, current_time, x=400):
59 """Reveal dialogue on textbox"""
60 self.image = self.make_dialogue_box_image()
61 self.check_to_draw_arrow()
62
63
64 def terminate_check(self, keys):
65 """Remove textbox from sprite group after 2 seconds"""
66
67 if self.timer == 0.0:
68 self.timer = self.current_time
69 elif (self.current_time - self.timer) > 300:
70 if keys[pg.K_SPACE]:
71 self.done = True
72
73
74 def check_to_draw_arrow(self):
75 """Blink arrow if more text needs to be read"""
76 if self.index < len(self.dialogue_list) - 1:
77 self.image.blit(self.arrow.image, self.arrow.rect)
78 else:
79 pass
80
81
82class ItemBox(DialogueBox):
83 """Text box for information like obtaining new items"""
84 def __init__(self, dialogue, index=0, image_key='infobox'):
85 super(ItemBox, self).__init__(dialogue, index, image_key)
86
87
88 def make_dialogue_box_image(self):
89 """Make the image of the dialogue box"""
90 image = pg.Surface(self.rect.size)
91 image.set_colorkey(c.BLACK)
92 image.blit(self.bground, (0, 0))
93
94 dialogue = 'You received ' + self.dialogue_list[self.index] + '.'
95
96 dialogue_image = self.font.render(dialogue,
97 False,
98 c.NEAR_BLACK)
99 dialogue_rect = dialogue_image.get_rect(left=50, top=50)
100 image.blit(dialogue_image, dialogue_rect)
101
102 return image
103
104
105
106class DialogueHandler(object):
107 """Handles interaction between sprites to create dialogue boxes"""
108
109 def __init__(self, player, sprites, level_object):
110 self.player = player
111 self.sprites = sprites
112 self.talking_sprite = None
113 self.textbox = None
114 self.level = level_object
115 self.last_textbox_timer = 0.0
116 self.game_data = level_object.persist
117
118
119 def update(self, keys, current_time):
120 """Checks for the creation of Dialogue boxes"""
121 if keys[pg.K_SPACE] and not self.textbox:
122 for sprite in self.sprites:
123 if (current_time - self.last_textbox_timer) > 300:
124 self.check_for_dialogue(sprite)
125
126 if self.textbox:
127 self.textbox.update(current_time, keys)
128
129 if self.textbox.done:
130
131 if self.textbox.index < (len(self.textbox.dialogue_list) - 1):
132 index = self.textbox.index + 1
133 dialogue = self.textbox.dialogue_list
134 if self.textbox.name == 'dialoguebox':
135 self.textbox = DialogueBox(dialogue, index)
136 elif self.textbox.name == 'infobox':
137 self.textbox = ItemBox(dialogue, index)
138 elif self.talking_sprite.item:
139 item = self.check_for_item()
140 self.textbox = ItemBox(item, 0)
141 else:
142 self.level.state = 'normal'
143 self.textbox = None
144 self.last_textbox_timer = current_time
145 self.reset_sprite_direction()
146
147
148 def check_for_dialogue(self, sprite):
149 """Checks if a sprite is in the correct location to give dialogue"""
150 player = self.player
151 tile_x, tile_y = player.location
152
153 if player.direction == 'up':
154 if sprite.location == [tile_x, tile_y - 1]:
155 self.textbox = DialogueBox(sprite.dialogue)
156 sprite.direction = 'down'
157 self.talking_sprite = sprite
158 elif player.direction == 'down':
159 if sprite.location == [tile_x, tile_y + 1]:
160 self.textbox = DialogueBox(sprite.dialogue)
161 sprite.direction = 'up'
162 self.talking_sprite = sprite
163 elif player.direction == 'left':
164 if sprite.location == [tile_x - 1, tile_y]:
165 self.textbox = DialogueBox(sprite.dialogue)
166 sprite.direction = 'right'
167 self.talking_sprite = sprite
168 elif player.direction == 'right':
169 if sprite.location == [tile_x + 1, tile_y]:
170 self.textbox = DialogueBox(sprite.dialogue)
171 sprite.direction = 'left'
172 self.talking_sprite = sprite
173
174
175 def check_for_item(self):
176 """Checks if sprite has an item to give to the player"""
177 item = self.talking_sprite.item
178 if item:
179 self.player.item_list.append(item)
180 self.talking_sprite.item = None
181 if self.talking_sprite.name == 'king':
182 self.game_data['king item'] = None
183
184 return item
185
186
187
188 def reset_sprite_direction(self):
189 """Reset sprite to default direction"""
190 for sprite in self.sprites:
191 if sprite.state == 'resting':
192 sprite.direction = sprite.default_direction
193
194
195 def draw(self, surface):
196 """Draws textbox to surface"""
197 if self.textbox:
198 surface.blit(self.textbox.image, self.textbox.rect)
199
200
201
202
203
204
205