基于面向对象设计一个对战游戏练习。

1.什么是对象?什么是类?

对象:对象是特征与技能的结合体,我可能有身高体重、而你也有身高体重,所以你会说你像我,但是你一定不会说向其他动物。

类:对象是特征与机能的结合体,类是一系列对象相同的特征与机能的结合体。

2.绑定方法的有什么特点

1.由类来调用类内部的函数,该函数只是一个普通的函数,普通函数需要接收几个参数就得传入几个参数。

2.绑定给谁,就应该由谁来调用,谁来调用就会江水当作第一个参数自动传入。

3.类中定义的函数,类确实可以使用,但其实类定义的函数大多情况下都是绑定给对象用的,所以在类中定义的函数都应该自带一个参数self

3.基于面向对象设计一个对战游戏

class Irelia:
    def __init__(self, name, health, magic, money):
        self.name = name
        self.health = health
        self.magic = magic
        self.money = money

    def attack(self,  enemy):
        enemy.health -= self.money

        print(f'{self.name}攻击了{ enemy.name},'
              f'{ enemy.name}当前生命值:{ enemy.health}')
        if  enemy.health <= 0:
            return True

class Ezreal:
    def __init__(self, name, health, damage, money):
        self.name = name
        self.health = health
        self.damage = damage
        self.money = money

    def attack(self, hero):
        hero.health -= self.money

        print(f'{self.name}攻击了{hero.name},'
              f'{hero.name}当前生命值:{hero.health}')
        if hero.health <= 0:
            return True


hero = Irelia('亚索', 2000, 400, 300)
adc = Ezreal('蛮子', 1500, 200, 400)


while True:

    mag1 = hero.attack(adc)
    if mag1:
        break


    mag2 = adc.attack(hero)
    if mag2:
        break
原文地址:https://www.cnblogs.com/WQ577098649/p/11643911.html