我目前正在学习课程并随手进行应用,但是遇到一个我不确定如何通过的问题:
我不知道如何定义一个拥有武器的角色。
我在其他地方研究了这个问题,但是它总是导致继承,我想避免这种继承,因为这没有意义。有什么办法吗?我将在下面显示与该问题相关的代码,然后在下面显示所用类的完整代码。
class weapon:
def __init__ (self):
<constructor>
def set_attack (self, attack):
self.attack = attack
def get_attack (self):
return self.attack
class character:
def __init__ (self):
<constructor>
def set_attack (self, amount):
self.attack = amount
def get_attack (self):
return self.attack
class weapon:
def __init__ (self, name, attack, crit_chance, crit_bonus, rarity, type_bonus):
self.name = name
self.attack = attack
self.crit_chance = crit_chance
self.crit_bonus = crit_bonus
self.rarity = rarity
self.type_bonus = type_bonus
def set_name (self, name):
self.name = name
def set_attack (self, attack):
self.attack = attack
def set_crit_chance (self, crit_chance):
self.crit_chance = crit_chance
def set_crit_bonus (self, crit_bonus):
self.crit_bonus = crit_bonus
def set_rarity (self, rarity):
self.rarity = rarity
def set_type_bonus (self, type_bonus):
self.type_bonus = type_bonus
def get_name (self):
return self.name
def get_attack (self):
return self.attack
def get_crit_chance (self):
return self.crit_chance
def get_crit_bonus (self):
return self.crit_bonus
def get_rarity (self):
return self.rarity
def get_type_bonus (self):
return self.type_bonus
class character:
def __init__ (self, name, max_health, current_health, attack):
self.name = name
self.max_health = max_health
self.current_health = current_health
self.attack = attack
def set_name (self, name):
self.name = name
def set_max_health (self, amount):
self.max_amount = amount
def set_current_health (self, amount):
self.current_health = amount
def set_attack (self, amount):
self.attack = amount
def get_name (self):
return self.name
def get_max_health (self):
return self.max_health
def get_current_health (self):
return self.current_health
def get_attack (self):
return self.attack
class player (character):
def __init__ (self, name, max_health, current_health, attack, money, level, xp):
super().__init__(name, max_health, current_health, attack)
self.money = money
self.level = level
self.xp = xp
def set_money (self, amount):
self.money = amount
def set_level (self, amount):
self.level = amount
def set_xp (self, amount):
self.xp = amount
def get_money (self):
return self.money
def get_level (self):
return self.level
def get_xp (self):
return self.xp
我期望从角色继承的玩家能够使用与该角色相关联的武器对象的值进行攻击。 感谢您提供任何信息。强调文字
答案 0 :(得分:0)
Character
类将具有一个字段,该字段是Weapon
类的实例,如下所示:
class Character:
def __init__ (self, name, max_health, current_health, attack, weapon):
self.name = name
self.max_health = max_health
self.current_health = current_health
self.attack = attack
self.weapon = weapon # this line
def set_name(self, name):
self.name = name
def set_weapon(self, weapon):
self.weapon = weapon
您只需构建一个Weapon
对象,然后在构造它时(或通过setter方法)将其传递给Character
对象:
weapon = Weapon()
character = Character('Char Name', 100, 100, 20, weapon)
现在,作为补充,请用大写字母命名您的班级,即PEP8 convention。它有助于将类名与实例区分开,否则在上面的示例中,您将无法调用实例weapon
和character
。
答案 1 :(得分:0)
首先,您可以摆脱所有这些getter和setter并将类设为
class Weapon:
def __init__ (self, name, attack, crit_chance, crit_bonus, rarity, type_bonus):
self.name = name
self.attack = attack
self.crit_chance = crit_chance
self.crit_bonus = crit_bonus
self.rarity = rarity
self.type_bonus = type_bonus
class Character:
def __init__ (self, name, max_health, current_health, attack):
self.name = name
self.max_health = max_health
self.current_health = current_health
self.attack = attack
然后将武器对象传递给玩家类,并使用它来攻击玩家,这里weapon
代表玩家的武器
class Player (character):
def __init__ (self, name, max_health, current_health, attack, money, level, xp, weapon):
super().__init__(name, max_health, current_health, attack)
self.money = money
self.level = level
self.xp = xp
self.weapon = weapon
#For e.g. A weapon attacks and reduces the current health of the player
def attack(self):
self.current_health -= self.weapon.attack
或者将武器和角色类对象同时传递给玩家类,otherplayer
是被攻击的玩家,weapon
是与玩家一起的武器
class Player (character):
def __init__ (self, name, max_health, current_health, attack, money, level, xp, otherplayer, weapon):
super().__init__(name, max_health, current_health, attack)
self.money = money
self.level = level
self.xp = xp
self.otherplayer = otherplayer
self.weapon = weapon
def attack(self):
self.otherplayer.current_health -= self.weapon.attack