Added treasure chests to dungeon.
Justin Armstrong justinmeister@gmail.com
Tue, 06 May 2014 14:24:48 -0700
8 files changed,
138 insertions(+),
16 deletions(-)
M
data/components/person.py
→
data/components/person.py
@@ -581,3 +581,45 @@ """Placeholder"""
pass +class Chest(Person): + """ + Treasure chest that contains items to collect. + """ + def __init__(self, x, y, id): + super(Chest, self).__init__('treasurechest', x, y) + self.spritesheet_dict = self.make_image_dict() + self.image_list = self.make_image_list() + self.image = self.image_list[self.index] + self.rect = self.image.get_rect(x=x, y=y) + self.id = id + + def make_image_dict(self): + """ + Make a dictionary for the sprite's images. + """ + sprite_sheet = setup.GFX['treasurechest'] + image_dict = {'closed': self.get_image(0, 0, 32, 32, sprite_sheet), + 'opened': self.get_image(32, 0, 32, 32, sprite_sheet)} + + return image_dict + + def make_image_list(self): + """ + Make the list of two images for the chest. + """ + image_list = [self.spritesheet_dict['closed'], + self.spritesheet_dict['opened']] + + return image_list + + def update(self, current_time, *args): + """Implemented by inheriting classes""" + self.blockers = self.set_blockers() + self.current_time = current_time + state_function = self.state_dict[self.state] + state_function() + self.location = self.get_tile_location() + + + +
M
data/components/textbox.py
→
data/components/textbox.py
@@ -86,8 +86,8 @@ image.set_colorkey(c.BLACK)
image.blit(self.bground, (0, 0)) if self.item: - type = list(self.item.keys())[0] - total = str(self.item[type]['quantity']) + type = 'Healing Potion' + total = str(1) dialogue = 'You received ' + total + ' ' + type + '.' self.dialogue_list = [dialogue] self.item = None@@ -126,6 +126,9 @@ self.allow_input = False
self.check_for_dialogue(sprite) if self.textbox: + if self.talking_sprite.name == 'treasurechest': + self.open_chest(self.talking_sprite) + self.textbox.update(keys, current_time) if self.textbox.done:@@ -138,9 +141,9 @@ self.textbox = DialogueBox(dialogue, index)
elif self.textbox.name == 'infobox': self.textbox = ItemBox(dialogue, index) elif self.talking_sprite.item: - item = self.check_for_item() - self.textbox = ItemBox(None, item) + self.check_for_item() else: + self.talking_sprite = None self.level.state = 'normal' self.textbox = None self.last_textbox_timer = current_time@@ -180,27 +183,33 @@
def check_for_item(self): """Checks if sprite has an item to give to the player""" item = self.talking_sprite.item - type = list(item.keys())[0] - quantity = item[type]['quantity'] - value = item[type]['value'] if item: - if type in self.game_data['player inventory']: - self.game_data['player inventory'][type]['quantity'] += item[type]['quantity'] + if item in self.game_data['player inventory']: + self.game_data['player inventory'][item]['quantity'] += 1 else: - self.game_data['player inventory'][type] = {'quantity': quantity, - 'value': value} + self.add_new_item_to_inventory(item) + self.update_game_items_info(self.talking_sprite.id) self.talking_sprite.item = None if self.talking_sprite.name == 'king': self.game_data['king item'] = None elif self.talking_sprite.name == 'oldmanbrother': self.game_data['old man item'] = None + elif self.talking_sprite.name == 'treasurechest': + self.talking_sprite.dialogue = ['Empty.'] - return item + def add_new_item_to_inventory(self, item): + inventory = self.game_data['player inventory'] + if item == 'Healing Potion': + inventory[item] = dict([('quantity',5), + ('value',15)]) + else: + pass - + def update_game_items_info(self, id): + self.game_data['treasure{}'.format(id)] = False def reset_sprite_direction(self): """Reset sprite to default direction"""@@ -226,3 +235,7 @@ textbox = None
return textbox + def open_chest(self, sprite): + if sprite.name == 'treasurechest': + sprite.index = 1 +
M
data/states/levels.py
→
data/states/levels.py
@@ -6,13 +6,14 @@ This class inherits from the generic state class
found in the tools.py module. """ -import copy, random +import copy import pygame as pg from .. import tools, collision from .. components import person, textbox, portal from . import player_menu from .. import tilerender from .. import setup + class LevelState(tools._State):@@ -133,6 +134,17 @@ index = 1
else: index = 0 + if 'item' in properties: + item = properties['item'] + else: + item = None + + if 'id' in properties: + id = properties['id'] + else: + id = None + + x = properties['x'] * 2 y = ((properties['y']) * 2) - 32@@ -151,10 +163,13 @@ x, y, direction),
'soldier': person.Person('soldier', x, y, direction, 'resting', index), - 'king': person.Person('king', x, y, direction)} + 'king': person.Person('king', x, y, direction), + 'treasurechest': person.Chest(x, y, id)} sprite = sprite_dict[properties['type']] + sprite.item = item self.assign_dialogue(sprite, properties) + self.check_for_opened_chest(sprite) sprites.add(sprite) return sprites@@ -167,6 +182,13 @@ dialogue_list = []
for i in range(int(property_dict['dialogue length'])): dialogue_list.append(property_dict['dialogue'+str(i)]) sprite.dialogue = dialogue_list + + def check_for_opened_chest(self, sprite): + if sprite.name == 'treasurechest': + if not self.game_data['treasure{}'.format(sprite.id)]: + sprite.dialogue = ['Empty.'] + sprite.item = None + sprite.index = 1 def make_state_dict(self): """
M
data/tools.py
→
data/tools.py
@@ -203,7 +203,12 @@ 'old man item': {'ELIXIR': dict([('value',1000),
('quantity',1)])}, 'player inventory': player_items, 'player stats': player_stats, - 'battle counter': random.randint(50, 255) + 'battle counter': random.randint(50, 255), + 'treasure1': True, + 'treasure2': True, + 'treasure3': True, + 'treasure4': True, + 'treasure5': True } return data_dict
M
resources/tmx/dungeon.tmx
→
resources/tmx/dungeon.tmx
@@ -120,6 +120,14 @@ <property name="direction" value="down"/>
<property name="state" value="dungeon4"/> </properties> </object> + <object name="sprite" type="treasurechest" gid="51" x="192" y="96"> + <properties> + <property name="dialogue length" value="1"/> + <property name="dialogue0" value="You found a Healing Potion"/> + <property name="id" value="1"/> + <property name="item" value="Healing Potion"/> + </properties> + </object> </objectgroup> <layer name="Tile Layer 2" width="25" height="27"> <data encoding="base64" compression="zlib">
M
resources/tmx/dungeon2.tmx
→
resources/tmx/dungeon2.tmx
@@ -137,5 +137,21 @@ <property name="direction" value="down"/>
<property name="state" value="dungeon4"/> </properties> </object> + <object name="sprite" type="treasurechest" gid="51" x="352" y="64"> + <properties> + <property name="dialogue length" value="1"/> + <property name="dialogue0" value="You found a Healing Potion."/> + <property name="id" value="3"/> + <property name="item" value="Healing Potion"/> + </properties> + </object> + <object name="sprite" type="treasurechest" gid="51" x="208" y="224"> + <properties> + <property name="dialogue length" value="1"/> + <property name="dialogue0" value="You found a Healing Potion."/> + <property name="id" value="2"/> + <property name="item" value="Healing Potion"/> + </properties> + </object> </objectgroup> </map>
M
resources/tmx/dungeon4.tmx
→
resources/tmx/dungeon4.tmx
@@ -139,5 +139,21 @@ <property name="direction" value="down"/>
<property name="state" value="dungeon2"/> </properties> </object> + <object name="sprite" type="treasurechest" gid="51" x="192" y="208"> + <properties> + <property name="dialogue length" value="1"/> + <property name="dialogue0" value="You found a Healing Potion"/> + <property name="id" value="4"/> + <property name="item" value="Healing Potion"/> + </properties> + </object> + <object name="sprite" type="treasurechest" gid="51" x="192" y="368"> + <properties> + <property name="dialogue length" value="1"/> + <property name="dialogue0" value="You found a Healing Potion"/> + <property name="id" value="5"/> + <property name="item" value="Healing Potion"/> + </properties> + </object> </objectgroup> </map>