all repos — Legends-RPG @ 51f66118257991c2b8d4f5924f51cf796d98cf8a

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, 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 = dialogue_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 DialogueHandler(object):
 81    """Handles interaction between sprites to create dialogue boxes"""
 82
 83    def __init__(self, player, sprites, level_object):
 84        self.player = player
 85        self.sprites = sprites
 86        self.textbox = None
 87        self.level = level_object
 88        self.last_textbox_timer = 0.0
 89
 90
 91    def update(self, keys, current_time):
 92        """Checks for the creation of Dialogue boxes"""
 93        if keys[pg.K_SPACE] and not self.textbox:
 94            for sprite in self.sprites:
 95                if (current_time - self.last_textbox_timer) > 300:
 96                    self.check_for_dialogue(sprite)
 97
 98        if self.textbox:
 99            self.textbox.update(current_time, keys)
100
101            if self.textbox.done:
102                if self.textbox.index < (len(self.textbox.dialogue_list) - 1):
103                    index = self.textbox.index + 1
104                    dialogue = self.textbox.dialogue_list
105                    self.textbox = DialogueBox(400, dialogue, index)
106                else:
107                    self.level.state = 'normal'
108                    self.textbox = None
109                    self.last_textbox_timer = current_time
110                    self.reset_sprite_direction()
111
112
113    def check_for_dialogue(self, sprite):
114        """Checks if a sprite is in the correct location to give dialogue"""
115        player = self.player
116        tile_x, tile_y = player.location
117
118        if player.direction == 'up':
119            if sprite.location == [tile_x, tile_y - 1]:
120                self.textbox = DialogueBox(400, sprite.dialogue)
121                sprite.direction = 'down'
122        elif player.direction == 'down':
123            if sprite.location == [tile_x, tile_y + 1]:
124                self.textbox = DialogueBox(400, sprite.dialogue)
125                sprite.direction = 'up'
126        elif player.direction == 'left':
127            if sprite.location == [tile_x - 1, tile_y]:
128                self.textbox = DialogueBox(400, sprite.dialogue)
129                sprite.direction = 'right'
130        elif player.direction == 'right':
131            if sprite.location == [tile_x + 1, tile_y]:
132                self.textbox = DialogueBox(400, sprite.dialogue)
133                sprite.direction = 'left'
134
135
136    def reset_sprite_direction(self):
137        """Reset sprite to default direction"""
138        for sprite in self.sprites:
139            if sprite.state == 'resting':
140                sprite.direction = sprite.default_direction
141
142
143    def draw(self, surface):
144        """Draws textbox to surface"""
145        if self.textbox:
146            surface.blit(self.textbox.image, self.textbox.rect)
147
148
149
150
151
152
153