Multiple textboxes per sprite work now. Arrow indicates more dialogue. Added additional dialogue to villager and soldier.
Justin Armstrong justinmeister@gmail.com
Sun, 16 Mar 2014 11:12:07 -0700
5 files changed,
71 insertions(+),
21 deletions(-)
M
data/components/person.py
→
data/components/person.py
@@ -29,6 +29,7 @@ self.current_time = 0.0
self.state = 'animated resting' self.blockers = self.set_blockers() self.location = self.get_tile_location() + self.dialogue = ['Placeholder Dialogue'] def create_spritesheet_dict(self, sheet_key):@@ -96,6 +97,7 @@ def update(self, current_time):
"""Implemented by inheriting classes""" self.blockers = self.set_blockers() self.current_time = current_time + self.image_list = self.animation_dict[self.direction] state_function = self.state_dict[self.state] state_function() self.location = self.get_tile_location()@@ -126,6 +128,7 @@ tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
blockers.extend([tile_rect1, tile_rect2]) return blockers + def get_tile_location(self):
M
data/components/textbox.py
→
data/components/textbox.py
@@ -1,40 +1,62 @@
__author__ = 'justinarmstrong' +import copy import pygame as pg from .. import setup from .. import constants as c + + +class NextArrow(pg.sprite.Sprite): + """Flashing arrow indicating more dialogue""" + def __init__(self): + super(NextArrow, self).__init__() + self.image = setup.GFX['fancyarrow'] + self.rect = self.image.get_rect(right=780, + bottom=135) + + class DialogueBox(object): """Text box used for dialogue""" - def __init__(self, x, dialogue): + def __init__(self, x, dialogue, dialogue_index=0): self.bground = setup.GFX['dialoguebox'] self.rect = self.bground.get_rect(centerx=x) self.image = pg.Surface(self.rect.size) self.image.set_colorkey(c.BLACK) self.image.blit(self.bground, (0, 0)) self.timer = 0.0 + self.arrow_timer = 0.0 self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22) - self.dialogue_image = self.font.render(dialogue, False, c.NEAR_BLACK) + self.dialogue_list = dialogue + self.index = dialogue_index + self.dialogue_image = self.font.render(dialogue[self.index], + False, + c.NEAR_BLACK) self.dialogue_rect = self.dialogue_image.get_rect(left=50, top=50) self.image.blit(self.dialogue_image, self.dialogue_rect) + self.arrow = NextArrow() + self.check_to_draw_arrow() self.done = False - self.arrow_image = setup.GFX['rightarrow'] - self.arrow_rect = self.arrow_image.get_rect(right=self.rect.right - 20, - bottom=self.rect.bottom - 10) - self.image.blit(self.arrow_image, self.arrow_rect) + def update(self, current_time, keys): """Updates scrolling text""" self.current_time = current_time - self.animate_dialogue() + self.draw_box(current_time) self.terminate_check(keys) - def animate_dialogue(self): + def draw_box(self, current_time, x=400): """Reveal dialogue on textbox""" + bground = setup.GFX['dialoguebox'] + rect = bground.get_rect(centerx=x) text_image = self.dialogue_image text_rect = self.dialogue_rect + self.image = pg.Surface(rect.size) + self.image.set_colorkey(c.BLACK) + self.image.blit(bground, (0, 0)) self.image.blit(text_image, text_rect) + self.check_to_draw_arrow() def terminate_check(self, keys):@@ -47,6 +69,14 @@ if keys[pg.K_SPACE]:
self.done = True + def check_to_draw_arrow(self): + """Blink arrow if more text needs to be read""" + if self.index < len(self.dialogue_list) - 1: + self.image.blit(self.arrow.image, self.arrow.rect) + else: + pass + + class DialogueHandler(object): """Handles interaction between sprites to create dialogue boxes"""@@ -66,13 +96,17 @@ if (current_time - self.last_textbox_timer) > 300:
self.check_for_dialogue(sprite) if self.textbox: - self.level.state = 'dialogue' self.textbox.update(current_time, keys) if self.textbox.done: - self.level.state = 'normal' - self.textbox = None - self.last_textbox_timer = current_time + if self.textbox.index < (len(self.textbox.dialogue_list) - 1): + index = self.textbox.index + 1 + dialogue = self.textbox.dialogue_list + self.textbox = DialogueBox(400, dialogue, index) + else: + self.level.state = 'normal' + self.textbox = None + self.last_textbox_timer = current_time def check_for_dialogue(self, sprite):@@ -83,15 +117,19 @@
if player.direction == 'up': if sprite.location == (tile_x, tile_y - 1): self.textbox = DialogueBox(400, sprite.dialogue) + sprite.direction = 'down' elif player.direction == 'down': if sprite.location == (tile_x, tile_y + 1): self.textbox = DialogueBox(400, sprite.dialogue) + sprite.direction = 'up' elif player.direction == 'left': if sprite.location == (tile_x - 1, tile_y): self.textbox = DialogueBox(400, sprite.dialogue) + sprite.direction = 'right' elif player.direction == 'right': if sprite.location == (tile_x + 1, tile_y): self.textbox = DialogueBox(400, sprite.dialogue) + sprite.direction = 'left' def draw(self, surface):
M
data/states/town.py
→
data/states/town.py
@@ -40,18 +40,23 @@ def set_sprite_dialogue(self):
"""Sets unique dialogue for each sprite""" for sprite in self.town_sprites: if sprite.location == (10, 47): - sprite.dialogue = 'Welcome to our town, Mr. Traveller!' + sprite.dialogue = ['Welcome to our town, Mr. Traveller!', + 'The King is loved by all!', + 'You should go visit him in his castle.'] elif sprite.location == (16, 42): - sprite.dialogue = 'You seem tired, why not rest at our Inn?' + sprite.dialogue = ['You seem tired, why not rest at our Inn?'] sprite.begin_auto_resting() elif sprite.location == (14, 14): - sprite.dialogue = 'Welcome to the castle, citizen.' + sprite.dialogue = ['Be careful. There are monsters surrounding our town.', + 'Make sure to equip sufficient armour and weapons.', + 'Spells and potions are useful too.'] elif sprite.location == (11, 14): - sprite.dialogue = 'I have heard rumours that the King has lost something...' + sprite.dialogue = ['I have heard rumours that the King has lost something...', + 'Perhaps you should pay him a visit.'] elif sprite.location == (11, 8): - sprite.dialogue = 'Be careful. There are monsters surrounding our town.' + sprite.dialogue = ['Welcome to the castle, citizen.'] elif sprite.location == (14, 8): - sprite.dialogue = 'Move along, citizen.' + sprite.dialogue = ['Move along.'] def make_state_dict(self):@@ -64,6 +69,7 @@
def running_normally(self, surface, keys, current_time): """Update level normally""" + self.check_for_dialogue() self.player.update(keys, current_time) self.town_sprites.update(current_time) self.collision_handler.update(keys, current_time)@@ -79,6 +85,12 @@ self.dialogue_handler.update(keys, current_time)
self.draw_level(surface) + def check_for_dialogue(self): + """Check if the level needs to freeze""" + if self.dialogue_handler.textbox: + self.state = 'dialogue' + + def update(self, surface, keys, current_time): """Updates state""" state_function = self.state_dict[self.state]@@ -101,9 +113,6 @@ surface.blit(self.level_surface, (0, 0), self.viewport)
self.dialogue_handler.draw(surface) - def get_event(self, event): - """Set event to level attribute""" - self.event = event