Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming you're familiar with objects. If not, you should really read up on them (LPTHW is really good) before continuing, as they're pretty central to Python. If so, you should really be using a Player object instead of half a dozen globals.</p> <p>The really easy approach is to have the sword be an object that does stuff when equipped and unequipped:</p> <pre><code>class Sword(object): health_bonus = 5 def on_equip(self, character): character.health += self.health_bonus def on_unequip(self, character): character.health -= self.health_bonus </code></pre> <p>Then you call <code>on_equip</code> when the player equips the sword, and <code>on_unequip</code> when the player unequips it. Other weapons can have similar event hooks, maybe for different stats.</p> <p>That's all well and good, but something about it bugs me: you have to remember to call <code>on_unequip</code> whenever the player loses an item, and there might be more than one way that happens. You also have to track the player's "base" health separately, and if it changes, updating the "calculated" health requires unequipping and reequipping the weapon, which doesn't make a lot of sense.</p> <p>So let's try something else. The "visitor pattern" Marcin referred to means (roughly) to loop through all the parts of a larger whole and call the same method on them. In this case, you want to ask all of the player's equipment how much extra health it grants.</p> <pre><code>class Sword(object): def visit_health_bonus(self, character): return 5 # Then, every turn, recalculate health: health = player.base_health for equipment in player.equipment: # Pretend this lists everything the player has equipped; you might # have to check each hand manually health += equipment.visit_health_bonus(player) </code></pre> <p>Now you calculate the total health every time it's needed, so you don't have to worry about updating it when, say, the player levels up. You also don't have to worry about doing any extra bookkeeping when something gets unequipped: as soon as it disappears from the list of equipment, it stops contributing.</p> <p>This is still a little inflexible, as it doesn't easily support e.g. a sword that doubles your attack power when fighting dragons. The most general way to solve this involves a lot of tangling and extra objects and figuring out what effects need to happen first, and I've yet to make it work to my satisfaction. Hopefully the above approaches are a good enough starting point. :)</p> <p>The most flexible way to solve this is to have the action of "attack with equipped weapon" itself be an object and have equipment, the player, the target, etc. all react to the attack by increasing stats. But then you have questions of priority that are really fiddly to resolve so let's not do that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload