all repos — Legends-RPG @ b5733447a499f57ee5e055cff73e18dbfac21354

A fantasy mini-RPG built with Python and Pygame.

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', item=None):
 21        self.item = item
 22        self.bground = setup.GFX[image_key]
 23        self.rect = self.bground.get_rect(centerx=400)
 24        self.arrow_timer = 0.0
 25        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 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.allow_input = False
 33        self.name = image_key
 34
 35
 36    def make_dialogue_box_image(self):
 37        """Make the image of the dialogue box"""
 38        image = pg.Surface(self.rect.size)
 39        image.set_colorkey(c.BLACK)
 40        image.blit(self.bground, (0, 0))
 41
 42        dialogue_image = self.font.render(self.dialogue_list[self.index],
 43                                          True,
 44                                          c.NEAR_BLACK)
 45        dialogue_rect = dialogue_image.get_rect(left=50, top=50)
 46        image.blit(dialogue_image, dialogue_rect)
 47
 48        return image
 49
 50    def update(self, keys, current_time):
 51        """Updates scrolling text"""
 52        self.current_time = current_time
 53        self.draw_box(current_time)
 54        self.terminate_check(keys)
 55
 56    def draw_box(self, current_time, x=400):
 57        """Reveal dialogue on textbox"""
 58        self.image = self.make_dialogue_box_image()
 59        self.check_to_draw_arrow()
 60
 61    def terminate_check(self, keys):
 62        """Remove textbox from sprite group after 2 seconds"""
 63        if keys[pg.K_SPACE] and self.allow_input:
 64            self.done = True
 65
 66        if not keys[pg.K_SPACE]:
 67            self.allow_input = True
 68
 69    def check_to_draw_arrow(self):
 70        """Blink arrow if more text needs to be read"""
 71        if self.index < len(self.dialogue_list) - 1:
 72            self.image.blit(self.arrow.image, self.arrow.rect)
 73        else:
 74            pass
 75
 76
 77class ItemBox(DialogueBox):
 78    """Text box for information like obtaining new items"""
 79    def __init__(self, dialogue, item=None):
 80        super(ItemBox, self).__init__(None, 0, 'infobox', item)
 81
 82    def make_dialogue_box_image(self):
 83        """Make the image of the dialogue box"""
 84        image = pg.Surface(self.rect.size)
 85        image.set_colorkey(c.BLACK)
 86        image.blit(self.bground, (0, 0))
 87
 88        if self.item:
 89            type = list(self.item.keys())[0]
 90            total = str(self.item[type]['quantity'])
 91            dialogue = 'You received ' + total + ' ' + type + '.'
 92            self.dialogue_list = [dialogue]
 93            self.item = None
 94
 95        dialogue_image = self.font.render(self.dialogue_list[self.index],
 96                                          False,
 97                                          c.NEAR_BLACK)
 98        dialogue_rect = dialogue_image.get_rect(left=50, top=50)
 99        image.blit(dialogue_image, dialogue_rect)
100
101        return image
102
103
104
105class TextHandler(object):
106    """Handles interaction between sprites to create dialogue boxes"""
107
108    def __init__(self, level):
109        self.player = level.player
110        self.sprites = level.sprites
111        self.talking_sprite = None
112        self.textbox = None
113        self.allow_input = False
114        self.level = level
115        self.last_textbox_timer = 0.0
116        self.game_data = level.game_data
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 and self.allow_input:
122            for sprite in self.sprites:
123                if (current_time - self.last_textbox_timer) > 300:
124                    if self.player.state == 'resting':
125                        self.allow_input = False
126                        self.check_for_dialogue(sprite)
127
128        if self.textbox:
129            self.textbox.update(keys, current_time)
130
131            if self.textbox.done:
132
133                if self.textbox.index < (len(self.textbox.dialogue_list) - 1):
134                    index = self.textbox.index + 1
135                    dialogue = self.textbox.dialogue_list
136                    if self.textbox.name == 'dialoguebox':
137                        self.textbox = DialogueBox(dialogue, index)
138                    elif self.textbox.name == 'infobox':
139                        self.textbox = ItemBox(dialogue, index)
140                elif self.talking_sprite.item:
141                    item = self.check_for_item()
142                    self.textbox = ItemBox(None, item)
143                else:
144                    self.level.state = 'normal'
145                    self.textbox = None
146                    self.last_textbox_timer = current_time
147                    self.reset_sprite_direction()
148
149        if not keys[pg.K_SPACE]:
150            self.allow_input = True
151
152
153    def check_for_dialogue(self, sprite):
154        """Checks if a sprite is in the correct location to give dialogue"""
155        player = self.player
156        tile_x, tile_y = player.location
157
158        if player.direction == 'up':
159            if sprite.location == [tile_x, tile_y - 1]:
160                self.textbox = DialogueBox(sprite.dialogue)
161                sprite.direction = 'down'
162                self.talking_sprite = sprite
163        elif player.direction == 'down':
164            if sprite.location == [tile_x, tile_y + 1]:
165                self.textbox = DialogueBox(sprite.dialogue)
166                sprite.direction = 'up'
167                self.talking_sprite = sprite
168        elif player.direction == 'left':
169            if sprite.location == [tile_x - 1, tile_y]:
170                self.textbox = DialogueBox(sprite.dialogue)
171                sprite.direction = 'right'
172                self.talking_sprite = sprite
173        elif player.direction == 'right':
174            if sprite.location == [tile_x + 1, tile_y]:
175                self.textbox = DialogueBox(sprite.dialogue)
176                sprite.direction = 'left'
177                self.talking_sprite = sprite
178
179
180    def check_for_item(self):
181        """Checks if sprite has an item to give to the player"""
182        item = self.talking_sprite.item
183        type = list(item.keys())[0]
184        quantity = item[type]['quantity']
185        value = item[type]['value']
186
187        if item:
188            if type in self.game_data['player inventory']:
189                self.game_data['player inventory'][type]['quantity'] += item[type]['quantity']
190            else:
191                self.game_data['player inventory'][type] = {'quantity': quantity,
192                                                            'value': value}
193
194            self.talking_sprite.item = None
195
196            if self.talking_sprite.name == 'king':
197                self.game_data['king item'] = None
198            elif self.talking_sprite.name == 'oldmanbrother':
199                self.game_data['old man item'] = None
200
201        return item
202
203
204
205    def reset_sprite_direction(self):
206        """Reset sprite to default direction"""
207        for sprite in self.sprites:
208            if sprite.state == 'resting':
209                sprite.direction = sprite.default_direction
210
211
212    def draw(self, surface):
213        """Draws textbox to surface"""
214        if self.textbox:
215            surface.blit(self.textbox.image, self.textbox.rect)
216
217
218    def make_textbox(self, name, dialogue, item=None):
219        """Make textbox on demand"""
220        if name == 'itembox':
221            textbox = ItemBox(dialogue, item)
222        elif name == 'dialoguebox':
223            textbox = DialogueBox(dialogue)
224        else:
225            textbox = None
226
227        return textbox
228