Get self variables from another class and custom event

Asked

Viewed 1,551 times

2

Read only comments in English, 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 within the Masteryi class that would cause every time the self.Yilevel variable was incremented into 1, variables like self.Yiad and others that I didn’t put in the code here, to be incremented into specific values.

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

And also how I make the increment of self.YiLevel in the code, I could together already do the increment in the other variables together, but in the future, in this code, I will need to know how to do custom events whether I want to or not, so I’d rather ask a question with a simpler example.

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

1 answer

2


You are repeating the same thing for multiple classes, take a look at the beginning DRY — Don’t Repeat Yourself, a question has already been answered here at Sopt

Magics

First I created a class Magic, where Q, E, W and R sane daughters. Each daughter possesses her class, that is, you can define unique methods for each and, incorporate the methods Use and Damage 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 will group all the 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

Class incorporates class attributes 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 magic per character, just use: Character.Magics.Magic.Method

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

See the final result on ideone

  • But the idea of not repeating the code I understood. is a class problem within class or whatever ?

  • And also has the following question not all the 'Champions' the Q work like this: Q = Q + AD some will be Q = Q + AD * 30 / 100, others Q = Q + AP each (Q, W, E, R) of each 'Champion' will be different.

  • So will it be a different value for each magic? For example ZedQ, ZedW, ZedE...? @axell13

  • Yeah, except not all spells are damage, like Zedw is a Skill who casts a shadow that mimics Zedq and Zede, Yiw is a magic that recovers life over four seconds. Each spell will almost always be treated differently, but not all as for example the Yiq and Zedq all two are the base damage of the magic 25, 75 respectively + the AD.

  • I edited the answer, see if that’s what you need. @axell13

  • What about the question of Q in other characters ? As I said sometimes it can be power = power + ((ad * 30) / 100) or power = power + ((ap * 80) / 100) or other calculations. I prefer to copy the class for example from Masteryi (from my code) and adapt the calculations of the evils for each one.

  • I do not create a(a) function/constructor(__init__) passing a string that would be the calculation, because there are more than 120 Champions, there is no way I remember the calculation of each, mainly because it is not 120 * 4 (Q,W,E,R), so I would have to be looking all the time.

  • And there is also the question that not all Q,W,E,R has damage, some give attributes with armor penetration, true damage, attack speed, armor and etc, are you reading the comments I am writing ? it seems that no.

  • I think you want the code ready, for sure you’ll have to adapt the code as you prefer according to your need, I’m doing you a favor. For every problem you mentioned there is a solution, just think! Hugs @axell13

  • ok I prefer to make the classes the way I was, I prefer to copy a class and adapt the calculations of the spellings I find easier.

  • Mainly because I don’t want to spend all the time parameters such as ad, ap, level and etc, after all each Champion will be different. I want to have them ready, I want ease in the future, so I assemble the classes, instead of passing parameters only create the object of my Champion, ready.

  • He even looked at the ideone? At the end has a class LOL, where I already define Champions.. Do you prefer to create 120 Champions and manually insert all methods, variables, where much is the same code? Have you thought in the future how difficult it will be to maintain this code?.... @axell13

  • the only problem even is the question of randomness in the calculations of each Champion (This does not know what to do)...

  • If the code is Python2 and not Python3, do not forget that the classes have all who inherit from "Object"

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.