def __init__(self, name, sprite, description, price, equipped_sprites, body_part, defense, res, atk, weight, restrictions): Item.__init__(self, name, sprite, description, price) self.defense = defense self.res = res self.atk = atk self.weight = weight self.restrictions = restrictions self.body_part = body_part self.equipped_sprite = pg.transform.scale( pg.image.load(equipped_sprites[0]).convert_alpha(), (TILE_SIZE, TILE_SIZE)) if len(equipped_sprites) > 1: for sp in equipped_sprites[1:]: self.equipped_sprite.blit( pg.transform.scale( pg.image.load(sp).convert_alpha(), (TILE_SIZE, TILE_SIZE)), (0, 0)) # Used when character wearing the equipment cannot be selected self.sprite_unavailable = self.equipped_sprite.copy() color_image = pg.Surface(self.sprite.get_size()).convert_alpha() color_image.fill(LIGHT_GREY) self.sprite_unavailable.blit(color_image, (0, 0), special_flags=pg.BLEND_RGBA_MULT) self.normal_sprite = self.equipped_sprite
def __init__(self, amount): Item.__init__( self, str(amount) + " Gold", "imgs/dungeon_crawl/item/gold/gold_pile_10.png", "Gold could be used to buy some items or other services", 0) self.amount = amount
def test_init_item(self): name = 'life_potion' sprite = 'imgs/dungeon_crawl/item/potion/magenta_new.png' description = 'This is a test description' item = Item(name, sprite, description) self.assertEqual(name, item.name) self.assertEqual(description, item.desc) self.assertEqual('Life Potion', str(item))
def test_name_format(self): sprite = 'imgs/dungeon_crawl/item/potion/magenta_new.png' description = 'This is a test description' name = 'test' item = Item(name, sprite, description) self.assertEqual('Test', str(item)) name = 'Test' item = Item(name, sprite, description) self.assertEqual('Test', str(item)) name = 'item_test' item = Item(name, sprite, description) self.assertEqual('Item Test', str(item)) name = '5item_test_01' item = Item(name, sprite, description) self.assertEqual('5Item Test 01', str(item))
def test_init_item_with_price(self): name = 'life_potion' sprite = 'imgs/dungeon_crawl/item/potion/magenta_new.png' description = 'This is a test description' price = 200 item = Item(name, sprite, description, price) self.assertEqual(name, item.name) self.assertEqual(description, item.desc) self.assertEqual('Life Potion', str(item)) self.assertEqual(price, item.price) self.assertEqual(price // 2, item.resell_price)
def test_init_chest(self): pos = (0, 0) sprite_close = 'imgs/dungeon_crawl/dungeon/chest_2_closed.png' sprite_open = 'imgs/dungeon_crawl/dungeon/chest_2_open.png' item = Item("TestItem", 'imgs/dungeon_crawl/item/potion/yellow_new.png', "This is a desc", 50) potential_items = [(item, 1.0)] chest = Chest(pos, sprite_close, sprite_open, potential_items) self.assertEqual(pos, chest.pos) self.assertEqual(sprite_close, chest.sprite_close_link) self.assertEqual(sprite_open, chest.sprite_open_link) self.assertEqual(chest.item, item) self.assertFalse(chest.opened) self.assertFalse(chest.pick_lock_initiated) # Chest's current sprite should be the closed one self.assertNotEqual(chest.sprite_open, chest.sprite)
def __init__(self, name, sprite, description, price, effects): Item.__init__(self, name, sprite, description, price) self.effects = effects
def __init__(self, name, sprite, description, price): Item.__init__(self, name, sprite, description, price)
def parse_item_file(name): # Retrieve data root for item it_tree_root = etree.parse('data/items.xml').getroot().find('.//' + name) sprite = 'imgs/dungeon_crawl/item/' + it_tree_root.find( 'sprite').text.strip() info = it_tree_root.find('info').text.strip() price = it_tree_root.find('price') if price is not None: price = int(price.text.strip()) else: price = 0 category = it_tree_root.find('category').text.strip() if category == 'potion' or category == 'consumable': effects = [] for eff in it_tree_root.findall('.//effect'): effect_name = eff.find('type').text.strip() pow_el = eff.find('power') power = int(pow_el.text.strip()) if pow_el is not None else 0 duration_el = eff.find('duration') duration = int( duration_el.text.strip()) if duration_el is not None else 0 effects.append(Effect(effect_name, power, duration)) item = Potion(name, sprite, info, price, effects) if category == 'potion' else \ Consumable(name, sprite, info, price, effects) elif category == 'armor': body_part = it_tree_root.find('bodypart').text.strip() defense_el = it_tree_root.find('def') defense = int(defense_el.text.strip()) if defense_el is not None else 0 weight = int(it_tree_root.find('weight').text.strip()) eq_sprites = it_tree_root.find('equipped_sprites') if eq_sprites is not None: equipped_sprites = [] for eq_sprite in eq_sprites.findall('sprite'): equipped_sprites.append('imgs/dungeon_crawl/player/' + eq_sprite.text.strip()) else: equipped_sprites = [ 'imgs/dungeon_crawl/player/' + it_tree_root.find('equipped_sprite').text.strip() ] restrictions = load_restrictions(it_tree_root.find('restrictions')) item = Equipment(name, sprite, info, price, equipped_sprites, body_part, defense, 0, 0, weight, restrictions) elif category == 'shield': parry = int(float(it_tree_root.find('parry_rate').text.strip()) * 100) defense_el = it_tree_root.find('def') defense = int(defense_el.text.strip()) if defense_el is not None else 0 fragility = int(it_tree_root.find('fragility').text.strip()) weight = int(it_tree_root.find('weight').text.strip()) equipped_sprite = [ 'imgs/dungeon_crawl/player/hand_left/' + it_tree_root.find('equipped_sprite').text.strip() ] restrictions = load_restrictions(it_tree_root.find('restrictions')) item = Shield(name, sprite, info, price, equipped_sprite, defense, weight, parry, fragility, restrictions) elif category == 'weapon': power = int(it_tree_root.find('power').text.strip()) attack_kind = it_tree_root.find('kind').text.strip() weight = int(it_tree_root.find('weight').text.strip()) fragility = int(it_tree_root.find('fragility').text.strip()) w_range = [ int(reach) for reach in it_tree_root.find('range').text.strip().split(',') ] equipped_sprite = [ 'imgs/dungeon_crawl/player/hand_right/' + it_tree_root.find('equipped_sprite').text.strip() ] restrictions = load_restrictions(it_tree_root.find('restrictions')) effects = it_tree_root.find('effects') possible_effects = [] if effects is not None: possible_effects = [ load_weapon_effect(eff) for eff in effects.findall('effect') ] keywords_el = it_tree_root.find('strong_against/keywords') strong_against = [ Keyword[keyword.upper()] for keyword in keywords_el.text.strip().split(',') ] if keywords_el is not None else [] item = Weapon(name, sprite, info, price, equipped_sprite, power, attack_kind, weight, fragility, w_range, restrictions, possible_effects, strong_against) elif category == 'key': for_chest = it_tree_root.find('open_chest') is not None for_door = it_tree_root.find('open_door') is not None item = Key(name, sprite, info, price, for_chest, for_door) elif category == 'spellbook': spell = it_tree_root.find('effect').text.strip() item = Spellbook(name, sprite, info, price, spell) else: # No special category item = Item(name, sprite, info, price) return item
def random_item(price=None): attrs = random_item_attributes(price) return Item(attrs['name'], attrs['sample_img'], attrs['desc'], attrs['cost'])
def __init__(self, name, sprite, description, price, for_chest, for_door): Item.__init__(self, name, sprite, description, price) self.for_chest = for_chest self.for_door = for_door
def __init__(self, name, sprite, description, price, effects): Item.__init__(self, name, sprite, description, price) self.effects = effects self.drinksfx = pygame.mixer.Sound( os.path.join('sound_fx', 'potion.ogg'))