Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

OOP RGP - additional features - Armory

I wanted to add some additional functionality to the RGP style game we created. I liked the feature of the sword and axe adding hit chance and I wanted to add another class of weapons or an armory. I created a new file called armory.py and made a class Weapon with subclasses Sword, Axe, and Bow. All add values to the attack limit, dodge chance, and attack strength to varying degrees. I am not sure how to equip the objects to the character. I wanted to see if it was possible to have these change the character stats at the Character object level. I just have no idea how to

class Character(Combat):
  attack_limit = 10
  experience = 0
  base_hit_points = 10

from combat import Combat

class Weapon(Combat):
  pass

class Sword(Weapon):
  attack_strength += 1
  attack_limit += 2
  dodge_limit += 1

class Axe(Weapon):
  attack_strength += 3
  attack_limit += 2
  dodge_limit += 0  

class Bow(Weapon):
  attack_strength += 0
  attack_limit += 3
  dodge_limit += 2  

I keep getting errors and am not even sure where to start. Any ideas?

[edit formatting --cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

I suspect your are getting errors from trying to increment an attribute in your weapons classes, such as, "undefined name attack_strength". First step replace the '+=' with '=' in your weapon classes.

It seems you are looking for a way to modify the effective attack_strength, attack_limit, and dodge_limit depending on the weapon at hand. Does your base `Character have a nominal value for these strengths and limits without the weapon?

A key question is how do you plan to associate the weapon with the character? You could add a weapon attribute to Character initially set to None, then as a weapon is added or changed, set this attribute to a new instance of the specific weapon class:

bob = Character()
# assign base attributes
bob.attack_strength = 1
bob.attack_limit = 1
bob.dodge_limit = 1
# assign weapon
bob.weapon = Sword()

Then add methods to Character that return the effect attack_strength, attack_limit, and dodge_limit by examining the weapon value:

class Character(Combat):
    attack_limit = 10
    experience = 0
    base_hit_points = 10
    weapon = None
    attack_strength = 0
    attack_limit = 0
    dodge_limit = 0

    def get_attack_strength(self):
        if self.weapon:
            # Add weapon value to character value
            return self.weapon.attack_strength + self.attack_strength
        else:
            # No weapon. Return character value
            return self.attack_strength

    # same for attack_limit and dodge_limit

from combat import Combat

class Weapon(Combat):
  pass

# Change attribute statements. Replace '+=' with '='
class Sword(Weapon):
  attack_strength = 1 
  attack_limit = 2
  dodge_limit = 1

class Axe(Weapon):
  attack_strength = 3
  attack_limit = 2
  dodge_limit = 0  

class Bow(Weapon):
  attack_strength = 0
  attack_limit = 3
  dodge_limit = 2  

You could go further and define an __init__ method for Character that allows you to specify the base attack_strength, attack_limit, and dodge_limit in the assignment:

big_bob = Character(attack_strength=2, attack_limit=2, dodge_limit=2, weapon=Sword())

Further you could add an __init__ method to each of your weapon classes to allow creating various swords, etc:

big_bob = Character(attack_strength=2, attack_limit=2, dodge_limit=2, weapon=Sword(attack_strength=10))

Perfect, thank you!