为什么我的玩家对象会更新分数但不跟踪它是否还活着?
Why does my player object update score but doesn't keep track of whether it's alive or not?
这是玩家class。
class Character():
def __init__(self, name, territory):
self.waterScore = 1
self.airScore = 1
self.landScore = 1
self.tribe = ""
if territory == 1: #Water tribe
self.airScore = 0
self.landScore = 1
self.waterScore = 2
self.tribe = "Water"
if territory == 2: #Air tribe
self.landScore = 0
self.waterScore = 1
self.airScore = 2
self.tribe = "Air"
if territory == 3: #Land tribe
self.waterScore = 0
self.airScore = 1
self.landScore = 2
self.tribe = "Land"
self.name = name
self.tribeScore = 0
if self.tribe == "Land":
self.tribeScore = self.landScore
elif self.tribe == "Water":
self.tribeScore = self.waterScore
elif self.tribe == "Air":
self.tribeScore = self.airScore
class Player(Character):
def __init__(self, territory):
super().__init__("Player", territory)
self.dead = False
if self.dead:
print("You have died.")
time.sleep(2)
print("So long...")
time.sleep(2)
sys.exit(0)
def setTribeScore(self): #This is the problem area
if self.tribe == "Water":
self.tribeScore = self.waterScore #These three are working
elif self.tribe == "Air":
self.tribeScore = self.airScore
elif self.tribe == "Land":
self.tribeScore = self.landScore
if self.landScore <= -2 or self.airScore <= -2 or self.landScore <= -2: #this one isn't doing anything
self.dead = True
这里是调用"setTribeScore()"的地方:
"""
An enemy and the player are passed into the following function, as well as which tribe skill is at stake. The function checks whether the winner or the loser is the player in order to set their tribe score.
"""
def winFight(winner, loser, stake):
x = getattr(winner, stake)
setattr(winner, stake, x + 1) #the attack that the winner used gets increased
y = getattr(loser, stake)
setattr(loser, stake, y - 1)
if not isinstance(winner, Player):
del winner
loser.setTribeScore()
print("You lost! Your attributes are {} for land, {} for water, and {} for air.".format(getattr(loser, "landScore"), getattr(loser, "waterScore"), getattr(loser, "airScore")))
elif not isinstance(loser, Player):
del loser
winner.setTribeScore()
print("You won! Your attributes are {} for land, {} for water, and {} for air.".format(getattr(winner, "landScore"), getattr(winner, "waterScore"), getattr(winner, "airScore")))
当我 运行 通过我的游戏循环时,玩家永远不会死,即使部落得分低于 -1。然而,部落分数 确实 更新,如打印调试所示,所以我知道函数本身正在做 某事 。可能是什么问题?
你的这些代码行 Player.__init__
:
if self.dead:
print("You have died.")
time.sleep(2)
print("So long...")
time.sleep(2)
您希望它在什么时候执行?我在这里得到的是,从你的提问线看来,你希望只要你将 self.dead
设置为 True
(当 landScore
、airScore
或者 waterScore
小于或等于 -2).
事实并非如此。 Player.__init__
中的 if 语句的主体只会在创建 Player 对象时执行(毕竟它在 __init__
方法中) - 只有当 self.dead
是 True
,默认情况下您已将其硬编码为 False
。
这是玩家class。
class Character():
def __init__(self, name, territory):
self.waterScore = 1
self.airScore = 1
self.landScore = 1
self.tribe = ""
if territory == 1: #Water tribe
self.airScore = 0
self.landScore = 1
self.waterScore = 2
self.tribe = "Water"
if territory == 2: #Air tribe
self.landScore = 0
self.waterScore = 1
self.airScore = 2
self.tribe = "Air"
if territory == 3: #Land tribe
self.waterScore = 0
self.airScore = 1
self.landScore = 2
self.tribe = "Land"
self.name = name
self.tribeScore = 0
if self.tribe == "Land":
self.tribeScore = self.landScore
elif self.tribe == "Water":
self.tribeScore = self.waterScore
elif self.tribe == "Air":
self.tribeScore = self.airScore
class Player(Character):
def __init__(self, territory):
super().__init__("Player", territory)
self.dead = False
if self.dead:
print("You have died.")
time.sleep(2)
print("So long...")
time.sleep(2)
sys.exit(0)
def setTribeScore(self): #This is the problem area
if self.tribe == "Water":
self.tribeScore = self.waterScore #These three are working
elif self.tribe == "Air":
self.tribeScore = self.airScore
elif self.tribe == "Land":
self.tribeScore = self.landScore
if self.landScore <= -2 or self.airScore <= -2 or self.landScore <= -2: #this one isn't doing anything
self.dead = True
这里是调用"setTribeScore()"的地方:
"""
An enemy and the player are passed into the following function, as well as which tribe skill is at stake. The function checks whether the winner or the loser is the player in order to set their tribe score.
"""
def winFight(winner, loser, stake):
x = getattr(winner, stake)
setattr(winner, stake, x + 1) #the attack that the winner used gets increased
y = getattr(loser, stake)
setattr(loser, stake, y - 1)
if not isinstance(winner, Player):
del winner
loser.setTribeScore()
print("You lost! Your attributes are {} for land, {} for water, and {} for air.".format(getattr(loser, "landScore"), getattr(loser, "waterScore"), getattr(loser, "airScore")))
elif not isinstance(loser, Player):
del loser
winner.setTribeScore()
print("You won! Your attributes are {} for land, {} for water, and {} for air.".format(getattr(winner, "landScore"), getattr(winner, "waterScore"), getattr(winner, "airScore")))
当我 运行 通过我的游戏循环时,玩家永远不会死,即使部落得分低于 -1。然而,部落分数 确实 更新,如打印调试所示,所以我知道函数本身正在做 某事 。可能是什么问题?
你的这些代码行 Player.__init__
:
if self.dead:
print("You have died.")
time.sleep(2)
print("So long...")
time.sleep(2)
您希望它在什么时候执行?我在这里得到的是,从你的提问线看来,你希望只要你将 self.dead
设置为 True
(当 landScore
、airScore
或者 waterScore
小于或等于 -2).
事实并非如此。 Player.__init__
中的 if 语句的主体只会在创建 Player 对象时执行(毕竟它在 __init__
方法中) - 只有当 self.dead
是 True
,默认情况下您已将其硬编码为 False
。