all repos — Legends-RPG @ c0a1a80585351c8001637d8bf163dd13b033aaa7

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, observer, tools
  5from .. import constants as c
  6
  7
  8class NextArrow(pg.sprite.Sprite):
  9    """Flashing arrow indicating more dialogue"""
 10    def __init__(self):
 11        super(NextArrow, self).__init__()
 12        self.image = setup.GFX['fancyarrow']
 13        self.rect = self.image.get_rect(right=780,
 14                                        bottom=135)
 15
 16
 17class DialogueBox(object):
 18    """Text box used for dialogue"""
 19    def __init__(self, dialogue, index=0, image_key='dialoguebox', item=None):
 20        self.item = item
 21        self.bground = setup.GFX[image_key]
 22        self.rect = self.bground.get_rect(centerx=400)
 23        self.arrow_timer = 0.0
 24        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
 25        self.dialogue_list = dialogue
 26        self.index = index
 27        self.image = self.make_dialogue_box_image()
 28        self.arrow = NextArrow()
 29        self.check_to_draw_arrow()
 30        self.done = False
 31        self.allow_input = False
 32        self.name = image_key
 33        self.observers = [observer.SoundEffects()]
 34        self.notify = tools.notify_observers
 35        self.notify(self, c.CLICK)
 36
 37    def make_dialogue_box_image(self):
 38        """
 39        Make the image of the dialogue box.
 40        """
 41        image = pg.Surface(self.rect.size)
 42        image.set_colorkey(c.BLACK)
 43        image.blit(self.bground, (0, 0))
 44
 45        dialogue_image = self.font.render(self.dialogue_list[self.index],
 46                                          True,
 47                                          c.NEAR_BLACK)
 48        dialogue_rect = dialogue_image.get_rect(left=50, top=50)
 49        image.blit(dialogue_image, dialogue_rect)
 50
 51        return image
 52
 53    def update(self, keys, current_time):
 54        """Updates scrolling text"""
 55        self.current_time = current_time
 56        self.draw_box(current_time)
 57        self.terminate_check(keys)
 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    def terminate_check(self, keys):
 65        """Remove textbox from sprite group after 2 seconds"""
 66        if keys[pg.K_SPACE] and self.allow_input:
 67            self.done = True
 68
 69        if not keys[pg.K_SPACE]:
 70            self.allow_input = True
 71
 72    def check_to_draw_arrow(self):
 73        """
 74        Blink arrow if more text needs to be read.
 75        """
 76        if self.index < len(self.dialogue_list) - 1:
 77            self.image.blit(self.arrow.image, self.arrow.rect)
 78
 79
 80class TextHandler(object):
 81    """Handles interaction between sprites to create dialogue boxes"""
 82
 83    def __init__(self, level):
 84        self.player = level.player
 85        self.sprites = level.sprites
 86        self.talking_sprite = None
 87        self.textbox = None
 88        self.allow_input = False
 89        self.level = level
 90        self.last_textbox_timer = 0.0
 91        self.game_data = level.game_data
 92        self.observers = [observer.SoundEffects()]
 93        self.notify = tools.notify_observers
 94
 95    def update(self, keys, current_time):
 96        """Checks for the creation of Dialogue boxes"""
 97        if keys[pg.K_SPACE] and not self.textbox and self.allow_input:
 98            for sprite in self.sprites:
 99                if (current_time - self.last_textbox_timer) > 300:
100                    if self.player.state == 'resting':
101                        self.allow_input = False
102                        self.check_for_dialogue(sprite)
103
104        if self.textbox:
105            if self.talking_sprite.name == 'treasurechest':
106                self.open_chest(self.talking_sprite)
107
108            self.textbox.update(keys, current_time)
109
110            if self.textbox.done:
111
112                if self.textbox.index < (len(self.textbox.dialogue_list) - 1):
113                    index = self.textbox.index + 1
114                    dialogue = self.textbox.dialogue_list
115                    if self.textbox.name == 'dialoguebox':
116                        self.textbox = DialogueBox(dialogue, index)
117                    elif self.textbox.name == 'infobox':
118                        self.textbox = ItemBox(dialogue, index)
119                elif self.talking_sprite.item:
120                    self.check_for_item()
121                elif self.talking_sprite.battle:
122                    self.game_data['battle type'] = self.talking_sprite.battle
123                    self.end_dialogue(current_time)
124                    self.level.switch_to_battle = True
125                elif self.talking_sprite.name == 'oldmanbrother' and \
126                        self.game_data['talked to sick brother'] and \
127                        not self.game_data['has brother elixir']:
128                    self.talking_sprite.item = 'ELIXIR'
129                    self.game_data['has brother elixir'] = True
130                    self.check_for_item()
131                    dialogue = ['Hurry! There is precious little time.']
132                    self.talking_sprite.dialogue = dialogue
133                elif self.talking_sprite.name == 'oldman':
134                    if self.game_data['has brother elixir'] and \
135                            not self.game_data['elixir received']:
136                        del self.game_data['player inventory']['ELIXIR']
137                        self.game_data['elixir received'] = True
138                        dialogue = ['My good health is thanks to you.',
139                                    'I will be forever in your debt.']
140                        self.talking_sprite.dialogue = dialogue
141                    elif not self.game_data['talked to sick brother']:
142                        self.game_data['talked to sick brother'] = True
143                         
144                        dialogue = ['Hurry to the NorthEast Shores!',
145                                    'I do not have much time left.']
146                        self.talking_sprite.dialogue = dialogue
147                    else:
148                        self.end_dialogue(current_time)
149                elif self.talking_sprite.name == 'king':
150                     
151                    if not self.game_data['talked to king']:
152                        self.game_data['talked to king'] = True
153                        new_dialogue = ['Hurry to the castle in the NorthWest!',
154                                        'The sorceror who lives there has my crown.',
155                                        'Please retrieve it for me.']
156                        self.talking_sprite.dialogue = new_dialogue
157                        self.end_dialogue(current_time)
158                    elif self.game_data['crown quest']:
159                        self.game_data['delivered crown'] = True
160                        self.end_dialogue(current_time)
161                    else:
162                        self.end_dialogue(current_time)
163                else:
164                    self.end_dialogue(current_time)
165
166
167        if not keys[pg.K_SPACE]:
168            self.allow_input = True
169
170    def end_dialogue(self, current_time):
171        """
172        End dialogue state for level.
173        """
174        self.talking_sprite = None
175        self.level.state = 'normal'
176        self.textbox = None
177        self.last_textbox_timer = current_time
178        self.reset_sprite_direction()
179        self.notify(self, c.CLICK)
180
181    def check_for_dialogue(self, sprite):
182        """Checks if a sprite is in the correct location to give dialogue"""
183        player = self.player
184        tile_x, tile_y = player.location
185
186        if player.direction == 'up':
187            if sprite.location == [tile_x, tile_y - 1]:
188                self.textbox = DialogueBox(sprite.dialogue)
189                sprite.direction = 'down'
190                self.talking_sprite = sprite
191        elif player.direction == 'down':
192            if sprite.location == [tile_x, tile_y + 1]:
193                self.textbox = DialogueBox(sprite.dialogue)
194                sprite.direction = 'up'
195                self.talking_sprite = sprite
196        elif player.direction == 'left':
197            if sprite.location == [tile_x - 1, tile_y]:
198                self.textbox = DialogueBox(sprite.dialogue)
199                sprite.direction = 'right'
200                self.talking_sprite = sprite
201        elif player.direction == 'right':
202            if sprite.location == [tile_x + 1, tile_y]:
203                self.textbox = DialogueBox(sprite.dialogue)
204                sprite.direction = 'left'
205                self.talking_sprite = sprite
206
207    def check_for_item(self):
208        """Checks if sprite has an item to give to the player"""
209        item = self.talking_sprite.item
210
211        if item:
212            if item in self.game_data['player inventory']:
213                if 'quantity' in self.game_data['player inventory'][item]:
214                    if item == 'GOLD':
215                        self.game_data['player inventory'][item]['quantity'] += 100
216                    else:
217                        self.game_data['player inventory'][item]['quantity'] += 1
218            else:
219                self.add_new_item_to_inventory(item)
220
221            self.update_game_items_info(self.talking_sprite)
222            self.talking_sprite.item = None
223
224            if self.talking_sprite.name == 'treasurechest':
225                self.talking_sprite.dialogue = ['Empty.']
226
227            if item == 'ELIXIR':
228                self.game_data['has brother elixir'] = True
229                self.game_data['old man gift'] = 'Fire Blast'
230                dialogue = ['Hurry! There is precious little time.']
231                self.level.reset_dialogue = self.talking_sprite, dialogue
232
233    def add_new_item_to_inventory(self, item):
234        inventory = self.game_data['player inventory']
235        potions = ['Healing Potion', 'Ether Potion']
236        if item in potions:
237            inventory[item] = dict([('quantity',1),
238                                    ('value',15)])
239        elif item == 'ELIXIR':
240            inventory[item] = dict([('quantity',1)])
241        elif item == 'Fire Blast':
242            inventory[item] = dict([('magic points', 40),
243                                    ('power', 15)])
244        else:
245            pass
246
247    def update_game_items_info(self, sprite):
248        if sprite.name == 'treasurechest':
249            self.game_data['treasure{}'.format(sprite.id)] = False
250        elif sprite.name == 'oldmanbrother':
251            self.game_data['brother elixir'] = False
252
253    def reset_sprite_direction(self):
254        """Reset sprite to default direction"""
255        for sprite in self.sprites:
256            if sprite.state == 'resting':
257                sprite.direction = sprite.default_direction
258
259
260    def draw(self, surface):
261        """Draws textbox to surface"""
262        if self.textbox:
263            surface.blit(self.textbox.image, self.textbox.rect)
264
265
266    def make_textbox(self, name, dialogue, item=None):
267        """Make textbox on demand"""
268        if name == 'itembox':
269            textbox = ItemBox(dialogue, item)
270        elif name == 'dialoguebox':
271            textbox = DialogueBox(dialogue)
272        else:
273            textbox = None
274
275        return textbox
276
277    def open_chest(self, sprite):
278        if sprite.name == 'treasurechest':
279            sprite.index = 1
280