为游戏创建while循环

时间:2013-05-08 02:35:44

标签: python

我是Python的新手,我对我今天一直在努力的代码提出了一个简单的问题。我正在创建一个使用while循环的游戏,其中两个对手将轮流彼此“击中”,直到其中一个玩家拥有0个健康点。

这是我到目前为止的代码,虽然它不起作用。有人可以帮忙吗?

done=False
while not done:
    if You.Hit_points>Opponent.Hit_points:
        move=raw_input("Would you like to make a move? (y/n) ")
        if move=="y":
            print "",You.name,"hit ",Opponent.name," by",You.Hit_points," hit points!"
            Opponent.Health=You.Hit_points+You.Skill_points+Opponent.Health
            print "Due to the hit",Opponent.name,"is left with",Opponent.Health," health points."
            print ("The Mighty Beast will make a move.")
            print "",Opponent.name,"hits",You.name,"by",Opponent.Hit_points,"points"
            You.Health=(Opponent.Hit_points-Opponent.Skill_points)+You.Health
            print "Due to the hit",You.name,"loses",Opponent.Hit_points,"points.Leaving",You.name,"with",You.Health,"health points."
            print "Now it is",Opponent.name,"'s turn to make a move"
            You.Health=You.Health-(Opponent.Hit_points+Opponent.Skill_points)
            print "Due to the hit",You.name,"is left with",You.Health,"health points."
    else:
        You.Hit_points==0
        move=="n"
        done=True

1 个答案:

答案 0 :(得分:0)

if You.Hit_points>Opponent.Hit_points:

应该是

if You.Hit_points > 0 and Opponent.Hit_points > 0

正确?否则只要You的HP降到Opponent以下 - 这可能是在第一次转弯之后 - 战斗就会结束。