[英]Updating properties of an object in Python OOP
我正在嘗試使用 OOP 風格在 Python 中創建一個簡單的游戲。 設置了父類,有兩個子類,一個是英雄,一個是獸人。
基本上,當英雄攻擊獸人時(反之亦然),我希望根據造成的傷害更新生命值(傷害是攻擊角色擁有的力量)。 目前,每次循環時,它都會將健康值重置為 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()
快速修復您的代碼。 刪除所有這些不必要的屬性,例如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
這提高了代碼的可擴展性。 你可以更輕松地實施了“發燒友”系統,或者添加一個“鎧甲”的元素,會影響你如何收到損害,而不必更改代碼attack
。 您可以在子類中覆蓋這些方法。 所以,也許你希望所有的獸人有“thickskin”,並在Ork
:
def receive_damage(raw_damage):
self.health -= raw_damage*self.thickskin_modifier
在Ork
類__init__
設置self.thickskin_modifier
地方。 它可能類似於 0.9。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.