all repos — Legends-RPG @ 3bf8b0eba9dc6f70d6792f2a41121414df24a7ca

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, x, dialogue, index=0):
 21        self.bground = setup.GFX['dialoguebox']
 22        self.rect = self.bground.get_rect(centerx=x)
 23        self.image = pg.Surface(self.rect.size)
 24        self.image.set_colorkey(c.BLACK)
 25        self.image.blit(self.bground, (0, 0))
 26        self.timer = 0.0
 27        self.arrow_timer = 0.0
 28        self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)
 29        self.dialogue_list = dialogue
 30        self.index = index
 31        self.dialogue_image = self.font.render(dialogue[self.index],
 32                                               False,
 33                                               c.NEAR_BLACK)
 34        self.dialogue_rect = self.dialogue_image.get_rect(left=50, top=50)
 35        self.image.blit(self.dialogue_image, self.dialogue_rect)
 36        self.arrow = NextArrow()
 37        self.check_to_draw_arrow()
 38        self.done = False
 39
 40
 41    def update(self, current_time, keys):
 42        """Updates scrolling text"""
 43        self.current_time = current_time
 44        self.draw_box(current_time)
 45        self.terminate_check(keys)
 46
 47
 48    def draw_box(self, current_time, x=400):
 49        """Reveal dialogue on textbox"""
 50        bground = setup.GFX['dialoguebox']
 51        rect = bground.get_rect(centerx=x)
 52        text_image = self.dialogue_image
 53        text_rect = self.dialogue_rect
 54        self.image = pg.Surface(rect.size)
 55        self.image.set_colorkey(c.BLACK)
 56
 57        self.image.blit(bground, (0, 0))
 58        self.image.blit(text_image, text_rect)
 59        self.check_to_draw_arrow()
 60
 61
 62    def terminate_check(self, keys):
 63        """Remove textbox from sprite group after 2 seconds"""
 64
 65        if self.timer == 0.0:
 66            self.timer = self.current_time
 67        elif (self.current_time - self.timer) > 300:
 68            if keys[pg.K_SPACE]:
 69                self.done = True
 70
 71
 72    def check_to_draw_arrow(self):
 73        """Blink arrow if more text needs to be read"""
 74        if self.index < len(self.dialogue_list) - 1:
 75            self.image.blit(self.arrow.image, self.arrow.rect)
 76        else:
 77            pass
 78
 79
 80class InfoBox(DialogueBox):
 81    """Text box for information like obtaining new items"""
 82    def __init__(self, x, dialogue):
 83        super(InfoBox, self).__init__(x, dialogue)
 84        self.image = setup.GFX['infobox']
 85
 86
 87class DialogueHandler(object):
 88    """Handles interaction between sprites to create dialogue boxes"""
 89
 90    def __init__(self, player, sprites, level_object):
 91        self.player = player
 92        self.sprites = sprites
 93        self.textbox = None
 94        self.level = level_object
 95        self.last_textbox_timer = 0.0
 96
 97
 98    def update(self, keys, current_time):
 99        """Checks for the creation of Dialogue boxes"""
100        if keys[pg.K_SPACE] and not self.textbox:
101            for sprite in self.sprites:
102                if (current_time - self.last_textbox_timer) > 300:
103                    self.check_for_dialogue(sprite)
104
105        if self.textbox:
106            self.textbox.update(current_time, keys)
107
108            if self.textbox.done:
109                if self.textbox.index < (len(self.textbox.dialogue_list) - 1):
110                    index = self.textbox.index + 1
111                    dialogue = self.textbox.dialogue_list
112                    self.textbox = DialogueBox(400, dialogue, index)
113                else:
114                    self.level.state = 'normal'
115                    self.textbox = None
116                    self.last_textbox_timer = current_time
117                    self.reset_sprite_direction()
118
119
120    def check_for_dialogue(self, sprite):
121        """Checks if a sprite is in the correct location to give dialogue"""
122        player = self.player
123        tile_x, tile_y = player.location
124
125        if player.direction == 'up':
126            if sprite.location == [tile_x, tile_y - 1]:
127                self.textbox = DialogueBox(400, sprite.dialogue)
128                sprite.direction = 'down'
129        elif player.direction == 'down':
130            if sprite.location == [tile_x, tile_y + 1]:
131                self.textbox = DialogueBox(400, sprite.dialogue)
132                sprite.direction = 'up'
133        elif player.direction == 'left':
134            if sprite.location == [tile_x - 1, tile_y]:
135                self.textbox = DialogueBox(400, sprite.dialogue)
136                sprite.direction = 'right'
137        elif player.direction == 'right':
138            if sprite.location == [tile_x + 1, tile_y]:
139                self.textbox = DialogueBox(400, sprite.dialogue)
140                sprite.direction = 'left'
141
142
143    def reset_sprite_direction(self):
144        """Reset sprite to default direction"""
145        for sprite in self.sprites:
146            if sprite.state == 'resting':
147                sprite.direction = sprite.default_direction
148
149
150    def draw(self, surface):
151        """Draws textbox to surface"""
152        if self.textbox:
153            surface.blit(self.textbox.image, self.textbox.rect)
154
155
156
157
158
159
160