Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you are missing out on some of the awesome utility of object oriented programming, similar to what Marcin said you might what to make a series of classes like so:</p> <pre><code>class Weapon: def __init__(self, name, armor_hp_bonus = 0, skill_hp_bonus = 0): self.skill_hp_bonus = skill_hp_bonus self.armor_hp_bonus = armor_hp_bonus self.__name__ = name class Hero: def __init__(self): self.basehp = 10 self.armorhp = 0 self.skillhp = 0 self.righthand = None self.inventory = [] def choose_weapon(self): while True: for idx, item in enumerate(self.inventory): print '{i}: {weapon}'.format(i=idx, weapon=item.__name__) choice = raw_input("Pick your weapon: ") try: self.equip(self.inventory.pop(int(choice))) return except IndexError: print "Invalid choice" continue def equip(self, weapon): if self.righthand: print "Weapon already equipped to right hand!" return else: self.righthand = weapon self.armorhp += weapon.armor_hp_bonus self.skillhp += weapon.skill_hp_bonus def unequip(self): if not self.righthand: print "No weapon to unequip!" return self.skillhp -= self.righthand.skill_hp_bonus self.armorhp -= self.righthand.armor_hp_bonus self.inventory.append(self.righthand) self.righthand = None def acquire_weapon(self, weapon): self.inventory.append(weapon) def calc_effective_hp(self): return self.basehp + self.skillhp + self.armorhp </code></pre> <p>A class lets you keep all the variables you are having trouble keeping track of in one place. The skill bonus of one weapong will never get confused with the skill bonus of another weapon this way, because the variable storing that information is contained within the object. One you set up your classes correctly, the rest of your code is a lot shorter and cleaner because you can just call a method and rest assured that everything is being done correctly:</p> <pre><code>sword_of_1000_truths = Weapon('sword_of_1000_truths', skill_hp_bonus = 1337) gandalf = Hero() gandalf.acquire_weapon(sword_of_1000_truths) gandalf.choose_weapon() &gt;&gt; 0 sword_of_1000_truths &gt;&gt; Pick your weapon: 0 print gandalf.calc_effective_hp() &gt;&gt; 1347 gandalf.unequip() print gandalf.calc_effective_hp() &gt;&gt; 10 </code></pre> <p>The methods such as <code>equip</code> and <code>unequip</code> make sure to increment and decrement the <code>Hero</code>'s hp correctly, while keeping track of the logic of the game, i.e. you cant have two weapons in one hand, and you cant unequip a weapon if you don't have one equipped etc. This helps eliminate those ugly 100 lines of code you mention.</p> <p>Good luck! Hope this helps</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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