all repos — Legends-RPG @ 06000b5eb46fa12ffde316178a99cc020756b52f

A fantasy mini-RPG built with Python and Pygame.

data/components/textbox.py (view raw)

  1__author__ = 'justinarmstrong'
  2import pygame as pg
  3from .. import setup
  4from .. import constants as c
  5
  6class DialogueBox(object):
  7    """Text box used for dialogue"""
  8    def __init__(self, x, dialogue):
  9        self.bground = setup.GFX['dialoguebox']
 10        self.rect = self.bground.get_rect(centerx=x)
 11        self.image = pg.Surface(self.rect.size)
 12        self.image.set_colorkey(c.BLACK)
 13        self.image.blit(self.bground, (0, 0))
 14        self.timer = 0.0
 15        self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)
 16        self.dialogue_image = self.font.render(dialogue, False,  c.NEAR_BLACK)
 17        self.dialogue_rect = self.dialogue_image.get_rect(left=50, top=50)
 18        self.image.blit(self.dialogue_image, self.dialogue_rect)
 19        self.done = False
 20        self.arrow_image = setup.GFX['rightarrow']
 21        self.arrow_rect = self.arrow_image.get_rect(right=self.rect.right - 20,
 22                                                    bottom=self.rect.bottom - 10)
 23        self.image.blit(self.arrow_image, self.arrow_rect)
 24
 25    def update(self, current_time, keys):
 26        """Updates scrolling text"""
 27        self.current_time = current_time
 28        self.animate_dialogue()
 29        self.terminate_check(keys)
 30
 31
 32    def animate_dialogue(self):
 33        """Reveal dialogue on textbox"""
 34        text_image = self.dialogue_image
 35        text_rect = self.dialogue_rect
 36
 37        self.image.blit(text_image, text_rect)
 38
 39
 40    def terminate_check(self, keys):
 41        """Remove textbox from sprite group after 2 seconds"""
 42
 43        if self.timer == 0.0:
 44            self.timer = self.current_time
 45        elif (self.current_time - self.timer) > 300:
 46            if keys[pg.K_SPACE]:
 47                self.done = True
 48
 49
 50class DialogueHandler(object):
 51    """Handles interaction between sprites to create dialogue boxes"""
 52
 53    def __init__(self, player, sprites, level_object):
 54        self.player = player
 55        self.sprites = sprites
 56        self.textbox = None
 57        self.level = level_object
 58        self.last_textbox_timer = 0.0
 59
 60
 61    def update(self, keys, current_time):
 62        """Checks for the creation of Dialogue boxes"""
 63        if keys[pg.K_SPACE] and not self.textbox:
 64            for sprite in self.sprites:
 65                if (current_time - self.last_textbox_timer) > 300:
 66                    self.check_for_dialogue(sprite)
 67
 68        if self.textbox:
 69            self.level.state = 'dialogue'
 70            self.textbox.update(current_time, keys)
 71
 72            if self.textbox.done:
 73                self.level.state = 'normal'
 74                self.textbox = None
 75                self.last_textbox_timer = current_time
 76
 77
 78    def check_for_dialogue(self, sprite):
 79        """Checks if a sprite is in the correct location to give dialogue"""
 80        player = self.player
 81        tile_x, tile_y = player.location
 82
 83        if player.direction == 'up':
 84            if sprite.location == (tile_x, tile_y - 1):
 85                self.textbox = DialogueBox(400, sprite.dialogue)
 86        elif player.direction == 'down':
 87            if sprite.location == (tile_x, tile_y + 1):
 88                self.textbox = DialogueBox(400, sprite.dialogue)
 89        elif player.direction == 'left':
 90            if sprite.location == (tile_x - 1, tile_y):
 91                self.textbox = DialogueBox(400, sprite.dialogue)
 92        elif player.direction == 'right':
 93            if sprite.location == (tile_x + 1, tile_y):
 94                self.textbox = DialogueBox(400, sprite.dialogue)
 95
 96
 97    def draw(self, surface):
 98        """Draws textbox to surface"""
 99        if self.textbox:
100            surface.blit(self.textbox.image, self.textbox.rect)
101
102
103
104
105
106
107