Get self variables from another class and custom event

1

Read only comments in Portuguese, the question of self is in comments in the code, now the events will be down there.

class LOL:
    class Champions:
        class MasterYi:
            def __init__(self):
                #YiAttributes
                self.YiAD = 66
                self.YiLevel = 1
                self.YiQ = 25 + self.YiAD

            def Name(self):
                return 'MasterYi'

            class Q:
                def Use(self, target):
                    print( "[MasterYi] Q In %s" % ( target ) )
                def Damage(self):
                    return self.YiQ #Como Eu Conseguiria Pegar O self.YiQ Da Classe MasterYi ?

        class Zed:
            def __init__(self):
                #ZedAttributes
                self.ZedAD = 84
                self.ZedLevel = 1
                self.ZedQ = 75 + self.ZedAD

            def Name(self):
                return 'Zed'

            class Q:
                def Use(self, target):
                    print( "[Zed] Q In %s" % ( target ) )
                def Damage(self):
                    return self.ZedQ #Mesmo Caso Do self.YiQ

    class Match:
        def Start(self, Tuple):
            print( "------ Starting Match ------\n" )
            if (len(Tuple) == 1):
                print( "Champions: %s" % ( Tuple[0].Name() ) )
            elif (len(Tuple) == 2):
                print( "Champions: %s, %s" % ( Tuple[0].Name(), Tuple[1].Name() ) )

        def Finish(self):
            print( "\n------ Finishing Match ------" )

#Creating Objects
Match = LOL.Match()
MasterYi = LOL.Champions.MasterYi()
Zed = LOL.Champions.Zed()

#Starting
Match.Start( ( MasterYi, Zed ) )

MasterYi.Q().Use( Zed.Name() )
print( "YiQ Damage: %g" % ( MasterYi.Q().Damage() ) )

#Finishing
Match.Finish()

I would need an event inside the class MasterYi that would cause every time the variable self.YiLevel to be incremented by 1, variables like self.YiAD and others that I did not put here in the code would be increased by specific values

I left the creation part of the objects in the code because then you can pick up and test the code.

And also how do I do the self.YiLevel incrementing in the code, I could already do the incrementing in the other variables together, but in the future, in this code, I'll need to know how to make custom events wanting or not, so I'd rather ask a question with a simpler example.

Another detail of these two classes 'MasterYi' and 'Zed' are similar, but there will be more dozens of classes and I assure that most will be different, in these two the self.Q calculation works almost the same way, only which in other classes will be for example self.Q = 30 + self.AP others will make percent calculations type self.Q = self.AP * 30/100 + self.AD * 10/100. Then will not always be (some number + AD), it still has the question that n is only Q and yes (Q, W, E, R)     

asked by anonymous 02.01.2015 / 14:11

1 answer

2

You're repeating the same thing for multiple classes, take a look at the DRY — Don’t Repeat Yourself principle, a question has already been answered here at SOPt

Magics

First I created a class Magic , where Q , E , W and R are daughters . Each child daughter has its class, that is, you can define unique methods for each, and incorporate the methods Use and Damage of super class Magic .

class Magic:

    def __init__(self, magic, source, power):
        self.magic = magic
        self.source = source
        self.power = power

    def Use(self, target):
        print("{source} {magic} In {target}".format(source=self.source, magic=self.magic, target=target))

    def Damage(self):
        return self.power

class Q(Magic, object):

    def __init__(self, source, ad, power):
        power = power + ad
        Magic.__init__(self, 'Q', source, power)

# W, E, R ....

And another class Magics where it will group all daughters.

class Magics:

    def __init__(self, source, ad, q, w, e, r):
        self.Q = Q(source, ad, q)
        self.W = W(source, ad, w)
        self.E = E(source, e)
        self.R = R(source, ad, r)

Champions

The class incorporates the attributes of class Magics

class Champion:

    def __init__(self, name, ad, level, powerQ, powerW, powerE, powerR):
        self.name = name
        self.ad = ad
        self.level = level
        self.powerQ = powerQ
        self.powerW = powerW
        self.powerE = powerE
        self.powerR = powerR

        self.Magics = Magics(self.name, self.ad, self.powerQ, powerW, powerE, powerR)

To use the spells per character, simply use: Character . Method

MasterYi.Magics.Q.Use(Zed.name)

See the final result on ideone

    
02.01.2015 / 15:52