如何在玩家之间轮流

时间:2018-12-15 21:03:05

标签: python

假设我有list个玩家,list_of_players = [1,2,3,4],每个数字代表一个玩家。基本上,我想要做的是每次运行变成while run:时,使每个玩家都经过True(在程序运行中,只要您按Enter键就变为true)。因此,对于第一回合,我希望player变为1,并且当循环中断并且再次按Enter时,我希望播放器变为2,依此类推。当播放器达到4时,我希望4通过循环并重置播放器= 4玩家= 1(第二回合)。我已经尝试了许多方法,但是无法正常工作。所以问题是我该怎么做? 各种各样的帮助表示赞赏。

    while run:
        message.append(display_turn(player))
        new_position = return_postion(player) + int(rolled_number[-1])
        updating_position(player,new_position)
        if level_1[return_postion(player)]== 15:
            message.append('You landed on a trap')
            updating_health(player,0,15)
        break

1 个答案:

答案 0 :(得分:2)

您正在寻找模运算符

max_players = 4

player = 0

while True:
    print("player {} turn".format(player+1))

    input("enter for next {}".format("player" if player + 1 < max_players else "round"))
    player = (player + 1) % max_players

输出

player 1 turn
enter for next player 
player 2 turn
enter for next player 
player 3 turn
enter for next player 
player 4 turn
enter for next round 
player 1 turn
enter for next player 
player 2 turn
enter for next player 
player 3 turn
enter for next player 
player 4 turn
enter for next round  
...