all repos — Legends-RPG @ fcf0f9b3eaa3beed201cf7b7e1b9b147f6c0ef7b

A fantasy mini-RPG built with Python and Pygame.

data/components/textbox.py (view raw)

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