Python: variables inside class methods

后端 未结 3 592
情深已故
情深已故 2021-01-23 04:13

I\'m learning python and am trying to write a wound system based on hot zones of a character. Here\'s what I\'ve written. Don\'t judge me too much.

class Charact         


        
3条回答
  •  不要未来只要你来
    2021-01-23 04:52

    I change my previous answer to this.

    class Character:
    def __init__ (self, agility, strength, coordination):
            self.max_agility = 100
            self.max_strength = 100
            self.max_coordination = 100
            self.agility = agility
            self.strength = strength
            self.coordination = coordination
            self.l_arm=[]
            self.r_arm=[]
            self.l_leg=[]
            self.r_leg=[]
            self.hit_region_list = [self.l_arm , self.r_arm, self.l_leg, self.r_leg]
            self.healthy = "Healthy"
            self.skin_cut = "Skin Cut"
            self.muscle_cut = "Muscle Cut"
            self.bone_cut = "Exposed Bone"
    
    def hit (self, hit_region, wound):
            self.hit_region = hit_region
            self.wound = wound
            hit_region.append(wound)
            #Hit Zones
    
    
    
            #Wound Pretty Names
    
    
    
    
    john = Character(34, 33, 33)
    
    john.hit(john.l_arm,john.skin_cut)
    
    print john.hit_region
    print john.l_arm
    

    After running the above code I got this output

    output:
    ['Skin Cut']
    ['Skin Cut']
    

    As per the post, I think this is what you wanted. According to your previous code, your declarations were accessible inside a function only. Now You can manipulate the data and these variables for particular instances by declaring them inside the constructor.

提交回复
热议问题