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