首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用类中的方法正确地遍历代码的一部分?

如何使用类中的方法正确地遍历代码的一部分?
EN

Stack Overflow用户
提问于 2021-01-04 16:49:54
回答 1查看 38关注 0票数 0

我正在学习Python中的类,并尝试制作一个简单的战斗游戏。这段代码运行得很好,只要我多次打印devin.battle(),但我想要一个while循环,这样它就会恢复到询问用户是否愿意发动战斗,同时从命中点中推断出攻击。

代码语言:javascript
复制
start_battle = input("Would you like to start a battle? Y/N ===>  ")

class People:
        max_hit_points = 150
        current_hit_points = 150
        current_strength = 5
        defence = 0

        def __init__(self, name, current_hit_points):
            self.name = name
            self.current_hit_points
            self.damage = 50
            

        def battle(self):
            if start_battle == "Y":
                self.attacked()
            elif start_battle == "N":
                print("okay, nevermind then")
            
        def attacked(self):
            self.current_hit_points -= self.damage
            print("You have been attacked")
            if self.current_hit_points > 0:
                print("Try again newb!")
            else:
                print("Your HP has reached 0, you are dead")

        def __str__(self):
            return f"{self. name} has {self.current_hit_points}HP remaining"
        


devin = People("Devin", 150)
devin.battle()
print(devin)

但是,当我在整个代码中添加一个时,Python就冻结了。

代码语言:javascript
复制
start_battle = input("Would you like to start a battle? Y/N ===>  ")

class People:
        max_hit_points = 150
        current_hit_points = 150
        current_strength = 5
        defence = 0

        def __init__(self, name, current_hit_points):
            self.name = name
            self.current_hit_points
            self.damage = 50

        while current_hit_points > 0:
            def battle(self):
                if start_battle == "Y":
                    self.attacked()
                elif start_battle == "N":
                    print("okay, nevermind then")
                
            def attacked(self):
                self.current_hit_points -= self.damage
                print("You have been attacked")
                if self.current_hit_points > 0:
                    print("Try again newb!")
                else:
                    print("Your HP has reached 0, you are dead")

            def __str__(self):
                return f"{self. name} has {self.current_hit_points}HP remaining"
            continue
        
        else:
            print("You are dead") 

devin = People("Devin", 150)
devin.battle()
print(devin)

如果有人能向我解释为什么会结冰,以及我如何正确地循环一场战斗,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2021-01-04 18:39:41

您在这里有多个问题,并且不是很清楚您要做什么,但根据您的评论,这里有一些指导,请确保阅读# code comments

代码语言:javascript
复制
class People:
        max_hit_points = 150
        current_hit_points = 150
        current_strength = 5
        defence = 0

        def __init__(self, name, current_hit_points):
            self.name = name
            # you forgot to actually assign the hit points
            self.current_hit_points = current_hit_points
            self.damage = 50

        def battle(self):
            while self.current_hit_points > 0:
                # you want to ask the user every round?
                start_battle = input("Would you like to start a battle? Y/N ===>  ")
                if start_battle == "Y":
                    self.attacked()
                    # note that the user didnt get to attack back
                    # maybe print(self) here so the user can see his hp?
                elif start_battle == "N":
                    print("okay, nevermind then")
                    break
            else:
                print("You are dead") 
                
        def attacked(self):
            self.current_hit_points -= self.damage
            print("You have been attacked")
            if self.current_hit_points > 0:
                print("Try again newb!")
            else:
                print("Your HP has reached 0, you are dead")

        def __str__(self):
            return f"{self. name} has {self.current_hit_points}HP remaining"

devin = People("Devin", 150)
devin.battle()
print(devin)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65559891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档