day23 02 combination (continue the dog fight game)
Three characteristics of object-oriented: inheritance polymorphism encapsulation
Let's talk about the combination
Combination: the property value of one object is the object of another class: object, property and property (generally there are two points)
Continue to expand the little game of man dog war in day22 01 to explain the existence of the combination. Compared with the previous game, this time an additional weapon equipment class is added, and the weapon has its big moves. First of all, all the attributes of a weapon are: the name of the weapon, the attack power the weapon has, the number of times the weapon can be used, and the money needed to purchase the weapon. So before you use the weapon every time, you need to see if you have enough money. If you don't have enough money, you need to recharge it first. After you purchase the weapon every time, you need to deduct the corresponding money. The code is as follows, with related comments:
# Definition of character class someone: def __init__(self,name,blood,aggr,sex): # self Object creation and initialization # Assign a value to a character's attribute to get a specific character self.name=name self.blood=blood self.aggr=aggr self.sex=sex self.money=0 # Attack skills possessed by characters def attack(self,dog): dog.blood -=self.aggr # If the character doesn't attack the dog, the dog will lose the corresponding amount of blood # Equip characters with weapons def get_weapon(self,weapon): if self.money>=weapon.price: # Only when the money possessed by the character is greater than or equal to the price of the weapon and equipment can the character possess the weapon and equipment self.money-=weapon.price # After buying weapons, the money will be deducted accordingly self.weapon=weapon # Add weapons self.aggr+=weapon.aggr # When a character adds weapons and equipment, its attack power equals its own plus weapon's else: print('Insufficient balance, please recharge first') # Remind people to recharge when they don't have enough money to buy weapons and equipment # Definition of dogs class dog: def __init__(self,name,blood,aggr,kind,): # self Object creation and initialization # Assign a value to the attribute of the dog class to get a specific dog self.name=name self.blood=blood self.aggr=aggr self.kind=kind self.money=0 # Default characters don't have money at first # Attack skills of dogs def bite(self,someone): someone.blood-=self.aggr # Every time a dog attacks a character, the character loses the corresponding amount of blood # Add a weapon and equipment class class weapon: def __init__(self,name,aggr,sustain,price): # self Object creation and initialization # Assign attribute value to weapon equipment self.name=name self.aggr=aggr self.sustain=sustain self.price=price # There's a big move in weaponry double_kill def double_kill(self,person,dog): if self.sustain>0: # Only when you have the remaining weapon use times can you use the big moves in the weapon person.blood-=self.aggr*2 # Every time you use a big move, the life of the character will lose twice the attack power of the weapon, that is, the big move of the weapon is twice the original attack power of the weapon dog.blood-=self.aggr*3 # Every time a big move is used and the dog is hit, the HP of the weapon will be 3 times of the attack power self.sustain-=1 # Each time you use a big move, the number of weapons you use will be reduced by one person1=someone('Wang Zhaojun',1000,300,'girl') # Character instantiation print(person1.name) dog1=dog('Yo-Yo',2000,500,'Poodle') # Instantiation of dog character print(dog1.name) person1.attack(dog1) # Conduct character attack on dog print(dog1.blood) # Because the attack power of a person is 300, when a character attacks a dog, the dog will lose 300 HP, leaving 1700 HP weapon1=weapon('Dog stick',100,3,1000) # Weapon instantiation person1.get_weapon(weapon1) # Equip characters with weapons and dog beating clubs person1.money=2000 # During the equipment process, it is found that the money possessed by the character is not enough to buy weapons and equipment. Remind to recharge, and then recharge 2000 person1.get_weapon(weapon1) print(person1.weapon.name) # Success in equipping people with weapons print(person1.aggr) # When a character gains weapon equipment, the attack power equals his own attack power plus the attack power of the weapon is 400 person1.attack(dog1) # The character gains the weapon before attacking the dog print(dog1.blood) # The dog will lose 400 HP, i.e. 1300 HP left at this time person1.weapon.double_kill(person1,dog1) # Big moves of characters using weapons print(dog1.blood) # The dog will lose 300 HP, 1000 left print(person1.blood) # People will lose 200 HP, 800 left
In the above code:
Personan1.weapon is an object of the weapon class
Personan1.webon is a property value of personan1
Double'u kill is another attribute value of person1.weapon
The attribute value of one object is the object of another class, forming a combination