Light
Light

Reputation: 1

How can the attributes of one class be accessed in another?

So what am I trying to do
Well what I'm doing is trying to create a battling system between your character and an enemy
So wheres it going wrong
Well there are two classes:

class Ally  

and

class Enemy

Each class has their own unique attributes of name, health, attack and defense

class Ally  
    def __init__(self, name, health, attack, defense):  
        self.name = 'goodguy'  
        self.health = 100  
        self.attack = 50  
        self.defense = 30  
class Enemy  
    def __init__(self, name, health, attack, defense):  
        self.name = 'badguy'  
        self.health = 120  
        self.attack = 40   
        self.defense = 20  

But both class Ally and class Enemy need each others health and defense attributes inorder to do damage

class Ally(object):  
    def __init__(self, name, health, attack, defense):  
        self.name = goodguy  
        self.health = 100  
        self.attack = 50  
        self.defense = 30  
    def fight(self)  
        (damage moves)  
        Enemy health = Enemy.health - ((self.attack/Enemy.defense)+2)  
    def battle_script(self)   
        while self.health > 0 and Enemy.health > 0:  
            self.fight()  
            if Enemy.health <=0:  
                break  
            Enemy.fight()  
            if self.health <=0:  
                break  
        if Enemy.health() <= 0:  
            print ('The ' + Enemy.name + ' was defeated')  
        if self.health <= 0:  
            print ("You were defeated")  
class Enemy(object):   
    def __init__(self, name, health, attack, defense):  
        self.name = badguy  
        self.health = 120  
        self.attack = 40  
        self.defense = 20  
    def fight(self)  
        (random damage moves)  
        Enemy health = Ally.health - ((self.attack/Ally.defense)+2)  
Ally.battle_scrip()  

So the problem is that I don't know how to pull in their respective attributes, i.e. in the battle script calling in the Enemy.health, I could probably pull it all into one class, but I'd rather keep them separated for when I create more enemies.

Upvotes: 0

Views: 157

Answers (2)

Livvy Mackintosh
Livvy Mackintosh

Reputation: 21

So you want a one:many relationship for the Ally:Enemy classes? What I would do, is create a list of enemy class objects that are a member of your Ally class. You could even pass in an argument to your Ally constructor to pick the number of enemies you want.

Should be something like this:

import itertools    

class Ally(object):  
    def __init__(self, enemies):  
        self.name = goodguy  
        self.health = 100  
        self.attack = 50  
        self.defense = 30
        self.enemies = []
        for _ in itertools.repeat(None, enemies):
            self.enemies.append(Enemy())
...

If you want a Many:Many relationship I would create another class that can be an object for the battle. Lets call this the "Battle" class. You can then do the same trick of having a list of class objects for both your allies and enemies and then have your logic for conducting the battle in the battle class.

class Battle(object):  
    def __init__(self, allies, enemies):  
        self.allies = []
        self.enemies = []
        for _ in itertools.repeat(None, allies):
            self.allies.append(Ally())
        for _ in itertools.repeat(None, enemies):
            self.enemies.append(Enemy())
...

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113988

first you create instances of your class bob = Ally();evil_frank = Enemy(); ... beyond that I have no idea what you expect to happen here based on the code you are given but im guessing you want something like what follows

def fight(ally,enemy):
    while ally.is_alive() and enemy.is_alive():
        ally.hp -= enemy.dmg
        enemy.hp -= ally.dmg
    print "OK FIGHT OVER SOMEONE DIED..."

fight(bob,evil_frank)

Upvotes: 1

Related Questions