Dialogue boxes handled in separate class, dialogue text now an attribute of sprites.
Justin Armstrong justinmeister@gmail.com
Wed, 12 Mar 2014 11:39:47 -0700
3 files changed,
106 insertions(+),
12 deletions(-)
M
data/components/person.py
→
data/components/person.py
@@ -27,6 +27,8 @@ self.timer = 0.0
self.current_time = 0.0 self.state = 'resting' self.blockers = self.set_blockers() + self.location = self.get_tile_location() + self.dialogue = 'Hello there!' def create_spritesheet_dict(self, sheet_key):@@ -94,6 +96,7 @@ self.current_time = current_time
self.check_for_input() state_function = self.state_dict[self.state] state_function() + self.location = self.get_tile_location() def set_blockers(self):@@ -123,6 +126,25 @@
return blockers + def get_tile_location(self): + """Converts pygame coordinates into tile coordinates""" + if self.rect.x == 0: + tile_x = 1 + elif self.rect.x % 32 == 0: + tile_x = (self.rect.x / 32) + 1 + else: + tile_x = 0 + + if self.rect.y == 0: + tile_y = 1 + elif self.rect.y % 32 == 0: + tile_y = (self.rect.y / 32) + 1 + else: + tile_y = 0 + + return (tile_x, tile_y) + + def resting(self): """ When the Person is not moving between tiles.@@ -193,6 +215,7 @@ self.current_time = current_time
self.check_for_input() state_function = self.state_dict[self.state] state_function() + self.location = self.get_tile_location() def check_for_input(self):@@ -214,6 +237,7 @@ """Soldier for the castle"""
def __init__(self, x, y): super(Soldier, self).__init__('soldier', x, y) + class FemaleVillager(Person):
M
data/components/textbox.py
→
data/components/textbox.py
@@ -1,21 +1,94 @@
__author__ = 'justinarmstrong' import pygame as pg from .. import setup +from .. import constants as c class Dialogue(pg.sprite.Sprite): """Text box used for dialogue""" - def __init__(self, x): + def __init__(self, x, dialogue): super(Dialogue, self).__init__() self.image = setup.GFX['dialoguebox'] self.rect = self.image.get_rect(centerx=x) self.timer = 0.0 + self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 20) + self.dialogue_image = self.font.render(dialogue, True, c.BLACK) + self.dialogue_rect = self.dialogue_image.get_rect(left=50, top=50) def update(self, current_time): """Updates scrolling text""" + self.current_time = current_time + self.animate_dialogue() + self.terminate_check() + + + def animate_dialogue(self): + """Reveal dialogue on textbox""" + text_image = self.dialogue_image + text_rect = self.dialogue_rect + + self.image.blit(text_image, text_rect) + + + + def terminate_check(self): + """Remove textbox from sprite group after 2 seconds""" if self.timer == 0.0: - self.timer = current_time - elif (current_time - self.timer) > 2000: + self.timer = self.current_time + elif (self.current_time - self.timer) > 2000: self.kill() +class DialogueHandler(object): + """Handles interaction between sprites to create dialogue boxes""" + + def __init__(self, player, sprites, textbox_group): + self.player = player + self.sprites = sprites + self.textbox_group = textbox_group + self.textbox_onscreen = False + + + def update(self, keys, current_time): + """Checks for the creation of Dialogue boxes""" + if keys[pg.K_SPACE] and not self.textbox_onscreen: + for sprite in self.sprites: + self.check_for_dialogue(sprite) + + self.textbox_group.update(current_time) + + if len(self.textbox_group) < 1: + self.textbox_onscreen = False + + assert(len(self.textbox_group) < 2) + + + + def check_for_dialogue(self, sprite): + """Checks if a sprite is in the correct location to give dialogue""" + player = self.player + tile_x, tile_y = player.location + + if player.direction == 'up': + if sprite.location == (tile_x, tile_y - 1): + self.textbox_group.add(Dialogue(400, sprite.dialogue)) + self.textbox_onscreen = True + elif player.direction == 'down': + if sprite.location == (tile_x, tile_y + 1): + self.textbox_group.add(Dialogue(400, sprite.dialogue)) + self.textbox_onscreen = True + elif player.direction == 'left': + if sprite.location == (tile_x - 1, tile_y): + self.textbox_group.add(Dialogue(400, sprite.dialogue)) + self.textbox_onscreen = True + elif player.direction == 'right': + if sprite.location == (tile_x + 1, tile_y): + self.textbox_group.add(Dialogue(400, sprite.dialogue)) + self.textbox_onscreen = True + + + + + + +
M
data/states/town.py
→
data/states/town.py
@@ -28,7 +28,10 @@ self.start_positions = self.set_sprite_positions()
self.collision_handler = collision.CollisionHandler(self.player, self.blockers, self.town_sprites) - self.text_box_group = pg.sprite.Group() + self.textbox_group = pg.sprite.Group() + self.dialogue_handler = textbox.DialogueHandler(self.player, + self.town_sprites, + self.textbox_group) def create_town_sprite_sheet_dict(self):@@ -346,9 +349,8 @@ self.keys = keys
self.current_time = current_time self.player.update(keys, current_time) self.collision_handler.update() - self.dialogue_handler(keys) self.update_viewport() - self.text_box_group.update(current_time) + self.dialogue_handler.update(keys, current_time) self.draw_level(surface)@@ -361,7 +363,7 @@ self.level_surface.blit(self.player.image, self.player.rect)
self.town_sprites.draw(self.level_surface) surface.blit(self.level_surface, (0,0), self.viewport) - self.text_box_group.draw(surface) + self.textbox_group.draw(surface) def update_viewport(self):@@ -369,11 +371,6 @@ """Viewport stays centered on character, unless at edge of map"""
self.viewport.center = self.player.rect.center self.viewport.clamp_ip(self.level_rect) - - def dialogue_handler(self, keys): - """Handles creation of dialogue boxes""" - if keys[pg.K_SPACE]: - self.text_box_group.add(textbox.Dialogue(self.level_rect.centerx))