为什么这个程序重演?

时间:2012-10-11 21:00:13

标签: python

我正试图通过LPTHW中的练习36开始游戏。

我可以让游戏的整个部分工作,除非它进入战斗。这是我认为问题所在。

def combat_engine():    
    global wins, hit_points, current_hp
    if monster_current_hp or current_hp > 0:
        print "You can type 'a' for attack, or 'q' for quit, choose one."
        answer = raw_input(":> ")
        if 'a' in answer:
            attack(monster)
            attack('player')
            combat_engine()
        elif 'q' in answer:
            print t.white_on_red("You give up and are devoured by the %s.") % monster
            exit(0)
        else:
            print "I dont understand %s." % answer
            next()
            combat_engine() 
    elif monster_hp == 0:
        print "You defeated the %s, congradulations %s!" % monster, name
        wins = wins + 1
        if wins == 5:
            you_win()
        elif victim == 'player':
            hit_points = hit_points + d6()
            current_hp = hit_points
            print "You feel hardier."
            next()
            barracks()
    elif current_hp == 0:
        print "You have been deafeted by the %s, better luck next time." % monster
        next()
        exit(0)

    else: 
        print "Bug Somewhere"

我相信这个bug就在这个函数的某个地方。当我打印出每个角色的HP值时,在怪物降低到-2马力之后,战斗仍在继续。也许这是布尔人的问题?

我希望它做的是对获胜进行所有统计调整,如果输了则退出游戏。我只是想通过这个,所以我可以开始学习课程和决定,这将使我的生活更轻松。如果我应该发布更多信息,请告诉我,我是新手,而不是最好在这里发布问题。

提前致谢!

2 个答案:

答案 0 :(得分:2)

if monster_current_hp or current_hp > 0:有两个方面存在缺陷:首先,它只是测试monster_current_hp是否为真,而不是对0进行测试;第二,即使修复了,如果EITHER战斗机有正HP,声明将继续该程序,而大概你想要在任何一个战斗机有负hp时停止。请改为尝试:if monster_current_hp > 0 and current_hp > 0:

一旦你这样做,在大多数情况下,你将开始进入“某处错误”行。这是因为当HP从正面变为负面时,elif monster_hp == 0:elif current_hp == 0:不会触发。在这两种情况下都使用<=来捕获HP为负数的实例。

答案 1 :(得分:1)

在代码的最后部分,您不会调用函数 d100(),而是调用值d100。这肯定不是你想要做的。

   if d20() >= monster_to_hit and d100() <= monster_crit:
        print "The %s scored a critical hit against you!" % monster
        hit_points = hit_points - (monster_dmg * 3)
        next()
    elif d20 >= monster_to_hit and d100() > crit:
        print "The %s strikes you!"
        hit_points = hit_points - monster_dmg
        next()

在python中调试时,主要词是“print”。您应该尽可能多地打印,以便了解正在发生的事情。

示例:

# attack function
def attack(victim):
    dice20 = d20()
    dice100 = d100()

    print "victim == monster", victim == monster    

    print dice20
    print dice100
    print to_hit
    print crit
    print monster_current_hp
    print mod_dmg 
    print mod_dmg * 3
    print monster_to_hit
    print monster_crit
    print hit_points
    print monster_dmg
    print monster_dmg * 3

    global monster_current_hp, current_hp, to_hit, crit, hit_points
    if victim == monster:
        if dice20 >= to_hit and dice100 <= crit:
            print "You scored a critical hit against the %s!" % monster
            monster_current_hp = monster_current_hp - (mod_dmg * 3)
            next()
        elif dice20 >= to_hit and dice100 > crit:
            print "You strike the %s!" % monster
            monster_current_hp = monster_current_hp - mod_dmg
            next()
        else:
            print "The %s evades your attack!" % monster
            next()
    else:
        if dice20 >= monster_to_hit and dice100 <= monster_crit:
            print "The %s scored a critical hit against you!" % monster
            hit_points = hit_points - (monster_dmg * 3)
            next()
        elif dice20 >= monster_to_hit and dice100 > crit:
            print "The %s strikes you!"
            hit_points = hit_points - monster_dmg
            next()

在战斗引擎中做同样的事情,以便看到无限循环出现。然后删除无用的打印,使用无用的信息膨胀你的跟踪,直到你清楚地看到它循环的原因,因为有一些值,因为一个布尔值,如果测试没有你预期的那样......就像那样。< / p>