如何在Python2.7中创建基于回合的游戏循环

2024-09-29 01:19:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python开发一个游戏,现在我需要做一个游戏循环,在这个循环中,随机选择一个人先走,轮到他们执行。在

到目前为止,我的代码是:

当运行状况>0和运行状况1>0时:

    if turn == 1:
            while loopx == False:
                    try:
                            move = raw_input("Do you want to attack or gegenerate health? Press 1 to ATTACK and 2 to REGEN. ")
                            print ""
                            move = int(move)
                            if move == 1:
                                    health1 = health1 - damage
                                    print "You attacked!"
                                    loopx == True
                            elif move == 2:
                                    health = health+regen
                                    print "You regenerated health!"
                                    loopx = True
                            else:
                                    print "Invalid number, try again"
                                    continue
                    except:
                                    print "Invalid number, try again"
                                    continue
            turn == 2

    if turn == 2:
            AImove = r.randint(1,2)
            if AImove == 1:
                    print "AI attacked!"
                    health = health - damage1
            else:
                    print "AI regenerated!"
                    health1 = health1+regen1
            turn == 1
            continue

打印“游戏结束!”在

我使用的是python2.7。在


Tags: toyoutrue游戏moveifturn运行状况
1条回答
网友
1楼 · 发布于 2024-09-29 01:19:20

我在你的代码中看到的一个明显的问题就是你经常做错题。您使用的是==运算符,而不是=运算符。前者检查平等,而不是做作业。在

以下是修复此问题后的代码(请参阅注释了解修复位置):

while health > 0 and health1 > 0:

    if turn == 1:
            while loopx == False:
                    try:
                            move = raw_input("Do you want to attack or gegenerate health? Press 1 to ATTACK and 2 to REGEN. ")
                            print ""
                            move = int(move)
                            if move == 1:
                                    health1 = health1 - damage
                                    print "You attacked!"
                                    loopx = True                        # fix1
                            elif move == 2:
                                    health = health+regen
                                    print "You regenerated health!"
                                    loopx = True
                            else:
                                    print "Invalid number, try again"
                                    continue
                    except:
                                    print "Invalid number, try again"
                                    continue
            turn = 2                                                    # fix 2

    if turn == 2:
            AImove = r.randint(1,2)
            if AImove == 1:
                    print "AI attacked!"
                    health = health - damage1
            else:
                    print "AI regenerated!"
                    health1 = health1+regen1
            turn = 1                                                    # fix 3
            continue

print "game over!"

相关问题 更多 >