在Python OOP

时间:2016-10-15 18:08:28

标签: python oop

我正在尝试使用OOP样式在Python中创建一个简单的游戏。 父类已设置,有两个子类,一个用于英雄,另一个用于ork。

基本上,当英雄攻击ork(反之亦然)时,我希望根据所造成的伤害更新生命值(伤害是攻击角色拥有的力量)。目前,每次循环时,它都会将健康值重置为100的原始值。

使用OOP执行此操作的最佳方法是什么?我可以用我自己的程序和杂乱的方式弄清楚如何做到这一点,但我想看看应该怎么做。

class Character:
    '''Blueprint for game characters'''
    def __init__(self):
        #default values
        self.character = ""
        self.speed = 0
        self.power = 0
        self.health = 100

    def attack(self, attackObj):
        self.attacker = self.character
        self.attackerPower = self.power
        self.victim = attackObj.character
        self.victimHealth = attackObj.health
        self.newHealth = self.victimHealth - self.attackerPower
        print(self.character, "you have chosen to attack", self.victim)
        print(self.victim, "you have suffered", self.attackerPower, "damage and     your health is now", self.newHealth)


class Hero(Character):
    '''Inherits from character to create hero'''
    def __init__(self):
        Character.__init__(self)
        self.character = "Hero"
        self.speed = 8
        self.power = 9
        print(self.character, "you have",self.speed, "speed,", self.power, "power and", self.health, "health.")


class Ork(Character):
    '''Inherits from character to create ork'''
    def __init__(self):
        Character.__init__(self)
        self.character = "Ork"
        self.speed = 2
        self.power = 8
        print(self.character, "you have",self.speed, "speed,", self.power,     "power and", self.health, "health.")

def main():
    charclass = Character()
    hero = Hero()
    ork = Ork()

    endLoop = False
    while endLoop == False:
        print("Please choose your character by typing the corresponding key: ")
        print("H for hero")
        print("O for ork")
        charChoice = input()
        if charChoice in ("H", "h", "hero", "Hero"):
            charChoice = hero
            enemy = ork
            hero = Hero()

        elif charChoice in ("O", "o", "ork", "Ork"):
            charChoice = ork
            enemy = hero

        print("Please choose an action by typing the corresponding key: ")
        print("A to attack")
        actionChoice = input()
        if actionChoice in ("A", "a"):
            charChoice.attack(enemy)  
        else:
            print("Nothing chosen!")

        finishedYN = input("Have you finished? Y/N ")
        if finishedYN in ("Y", "y", "Yes", "yes", "YES"):
            print("You have chosen to end the game...")
            endloop = True
            break
        else:
            pass

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

快速修复代码。删除所有这些不必要的属性,例如self.attacker(仅为self),self.attackPower(仅为self.power)。 self.victim = attackObj.character只为您的对象提供一个新属性,该属性与attackObj.character无关。同样,这个:self.newHealth = self.victimHealth - self.attackerPower每次调用方法时都会创建一个新属性,总是为100 - self.attack

def attack(self, attackObj):
    attackObj.health -= self.power
    print(self.character, "you have chosen to attack", attackObj.character)
    print(attackObj.character, "you have suffered", self.power, "damage and     your health is now", attackObj.health)

实际上,更好的方法是添加改变对象的方法,这些方法将用作对象与其他对象交互方式的接口。所以,例如:

class Character:
    '''Blueprint for game characters'''
    def __init__(self):
        #default values
        self.character = ""
        self.speed = 0
        self.power = 0
        self.health = 100

    def attack(self, victim):
        victim.receive_damage(self.power)

    def receive_damage(raw_damage):
        self.health -= raw_damage

这提高了代码的可扩展性。你可以更轻松地实现一个“buffs”系统,或者添加一个“盔甲”元素,它会影响你如何收到伤害,而不必更改attack中的代码。您可以在子类中覆盖这些方法。所以,也许你希望所有的orks都有“厚皮”,并且在Ork

    def receive_damage(raw_damage):
        self.health -= raw_damage*self.thickskin_modifier

self.thickskin_modifierOrk中设置__init__的位置。它可能是0.9。