Added shop dialogue and a counter
Justin Armstrong justinmeister@gmail.com
Wed, 19 Mar 2014 11:24:45 -0700
9 files changed,
87 insertions(+),
34 deletions(-)
M
data/components/person.py
→
data/components/person.py
@@ -355,6 +355,7 @@ self.x_vel = self.y_vel = 0
self.state = 'resting' self.direction = 'down' self.default_direction = self.direction + self.item = None def get_location(self): """Get tile location"""
M
data/components/textbox.py
→
data/components/textbox.py
@@ -49,7 +49,7 @@ return image
- def update(self, current_time, keys): + def update(self, keys, current_time): """Updates scrolling text""" self.current_time = current_time self.draw_box(current_time)@@ -109,17 +109,17 @@ return image
-class DialogueHandler(object): +class TextHandler(object): """Handles interaction between sprites to create dialogue boxes""" - def __init__(self, player, sprites, level_object): - self.player = player - self.sprites = sprites + def __init__(self, level): + self.player = level.player + self.sprites = level.sprites self.talking_sprite = None self.textbox = None - self.level = level_object + self.level = level self.last_textbox_timer = 0.0 - self.game_data = level_object.persist + self.game_data = level.persist def update(self, keys, current_time):@@ -130,7 +130,7 @@ if (current_time - self.last_textbox_timer) > 300:
self.check_for_dialogue(sprite) if self.textbox: - self.textbox.update(current_time, keys) + self.textbox.update(keys, current_time) if self.textbox.done:@@ -202,6 +202,30 @@ def draw(self, surface):
"""Draws textbox to surface""" if self.textbox: surface.blit(self.textbox.image, self.textbox.rect) + + + def make_textbox(self, name, dialogue, item=None): + """Make textbox on demand""" + if name == 'itembox': + textbox = ItemBox(dialogue, item) + elif name == 'dialoguebox': + textbox = DialogueBox(dialogue) + else: + textbox = None + + return textbox + + + def update_for_shops(self, keys, current_time): + """Update text handler when player is in a shop""" + self.textbox.update(keys, current_time) + last_index = len(self.textbox.dialogue_list) - 1 + + if self.textbox.done and (self.textbox.index < last_index): + index = self.textbox.index + 1 + dialogue = self.textbox.dialogue_list + self.textbox = DialogueBox(dialogue, index) +
M
data/states/castle/castle.py
→
data/states/castle/castle.py
@@ -14,7 +14,7 @@ self.parent_level = c.TOWN
def set_sprite_dialogue(self): """Sets unique dialogue for each sprite""" - for sprite in self.level_sprites: + for sprite in self.sprites: if sprite.location == [12, 6]: sprite.dialogue = ["Please! You must help me!", "An evil sorceror has stolen my magic crown!",
M
data/states/level_state.py
→
data/states/level_state.py
@@ -33,21 +33,18 @@ self.blockers = tm.create_blockers(self.name)
self.level_surface = tm.make_level_surface(self.town_map) self.level_rect = self.level_surface.get_rect() self.player = person.Player(persist['last direction']) - self.level_sprites = pg.sprite.Group() + self.sprites = pg.sprite.Group() self.start_positions = tm.set_sprite_positions(self.player, - self.level_sprites, + self.sprites, self.name, self.persist) self.set_sprite_dialogue() self.collision_handler = collision.CollisionHandler(self.player, self.blockers, - self.level_sprites) - self.dialogue_handler = textbox.DialogueHandler(self.player, - self.level_sprites, - self) + self.sprites) + self.dialogue_handler = textbox.TextHandler(self) self.state_dict = self.make_state_dict() self.portals = tm.make_level_portals(self.name) - self.parent_level = None def set_sprite_dialogue(self):@@ -68,7 +65,7 @@ """Update level normally"""
self.check_for_dialogue() self.check_for_portals() self.player.update(keys, current_time) - self.level_sprites.update(current_time) + self.sprites.update(current_time) self.collision_handler.update(keys, current_time) self.dialogue_handler.update(keys, current_time) self.viewport_update()@@ -142,7 +139,7 @@ def draw_level(self, surface):
"""Blits all images to screen""" self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport) self.level_surface.blit(self.player.image, self.player.rect) - self.level_sprites.draw(self.level_surface) + self.sprites.draw(self.level_surface) surface.blit(self.level_surface, (0, 0), self.viewport) self.dialogue_handler.draw(surface)
M
data/states/shop.py
→
data/states/shop.py
@@ -24,16 +24,30 @@ self.persist = persist
self.current_time = current_time self.state = 'normal' self.get_image = tools.get_image - self.level_surface = pg.Surface(c.SCREEN_SIZE).convert() - self.level_surface.fill(c.BLACK_BLUE) - self.level_rect = self.level_surface.get_rect() - self.player = self.make_sprite('player', 96, 32, 100) - self.shop_owner = self.make_sprite('man1', 32, 32, 600) + self.background = self.make_background() + self.player = None + self.sprites = None + self.text_handler = textbox.TextHandler(self) + self.make_greeting_text() + + + def make_background(self): + """Make the level surface""" + background = pg.sprite.Sprite() + surface = pg.Surface(c.SCREEN_SIZE).convert() + surface.fill(c.BLACK_BLUE) + background.image = surface + background.rect = background.image.get_rect() + + player = self.make_sprite('player', 96, 32, 150) + shop_owner = self.make_sprite('man1', 32, 32, 600) + counter = self.make_counter() + background.image.blit(player.image, player.rect) + background.image.blit(shop_owner.image, shop_owner.rect) + background.image.blit(counter.image, counter.rect) - self.dialogue_handler = textbox.DialogueHandler(self.player, - self.shop_owner, - self) + return background def make_sprite(self, key, coordx, coordy, x, y=304):@@ -54,18 +68,33 @@
return sprite - def make_shop_owner(self): - """Make the shop owner sprite""" - return None + def make_greeting_text(self): + """Make the textbox for the shop owner's greeting""" + dialogue = ["Welcome to the " + self.name + "!", + "Would you like to spend 30 gold on a room tonight?"] + textbox = self.text_handler.make_textbox('dialoguebox', dialogue) + + self.text_handler.textbox = textbox + + + def make_counter(self): + """Make the counter to conduct business""" + sprite_sheet = copy.copy(setup.GFX['house']) + sprite = pg.sprite.Sprite() + sprite.image = self.get_image(102, 64, 26, 82, sprite_sheet) + sprite.image = pg.transform.scale2x(sprite.image) + sprite.rect = sprite.image.get_rect(left=550, top=225) + + return sprite def update(self, surface, keys, current_time): """Update level state""" + self.text_handler.update_for_shops(keys, current_time) self.draw_level(surface) def draw_level(self, surface): """Blit graphics to game surface""" - surface.blit(self.level_surface, self.level_rect) - surface.blit(self.player.image, self.player.rect) - surface.blit(self.shop_owner.image, self.shop_owner.rect) + surface.blit(self.background.image, self.background.rect) + self.text_handler.draw(surface)
M
data/states/town/portals.txt
→
data/states/town/portals.txt
@@ -39,7 +39,7 @@ 0000000000011000000000000
0000000000011000000333000 0003330000011000000333000 0003330000011000000222000 -00022200000110000002B2000 +00022200000110000002C2000 0002A20000011000000010000 0000100000011000000010000 0000111111111111111110000
M
data/states/town/town.py
→
data/states/town/town.py
@@ -12,7 +12,7 @@ super(Town, self).__init__(name, width, height)
def set_sprite_dialogue(self): """Sets unique dialogue for each sprite""" - for sprite in self.level_sprites: + for sprite in self.sprites: if sprite.location == [9, 46]: sprite.dialogue = ['Welcome to our town, Mr. Traveller!', 'The King is loved by all!',
M
data/tilemap.py
→
data/tilemap.py
@@ -385,6 +385,8 @@ for column, letter in enumerate(line):
if letter == 'A': portal_group.add(portal.Portal(column, row, c.CASTLE)) elif letter == 'B': + portal_group.add(portal.Portal(column, row, c.TOWN)) + elif letter == 'C': portal_group.add(portal.Portal(column, row, c.SHOP)) return portal_group