data/components/textbox.py (view raw)
1__author__ = 'justinarmstrong'
2import pygame as pg
3from .. import setup
4from .. import constants as c
5
6class Dialogue(object):
7 """Text box used for dialogue"""
8 def __init__(self, x, dialogue):
9 self.bground = setup.GFX['dialoguebox']
10 self.rect = self.bground.get_rect(centerx=x)
11 self.image = pg.Surface(self.rect.size)
12 self.image.set_colorkey(c.BLACK)
13 self.image.blit(self.bground, (0, 0))
14 self.timer = 0.0
15 self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)
16 self.dialogue_image = self.font.render(dialogue, False, c.NEAR_BLACK)
17 self.dialogue_rect = self.dialogue_image.get_rect(left=50, top=50)
18 self.image.blit(self.dialogue_image, self.dialogue_rect)
19 self.done = False
20
21 def update(self, current_time):
22 """Updates scrolling text"""
23 self.current_time = current_time
24 self.animate_dialogue()
25 self.terminate_check()
26
27
28 def animate_dialogue(self):
29 """Reveal dialogue on textbox"""
30 text_image = self.dialogue_image
31 text_rect = self.dialogue_rect
32
33 self.image.blit(text_image, text_rect)
34
35
36 def terminate_check(self):
37 """Remove textbox from sprite group after 2 seconds"""
38
39 if self.timer == 0.0:
40 self.timer = self.current_time
41 elif (self.current_time - self.timer) > 3000:
42 self.done = True
43
44
45class DialogueHandler(object):
46 """Handles interaction between sprites to create dialogue boxes"""
47
48 def __init__(self, player, sprites):
49 self.player = player
50 self.sprites = sprites
51 self.textbox = None
52
53
54 def update(self, keys, current_time):
55 """Checks for the creation of Dialogue boxes"""
56 if keys[pg.K_SPACE] and not self.textbox:
57 for sprite in self.sprites:
58 self.check_for_dialogue(sprite)
59
60 if self.textbox:
61 self.textbox.update(current_time)
62
63 if self.textbox.done:
64 self.textbox = None
65
66
67 def check_for_dialogue(self, sprite):
68 """Checks if a sprite is in the correct location to give dialogue"""
69 player = self.player
70 tile_x, tile_y = player.location
71
72 if player.direction == 'up':
73 if sprite.location == (tile_x, tile_y - 1):
74 self.textbox = Dialogue(400, sprite.dialogue)
75 elif player.direction == 'down':
76 if sprite.location == (tile_x, tile_y + 1):
77 self.textbox = Dialogue(400, sprite.dialogue)
78 elif player.direction == 'left':
79 if sprite.location == (tile_x - 1, tile_y):
80 self.textbox = Dialogue(400, sprite.dialogue)
81 elif player.direction == 'right':
82 if sprite.location == (tile_x + 1, tile_y):
83 self.textbox = Dialogue(400, sprite.dialogue)
84
85
86 def draw(self, surface):
87 """Draws textbox to surface"""
88 if self.textbox:
89 surface.blit(self.textbox.image, self.textbox.rect)
90
91
92
93
94
95
96