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, x, dialogue, dialogue_index=0):
21 self.bground = setup.GFX['dialoguebox']
22 self.rect = self.bground.get_rect(centerx=x)
23 self.image = pg.Surface(self.rect.size)
24 self.image.set_colorkey(c.BLACK)
25 self.image.blit(self.bground, (0, 0))
26 self.timer = 0.0
27 self.arrow_timer = 0.0
28 self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)
29 self.dialogue_list = dialogue
30 self.index = dialogue_index
31 self.dialogue_image = self.font.render(dialogue[self.index],
32 False,
33 c.NEAR_BLACK)
34 self.dialogue_rect = self.dialogue_image.get_rect(left=50, top=50)
35 self.image.blit(self.dialogue_image, self.dialogue_rect)
36 self.arrow = NextArrow()
37 self.check_to_draw_arrow()
38 self.done = False
39
40
41 def update(self, current_time, keys):
42 """Updates scrolling text"""
43 self.current_time = current_time
44 self.draw_box(current_time)
45 self.terminate_check(keys)
46
47
48 def draw_box(self, current_time, x=400):
49 """Reveal dialogue on textbox"""
50 bground = setup.GFX['dialoguebox']
51 rect = bground.get_rect(centerx=x)
52 text_image = self.dialogue_image
53 text_rect = self.dialogue_rect
54 self.image = pg.Surface(rect.size)
55 self.image.set_colorkey(c.BLACK)
56
57 self.image.blit(bground, (0, 0))
58 self.image.blit(text_image, text_rect)
59 self.check_to_draw_arrow()
60
61
62 def terminate_check(self, keys):
63 """Remove textbox from sprite group after 2 seconds"""
64
65 if self.timer == 0.0:
66 self.timer = self.current_time
67 elif (self.current_time - self.timer) > 300:
68 if keys[pg.K_SPACE]:
69 self.done = True
70
71
72 def check_to_draw_arrow(self):
73 """Blink arrow if more text needs to be read"""
74 if self.index < len(self.dialogue_list) - 1:
75 self.image.blit(self.arrow.image, self.arrow.rect)
76 else:
77 pass
78
79
80class DialogueHandler(object):
81 """Handles interaction between sprites to create dialogue boxes"""
82
83 def __init__(self, player, sprites, level_object):
84 self.player = player
85 self.sprites = sprites
86 self.textbox = None
87 self.level = level_object
88 self.last_textbox_timer = 0.0
89
90
91 def update(self, keys, current_time):
92 """Checks for the creation of Dialogue boxes"""
93 if keys[pg.K_SPACE] and not self.textbox:
94 for sprite in self.sprites:
95 if (current_time - self.last_textbox_timer) > 300:
96 self.check_for_dialogue(sprite)
97
98 if self.textbox:
99 self.textbox.update(current_time, keys)
100
101 if self.textbox.done:
102 if self.textbox.index < (len(self.textbox.dialogue_list) - 1):
103 index = self.textbox.index + 1
104 dialogue = self.textbox.dialogue_list
105 self.textbox = DialogueBox(400, dialogue, index)
106 else:
107 self.level.state = 'normal'
108 self.textbox = None
109 self.last_textbox_timer = current_time
110
111
112 def check_for_dialogue(self, sprite):
113 """Checks if a sprite is in the correct location to give dialogue"""
114 player = self.player
115 tile_x, tile_y = player.location
116
117 if player.direction == 'up':
118 if sprite.location == (tile_x, tile_y - 1):
119 self.textbox = DialogueBox(400, sprite.dialogue)
120 sprite.direction = 'down'
121 elif player.direction == 'down':
122 if sprite.location == (tile_x, tile_y + 1):
123 self.textbox = DialogueBox(400, sprite.dialogue)
124 sprite.direction = 'up'
125 elif player.direction == 'left':
126 if sprite.location == (tile_x - 1, tile_y):
127 self.textbox = DialogueBox(400, sprite.dialogue)
128 sprite.direction = 'right'
129 elif player.direction == 'right':
130 if sprite.location == (tile_x + 1, tile_y):
131 self.textbox = DialogueBox(400, sprite.dialogue)
132 sprite.direction = 'left'
133
134
135 def draw(self, surface):
136 """Draws textbox to surface"""
137 if self.textbox:
138 surface.blit(self.textbox.image, self.textbox.rect)
139
140
141
142
143
144
145