首页 > 解决方案 > 使用构造函数创建对象时如何避免死循环

问题描述

# Import Modules
from Dice import dice
d10 = dice(10,1)
d4 = dice(4,1)

# Assign Classes
class Player_Character:
    def __init__(self, hp, maxhp, ac, THAC0, Surprise_Adjustment, Initiative_Adjustment):
        self.hp = int(hp)
        self.maxhp = int(maxhp)
        self.ac = int(ac)
        self.THAC0 = int(THAC0)
        self.Surprise_Adjustment = int(Surprise_Adjustment)
        self.Initiative_Adjustment = int(Initiative_Adjustment)

    def attack(self, goblin):
        Player_Character_Damage = d10.die_roll()
        goblin.hp -= Player_Character_Damage
        if (goblin.hp <= 0):
            print("congratulations you killed the goblin")

    def flee(self):
        print("you run away ")
        quit()

    def heal(self, Player_Character):
        Player_Character.hp += d10.die_roll()
        if Player_Character.hp >= Player_Character.maxhp:
            Player_Character.hp = Player_Character.maxhp

class goblin:
    def __init__(self, hp, maxhp, ac, THAC0, Surprise_Adjustment, Initiative_Adjustment):
        self.hp = int(hp)
        self.maxhp = int(maxhp)
        self.ac = int(ac)
        self.THAC0 = int(THAC0)
        self.Surprise_Adjustment = int(Surprise_Adjustment)
        self.Initiative_Adjustment = int(Initiative_Adjustment)

    def attack(self, Player_Character):
        goblin_damage = d4.die_roll()
        Player_Character.hp -= goblin_damage
        if (Player_Character.hp <= 0):
            print("oh dear you have died")
            del Player_Character


MrHezy = Player_Character(10, 20, 10, 15, 0, 2)

def spawn_goblin(goblin):
    G1 = goblin(5, 10, 8, 18, 0, 0)
    return G1

goblin1 = spawn_goblin(goblin)

def battle(goblin1):

    # user input
    player_initiative_adjustment = MrHezy.Initiative_Adjustment
    monster_initiative_adjustment = goblin1.Initiative_Adjustment

    #define while loop for the battle
    battle_not_over = 'yes'
    while battle_not_over == 'yes':

        #use random.randint(a,b) to generate player and monster base initiative
        player_base_initiative = d10.die_roll()
        monster_base_initiative = d10.die_roll()

        #subtract the adjustment to get player and monster initiative
        player_initiative = player_base_initiative - player_initiative_adjustment
        monster_initiative = monster_base_initiative - monster_initiative_adjustment

        #compare the initiatives and display the results
        if (player_initiative < monster_initiative):
            attack_flee_heal = input("congratulations you go first. Would you like to attack, flee, or heal?")
            while attack_flee_heal != 'attack' or 'flee' or 'heal':
                if attack_flee_heal == 'attack':
                    MrHezy.attack(goblin1)
                elif attack_flee_heal == 'heal':
                    MrHezy.heal(MrHezy)
                    print("the goblin attacks")
                    goblin1.attack(MrHezy)
                    break
                elif attack_flee_heal == 'flee':
                    MrHezy.flee()
                    break
        else:
            print("uhoh, the monsters go first, they attack!")
            goblin1.attack(MrHezy)
            attack_flee_heal = input("Would you like to attack, flee, or heal? ")
            while attack_flee_heal != 'attack' or 'flee' or 'heal':
                if attack_flee_heal == 'attack':
                    MrHezy.attack(goblin1)
                elif attack_flee_heal == 'heal':
                    MrHezy.heal(MrHezy)
                    print("the goblin attacks")
                    goblin1.attack(MrHezy)
                    break
                elif attack_flee_heal == 'flee':
                    MrHezy.flee()
                    break

#main game loop
while True:
    spawn_goblin(goblin)
    battle(goblin1)

这是我一直在研究的战斗模拟器的代码。它首先导入我制作的一个模块,该模块由一个名为“dice”的类组成,我用它来随机生成数字。接下来是定义等级,属性包括hp,maxhp,装甲等级,“击中装甲等级0”,突击调整和先攻调整,以及允许您攻击怪物的攻击,退出战斗的逃跑和治疗的方法这会给你的角色更多的生命值。程序继续定义生成 goblin 的 spawn_goblin() 函数(这在循环的第一次迭代中工作得很好)。然后进入非常简单的战斗部分;它所做的只是检查谁先走,然后让你攻击、逃跑或治愈自己。请忽略“治愈”和“逃离” 方法,这些工作得很好。当我攻击妖精时会出现问题。它没有杀死妖精并生成另一个妖精,而是创建了一个无限循环,上面写着“恭喜你杀死妖精”

标签: pythonfunctionoopmethodswhile-loop

解决方案


while (goblin.hp <= 0):
           print("congratulations you killed the goblin")

我认为您的这部分代码是错误的。while应该是,if因为您只想检查地精是否已经死亡,并运行一些代码一次。一旦妖精的生命值小于或等于0,该print语句将永远循环,因为表达式goblin.hp <= 0将始终返回True

编辑:现在我认为您的代码错误的地方是:

while attack_flee_heal != 'attack' or 'flee' or 'heal':
               if attack_flee_heal == 'attack':
                   MrHezy.attack(goblin1)
               elif attack_flee_heal == 'heal':
                   MrHezy.heal(MrHezy)
                    ...

我认为您错过了break第一个if(此外,在这里使用 an 而不是 aif attack_flee_heal == 'attack' 更合适,因为正如我上面提到的,您只想检查一次.ifwhile


推荐阅读