Began implementing dialogue boxes
Justin Armstrong justinmeister@gmail.com
Wed, 12 Mar 2014 09:43:21 -0700
4 files changed,
31 insertions(+),
159 deletions(-)
D
data/components/player.py
@@ -1,157 +0,0 @@
-__author__ = 'justinarmstrong' - -import pygame as pg -from .. import setup - -class Player(pg.sprite.Sprite): - """User controllable character""" - def __init__(self): - super(Player, self).__init__() - self.get_image = setup.tools.get_image - self.spritesheet_dict = self.create_spritesheet() - self.animation_lists = self.create_animation_lists() - self.index = 1 - self.direction = 'up' - self.image_list = self.animation_lists[self.direction] - self.image = self.image_list[self.index] - self.rect = self.image.get_rect() - self.state_dict = self.create_state_dict() - self.direction_dict = self.create_direction_dict() - self.state = 'resting' - self.x_vel = 0 - self.y_vel = 0 - self.direction = 'up' - self.timer = 0.0 - - - def create_spritesheet(self): - """Creates the sprite sheet dictionary for player""" - sheet = setup.GFX['player'] - image_list = [] - for row in range(2): - for column in range(4): - image_list.append(self.get_image( - self, column*32, row*32, 32, 32, sheet)) - - image_dict = {'facing up 1': image_list[0], - 'facing up 2': image_list[1], - 'facing down 1': image_list[2], - 'facing down 2': image_list[3], - 'facing left 1': image_list[4], - 'facing left 2': image_list[5], - 'facing right 1': image_list[6], - 'facing right 2': image_list[7]} - - return image_dict - - - def create_animation_lists(self): - """Create the lists for each walking direction""" - image_dict = self.spritesheet_dict - - left_list = [image_dict['facing left 1'], image_dict['facing left 2']] - right_list = [image_dict['facing right 1'], image_dict['facing right 2']] - up_list = [image_dict['facing up 1'], image_dict['facing up 2']] - down_list = [image_dict['facing down 1'], image_dict['facing down 2']] - - direction_dict = {'left': left_list, - 'right': right_list, - 'up': up_list, - 'down': down_list} - - return direction_dict - - - def create_state_dict(self): - """Creates a dictionary of all the states the player - can be in""" - dict = {'resting': self.resting, - 'moving': self.moving} - - return dict - - - def create_direction_dict(self): - """Creates a dictionary of x and y velocities set to - direction keys""" - dict = {'up': (0, -2), - 'down': (0, 2), - 'left': (-2, 0), - 'right': (2, 0)} - - return dict - - - def update(self, keys, current_time): - """Updates player behavior""" - self.keys = keys - self.current_time = current_time - self.check_for_input() - state_function = self.state_dict[self.state] - state_function() - - - def resting(self): - """When the player is not moving between tiles. - Checks if the player is centered on a tile""" - self.image = self.image_list[self.index] - - assert(self.rect.y % 32 == 0), ('Player not centered on tile: ' - + str(self.rect.y)) - assert(self.rect.x % 32 == 0), ('Player not centered on tile' - + str(self.rect.x)) - - - def moving(self): - """When the player is moving between tiles""" - if (self.current_time - self.timer) > 100: - if self.index < (len(self.image_list) - 1): - self.index += 1 - else: - self.index = 0 - self.timer = self.current_time - - self.image = self.image_list[self.index] - - assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \ - 'Not centered on tile' - - - def begin_moving(self, direction): - """Transitions the player into the moving state""" - self.direction = direction - self.image_list = self.animation_lists[direction] - self.timer = self.current_time - self.state = 'moving' - - - if self.rect.x % 32 == 0: - self.y_vel = self.direction_dict[self.direction][1] - if self.rect.y % 32 == 0: - self.x_vel = self.direction_dict[self.direction][0] - - - def begin_resting(self): - """Transitions the player into the resting state""" - self.state = 'resting' - self.index = 1 - self.x_vel = self.y_vel = 0 - - - - - def check_for_input(self): - """Checks for player input""" - if self.state == 'resting': - if self.keys[pg.K_UP]: - self.begin_moving('up') - elif self.keys[pg.K_DOWN]: - self.begin_moving('down') - elif self.keys[pg.K_LEFT]: - self.begin_moving('left') - elif self.keys[pg.K_RIGHT]: - self.begin_moving('right') - - - -
A
data/components/textbox.py
@@ -0,0 +1,21 @@
+__author__ = 'justinarmstrong' +import pygame as pg +from .. import setup + +class Dialogue(pg.sprite.Sprite): + """Text box used for dialogue""" + def __init__(self, x): + super(Dialogue, self).__init__() + self.image = setup.GFX['dialoguebox'] + self.rect = self.image.get_rect(centerx=x) + self.timer = 0.0 + + def update(self, current_time): + """Updates scrolling text""" + if self.timer == 0.0: + self.timer = current_time + elif (current_time - self.timer) > 2000: + self.kill() + + +
M
data/states/town.py
→
data/states/town.py
@@ -4,8 +4,7 @@ import os
import pygame as pg from .. import setup, tools, collision from .. import constants as c -from .. components.player import Player -from .. components import person +from .. components import person, textbox class Town(tools._State): def __init__(self):@@ -29,6 +28,7 @@ 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() def create_town_sprite_sheet_dict(self):@@ -346,7 +346,9 @@ 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.draw_level(surface)@@ -359,6 +361,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) def update_viewport(self):@@ -366,6 +369,11 @@ """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))