Linux Format

LOOT BOXES

-

Crawling virtual dungeons and defeating enemies is fine but nothing compares to the achievemen­t gained in rifling a virtual corpse for goodies. If you’ve already created the enemy class, you can create a couple of items, then add them to the enemy’s loot. For example: necklace = StatItem(“Necklace”, “A necklace made from yellowed bones.”, None, True, None, 0, “You feel a sudden pain as you try to wear the bone necklace. Lose 3 HP.”, None, None, None, True, -3) nugget = Item(“Nugget”, “A shiny gold nugget.”, None, True, None, 0,

None, None, None, None, False) hobgoblin = Enemy(“HobGoblin”, True, “A menacing hobgoblin stands before you.”, “The lifeless body of a hobgoblin lies on the ground.”, mace, 6, [necklace,nugget])

Enemies can automatica­lly drop items after defeat using the lootbody procedure:

def lootbody(enemy): current_room = player.currentroo­m

print(f”You defeated the {enemy.name} in combat!”) print(f”You find the following items on the {enemy.name}’s body:”)

if enemy.weapon != None: current_room.items.append(enemy.weapon) print(f”- {enemy.weapon.name}”) if enemy.loot is not None: for item in enemy.loot: current_room.items.append(item) print(f”- {item.name}”) current_room.enemies.remove(enemy)

This procedure first checks if the enemy has a weapon, then adds it to the current room items. It then does the same for any loot.

The procedure itself is invoked in the trytofight procedure if the player is victorious. View the nextroomst­atscript2.py script for an example of this.

else: print(enemy.deaddesc)

We can now add the command listenemie­s() at the top of the while True loop after listroomit­ems() . Take a moment to run the example script. The hobgoblin is now standing in the room (see Coding Combat box, p.87 to fight him). Once done, check out Loot Boxes (above) to rifle his body for trophies.

Further coding

If you’ve been following the Python text adventure series so far, we recommend downloadin­g the

adventure3.py example script, which covers all of the code covered in this tutorial.

If you’re feeling ambitious, try playing around with the combat mechanics, perhaps by using the random command as we suggested to introduce a little unpredicta­bility to combats.

We also haven’t had time to delve into defensive items such as armour or shields, which might reduce the HP damage during combat. Why not create a

DefenceIte­m subclass and see what you can do?

Stay tuned for the fourth and final part next month, where we’ll discuss introducin­g simple graphics.

Newspapers in English

Newspapers from Australia