重玩游戏

问题描述 投票:0回答:1

如何让我的游戏循环重播?是否有可能玩家看不到对方的答案或者同时添加?这是《Strategy Steps》、《Wii Party》的重制版,我想(在最后)问玩家是否想再玩一次游戏。

import time

def game():
    welcome = input("Welcome to Wii Party: Strategy Steps! Would you like to know how to play this game? Type 'yes' or 'no': ")
    if welcome == "Yes" or welcome == "yes":
        time.sleep(1)
        print("Two players choose between three numbers (1,3,5).\nIf a player selects the same number as another player, both players stay where they are.\nIf both numbers are different, the players will move up in accordance to the number they have selected.\nFor example, if players 1-3 (in 4 player situation) choose 3 and player 4 chooses 5, player 4 will move up by 5 steps but players 1-3 will stay where they are.\nThere are 20 steps altogether.\nThe first player to reach the top of the staircase is the winner.")
    else:
        time.sleep(1)
        print("Let's move on!")
    totalsteps = 20 
    player1position = 0
    player2position = 0
    options = [1, 3, 5]

    while player1position < totalsteps and player2position < totalsteps:
        player1choice = int(input("Player 1, choose a number between 1, 3 and 5: "))
        while player1choice not in options:
            print("That is not one of the options. Try again.")
            player1choice = int(input("Player 1, choose a number between 1, 3 and 5: "))
            if player1choice in options:
                break
            else:
                pass
        time.sleep(1)
        player2choice = int(input("Player 2, choose a number between 1, 3 and 5: "))
        while player2choice not in options:
            print("That is not one of the options. Try again.")
            player2choice = int(input("Player 2, choose a number between 1, 3 and 5: "))
            if player2choice in options:
                break
            else:
                pass
        
        if player1choice != player2choice:
            player1position += player1choice
            player2position += player2choice
            print("Player 1 has moved up ", player1choice, "steps and is now on step", player1position,"!")
            time.sleep(1)
            print("Player 2 has moved up ", player2choice, "steps and is now on step", player2position,"!")
        else:
            time.sleep(1)
            print("The same number was picked so no one moved")
        if player1position >= totalsteps:
            print("Player 1 wins!")
            break
        elif player2position >= totalsteps:
            print("Player 2 wins!")
            break
        
game()
python loops
1个回答
0
投票

通常,您会将整个游戏逻辑放入一个循环中,该循环在提示用户是否继续之前至少运行一次。

def loop(game):
    while True:
        game()
        response = input("Play again? ").lower()
        if response in ('n', 'no'):
            break
© www.soinside.com 2019 - 2024. All rights reserved.