为什么我的播放器对象更新分数,但不跟踪它是否还活着?

时间:2019-10-04 18:11:48

标签: python methods constructor updating

这是玩家阶层。

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,玩家也永远不会死。但是,正如印刷调试显示的那样,部落分数更新,所以我知道函数本身正在执行某事。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

EMPLID PLAN_TYPE EFFDT COVERAGE_ELECT 5423H 49 2013-01-01 W 5423H 49 2015-10-02 E 5423H 49 2015-10-09 T 5423H 49 2016-09-29 E 5423H 49 2018-10-01 E 5423H 49 2018-10-07 T 5423H 49 2018-11-14 W 中的以下代码行:

Player.__init__

您确切希望什么时候执行?我要说的是,从您的疑问来看,您希望每次将if self.dead: print("You have died.") time.sleep(2) print("So long...") time.sleep(2) 设置为self.dead时(当TruelandScoreairScore小于或等于-2)。

情况并非如此。 waterScore中if语句的主体仅在创建Player对象时才执行(毕竟它是在Player.__init__方法中)-并且只有在__init__为{{ 1}},默认情况下您已将其硬编码为self.dead