1

How can I access a variable from another class that is not inherited? In my code I am trying to access the hitPoints class variable from the Dragon object with the Ranger object in the quickShot method.

class Dragon(object):
    name = "Dragon"
    hitPoints = 25

# Create the Ranger class
class Ranger(object):
    name = "Ranger"
    attack = 80
    defence = 50
    hitPoints = 100

    def __init__(self):
        self = self

    def quickShot(self):
        damage = 25
        test = random.random()
        if test < .8:
            #I cannot access the dragon's hitPoints
            Dragon.hitPoints = Dragon.hitPoints - damage
        else:
            pass

    def concentratedShot(self):
        damage = 50
        chance = random.random()
        if chance <= .5:
            chance = True
        else:
            chance = False

    def heartSeeker(self):
        damage = 100
        chance = random.random()
        if chance <= .3:
            chance = True
        else:
            chance = False
3
  • 1
    What error are you getting? This should work fine. (I mean, it may not do what you want it to do, but it runs.) Commented Nov 12, 2014 at 22:30
  • 1
    Why are all of your attributes on the class, not the instance? Also, note you can simplify with e.g. chance = random.random() <= 0.3. Commented Nov 12, 2014 at 22:30
  • 2
    So there's only one ranger and one dragon in the world, and the ranger can only fight that one dragon? Commented Nov 12, 2014 at 22:36

3 Answers 3

3

I would expect it to look like:

class Character(object):

    """All Characters have a name and some hit points."""

    def __init__(self, name, hit_points):
        self.name = name # assigned in __init__ so instance attribute
        self.hit_points = hit_points


class Player(Character):

    """Players are Characters, with added attack and defence attributes."""

    def __init__(self, name, hit_points, attack, defence):
        super(Player, self).__init__(name, hit_points) # call superclass
        self.attack = attack
        self.defence = defence

    def _attack(self, other, chance, damage):
        """Generic attack function to reduce duplication."""
        if random.random() <= chance:
            other.hit_points -= damage # works for any Character or subclass

    def quick_attack(self, other):
        """Attack with 80% chance of doing 10 damage."""
        self._attack(other, 0.8, 10)


dragon = Character("Dragon", 25) # dragon is a Character instance
ranger = Player("Ranger", 100, 80, 50) # ranger is a Player instance
ranger.quick_attack(dragon)
print dragon.hit_points

Work through that, ensure you understand what's happening and why, then build on it. If you can't follow it, I suggest you look for a Python OOP tutorial (or the official docs); what you have now will get you nowhere fast.

(Note also bonus style guide compliance.)

Sign up to request clarification or add additional context in comments.

Comments

0

I think what you want is to specify the dragon that is being shot. Then you can pass in an instance of the dragon in your class.

def quickShot(self, dragon):
    damage = 25
    test = random.random()
    if test < .8:
        dragon.hitPoints -= damage
    else:
        pass

Comments

0

1) You need to create an instance of the Dragon class and pass it to def quickShot(self): method

or

2) You may use static variables to make them a part of the Dragon class itself. Then you can access variables without making an instance of the class.

Obviously, in you particular case "The world full of Dragons and Rangers" the second solution isn't the best one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.