all repos — Legends-RPG @ 042b912afd55ebeecab540dc4a44dd070f886b6a

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