游戏中的while循环问题。

时间:2016-11-24 04:44:19

标签: python while-loop adventure

我正在制作游戏,它显然有不同的瓷砖。但是我在主游戏中遇到了一个问题。

C:\Users\pp9x2>netstat -an | findstr 12345
TCP    192.168.56.1:12345     0.0.0.0:0              LISTENING

我游戏中的平铺示例:

def play():
    player  = player1()
    while True:
        room = ClubWorld.tile_at(player.x, player.y)
        print(room.intro_text())
        choose_action(room, player)

只要我在游戏牌上,这个循环就会继续。它会生成您可以执行的操作,并让其他功能知道您的位置。它还输出瓦片介绍文本,我的问题所在。我希望游戏只在输入图块时输出介绍文本,一旦发生这种情况,我只希望它显示可用的动作。建议?

1 个答案:

答案 0 :(得分:1)

您可以保留previous_room并与room进行比较:

def play():
    player  = player1()
    previous_room = None
    while True:
        room = ClubWorld.tile_at(player.x, player.y)
        if room != previous_room:
            print(room.intro_text())
            previous_room = room
        choose_action(room, player)

或保持玩家之前的位置previous_xprevious_y并与新位置进行比较

def play():
    player = player1()
    previous_x = None
    previous_y = None
    while True:
        if player.x != previous_x or player.y != previous_y :
            room = ClubWorld.tile_at(player.x, player.y)
            print(room.intro_text())
            previous_x = player.x
            previous_y = player.y
        choose_action(room, player)