代码有点长,是面向对象的题,感觉有点懵逼,看了两天还没懂,这里是要适当地囫囵吞枣吗?

我的疑问:

  1.为啥不可以把def attack 、def roadRush 、def fight 归为class Fight 这样一类呢?

  2.为啥把def drive 放在游戏人物(GmaeCharacter)一类不可以,放在这类下程序会无法执行

  3.把def attack 和 def roadRush 放在游戏人物(GameCharacter)一类的原因是什么?同理,def fight 和 def drive也是,为什么要放在其他类中

因为一开始我把这些归为一类导致程序一直报错,求解答~~~~

目前就想到这三个疑问,这面向对象搞得我有点懵,希望有大佬可以指点我

感谢你们,谢谢,万分感谢你们

class GameCharacter:
    def __init__(self,name,sex,age,ad,hp):
        self.name = name
        self.sex = sex
        self.age = age
        self.ad = ad
        self.hp = hp

    def addMoto(self,mo):
        self.mo = mo

    def addWeapon(self,wea):
        self.wea = wea

    def attack(self,gameCharacter):
        gameCharacter.hp = gameCharacter.hp - self.ad
        print('{0}赤手空拳打了{1}{2}滴血,{1}还剩{3}血'.format(
            self.name,gameCharacter.name,self.ad,gameCharacter.hp))

    def roadRush(self,gameCharacter):
        gameCharacter.hp=gameCharacter.hp - self.ad - self.wea.ad
        print('%s骑着%s打了骑着%s的%s一%s,%s哭了,还剩%s血' % (
            self.name,self.mo.name,gameCharacter.mo.name,gameCharacter.name,
            self.wea.name,gameCharacter.name,gameCharacter.hp))

class Weapon:
    def __init__(self,name,ad):
        self.name = name
        self.ad = ad

    def fight(self,gameCharacter01,gameCharacter02):
        gameCharacter02.hp = gameCharacter02.hp - gameCharacter01.ad - self.ad
        print('{0}利用{1}打了{2}一{1},{2}还剩{3}血'.format(
            gameCharacter01.name,self.name,gameCharacter02.name,gameCharacter02.hp))

class GameMoto:
    def __init__(self,name,speed):
        self.name = name
        self.speed = speed
    def drive(self,gameCharacter,GameMoto):
        print('%s骑着%s开着%s迈的车行驶在赛道上' %(gameCharacter.name,GameMoto.name,GameMoto.speed))

gameCharacter1 = GameCharacter('苍井井','',18,20,200)
gameCharacter2 = GameCharacter('东尼木木','',20,30,150)
gameCharacter3 = GameCharacter('波多多','',19,50,80)

weapon1 = Weapon('平底锅',20)
weapon2 = Weapon('斧子',20)
weapon3 = Weapon('双截棍',20)

gameMoto1 = GameMoto('小踏板',20)
gameMoto2 = GameMoto('雅马哈',80)
gameMoto3 = GameMoto('宝马',120)

# (1)苍井井骑着小踏板开着60迈的车行驶在赛道上。
gameCharacter1.addMoto(gameMoto1)   #给苍井井加上坐骑属性
gameCharacter1.mo.drive(gameCharacter1,gameMoto1)

# (2)东尼木木骑着宝马开着120迈的车行驶在赛道上
gameCharacter2.addMoto(gameMoto3)   #给东尼木木加上坐骑属性
gameCharacter2.mo.drive(gameCharacter2,gameMoto3)

#(3)波多多骑着雅马哈开着80迈的车行驶在赛道上。
gameCharacter3.addMoto(gameMoto2)   #给波多多加上坐骑属性
gameCharacter3.mo.drive(gameCharacter3,gameMoto2)

# (4)苍井井赤手空拳打了波多多20滴血,波多多还剩xx血。
gameCharacter1.attack(gameCharacter3)

# (5)东尼木木赤手空拳打了波多多30滴血,波多多还剩xx血。
gameCharacter2.attack(gameCharacter3)

# (6)波多多利用平底锅打了苍井井一平底锅,苍井井还剩xx血。
gameCharacter3.addWeapon(weapon1)   #给波多多加上武器属性
gameCharacter3.wea.fight(gameCharacter3,gameCharacter1)

# (7)波多多利用斧子打了东尼木木一斧子,东尼木木还剩xx血。
gameCharacter3.addWeapon(weapon2)   #给波多多加上武器属性
gameCharacter3.wea.fight(gameCharacter3,gameCharacter2)

# (8)苍井井骑着宝马打了骑着小踏板的东尼木木一双节棍,东尼木木哭了,还剩xx血。
gameCharacter1.addMoto(gameMoto3)   #给苍井井加上坐骑属性
gameCharacter1.addWeapon(weapon3)   #给苍井井加上武器属性
gameCharacter2.addMoto(gameMoto1)   #给东尼木木加上坐骑属性
gameCharacter1.roadRush(gameCharacter2)

# # (9)波多多骑着小踏板打了骑着雅马哈的东尼木木一斧子,东尼木木哭了,还剩xx血。
gameCharacter3.addMoto(gameMoto1)   #给波多多加上坐骑的属性
gameCharacter3.addWeapon(weapon2)   #给波多多加上武器属性
gameCharacter2.addMoto(gameMoto2)   #给东尼木木加上坐骑属性
gameCharacter3.roadRush(gameCharacter2)
01-25 18:16