回到我游戏中的主菜单 - Python

时间:2016-07-28 04:31:19

标签: python menu

我正在制作Rock,Paper,Scissors游戏。游戏有一个主菜单,我需要从每个子菜单返回。我已经尝试了一些我能想到的方法,以及在线查看和其他地方确定解决问题的方法。

我希望用户能够从主菜单中选择一个选项,转到所选的子菜单,然后会出现一个选项以返回主菜单。例如,选择规则子菜单,然后返回主菜单。或者,选择播放一轮Rock,Paper,Scissors,然后选择再次播放或返回主菜单。

# random integer
from random import randint

# list for weapon
WEAPON = ["Rock", "Paper", "Scissors"]

# module to run the program
#def main():
#    menu()

def main():
menuSelect = ""
print("\tRock, Paper, Scissors!")

# main menu
print("\n\t\tMain Menu")
print("\t1. See the rules")
print("\t2. Play against the computer")
print("\t3. Play a two player game")
print("\t4. Quit")

menuSelect = int(input("\nPlease select one of the four options "))

while menuSelect < 1 or menuSelect > 4:
    print("The selection provided is invalid.")
    menuSelect = int(input("\nPlease select one of the four options "))

if menuSelect == 1:
    rules()
elif menuSelect == 2:
    onePlayer()
elif menuSelect == 3:
    twoPlayer()
elif menuSelect == 4:
    endGame()

# display the rules to the user
def rules():

print("\n\t\tRules")
print("\tThe game is simple:")
print("\tPaper Covers Rock")
print("\tRock Smashes Scissors")
print("\tScissors Cut Paper")
print("")

# one player mode
def onePlayer():
again = ""
player = False

print("\n\tPlayer VS Computer")

while player == False:
    player = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
    player = player.lower()

    computer = WEAPON[randint(0,2)]
    computer = computer.lower()

    if player == computer:
        print(player," vs ",computer)
        print("It's a tie!\n")
    elif player == "rock":
        if computer == "paper":
            print(player," vs ",computer)
            print("Paper covers rock! You lose!\n")
        else:
            print("Rock smashes",computer,". You win!\n")     
    elif player == "paper":
        if computer == "scissors":
            print(player," vs ",computer)
            print("Scissors cut paper! You lose!\n")
        else:
            print("Paper covers",computer,". You win!\n")
    elif player == "scissors":
        if computer == "rock":
            print(player," vs ",computer)
            print("Rock smashes scissors! You lose!\n")
        else:
            print("Scissors cut",computer,". You win!\n")
    else:
        print("invalid input")

    again = input("Would you like to play again? Yes or no\n")
    again = again.lower()

    if again == "yes" or "y":
        player = False
    elif again == "no" or "n":
        main()


# two player mode
def twoPlayer():
fight = False
player1 = ""
player2 = ""

print("\n\tPlayer VS Player")

while fight == False:
    player1 = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
    player1 = player1.lower()
    player2 = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
    player2 = player2.lower()

    if player1 == player2:
        print(player1," vs ",player2)
        print("It's a tie!\n")
    elif player1 == "rock":
        if player2 == "paper":
            print(player1," vs ",player2)
            print("Paper covers rock! Player 2 wins!\n")
        else:
            print("Rock smashes",player2,". Player 1 wins!\n")     
    elif player1 == "paper":
        if player2 == "scissors":
            print(player1," vs ",player2)
            print("Scissors cut paper! Player 2 wins!\n")
        else:
            print("Paper covers",player2,". Player 1 wins!\n")
    elif player1 == "scissors":
        if player2 == "rock":
            print(player1," vs ",player2)
            print("Rock smashes scissors! Player 2 wins!\n")
        else:
            print("Scissors cut",player2,". Player 1 wins!\n")
    else:
        print("invalid input")

    again = input("Would you like to play again? Yes or no\n")
    again = again.lower()

    if again == "yes" or "y":
        player = False
    elif again == "no" or "n":
        main()

def endGame():
print("Thank you for playing!")

main()

目前我唯一的测试是在onePlayer()模块中。我的代码的想法是询问用户是否要继续玩。如果没有,那么我希望程序将它们带回主菜单。

3 个答案:

答案 0 :(得分:0)

尝试一下,除了命令。如果他们说你的代码不应该退出()。如果他们说是,则输入一个继续命令,它将重新启动整个事情。

答案 1 :(得分:0)

您正在使用player变量进行两项工作,而不是您可以使用另一个变量来检查条件而另一个变量用于输入用户。

您还可以查看以下条件: if again in ["yes","y"]

def main():
    menuSelect = ""
    print("\tRock, Paper, Scissors!")

# main menu
    print("\n\t\tMain Menu")
    print("\t1. See the rules")
    print("\t2. Play against the computer")
    print("\t3. Play a two player game")
    print("\t4. Quit")

    menuSelect = int(input("\nPlease select one of the four options "))

    while menuSelect < 1 or menuSelect > 4:
        print("The selection provided is invalid.")
        menuSelect = int(input("\nPlease select one of the four options "))

    if menuSelect == 1:
        rules()
    elif menuSelect == 2:
        onePlayer()
    elif menuSelect == 3:
        twoPlayer()
    elif menuSelect == 4:
        endGame()

# display the rules to the user
def rules():

    print("\n\t\tRules")
    print("\tThe game is simple:")
    print("\tPaper Covers Rock")
    print("\tRock Smashes Scissors")
    print("\tScissors Cut Paper")
    print("")

# one player mode
def onePlayer():
    again = ""
    player = False

    print("\n\tPlayer VS Computer")

    while player == False:
        player = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
        player = player.lower()

        #computer = WEAPON[randint(0,2)]
        #temporary
        computer = "paper"
        computer = computer.lower()

        if player == computer:
            print(player," vs ",computer)
            print("It's a tie!\n")
        elif player == "rock":
            if computer == "paper":
                print(player," vs ",computer)
                print("Paper covers rock! You lose!\n")
            else:
                print("Rock smashes",computer,". You win!\n")     
        elif player == "paper":
            if computer == "scissors":
                print(player," vs ",computer)
                print("Scissors cut paper! You lose!\n")
            else:
                print("Paper covers",computer,". You win!\n")
        elif player == "scissors":
            if computer == "rock":
                print(player," vs ",computer)
                print("Rock smashes scissors! You lose!\n")
            else:
                print("Scissors cut",computer,". You win!\n")
        else:
            print("invalid input")

        again = input("Would you like to play again? Yes or no\n")
        again = again.lower()

        if again=="yes" or again=="y":
            player = False
        else:
            player = True
            main()


main()

答案 2 :(得分:0)

将您的main方法放在while (True):循环中,如果调用了选项4,请使用break,如下所示:

elif menuSelect == 4:
        break

添加缩进

again = input("Would you like to play again? Yes or no\n")
again = again.lower()

if again == "yes" or "y":
    player = False
else:
    main() 

而不是仅仅调用main()设置player = True,而您的Weapon数组尚未定义。只需将WEAPON = ["rock", "paper", "scissors"]添加到onePlayer():方法的开头即可轻松修复。我可以看到另一个问题,改变

if again == "yes" or "y":

if again == "yes" or again == "y":

最后一件事,不要忘记你的进口! (把它放在代码的顶部。)

from random import randint
顺便说一句,break语句只是告诉python留下任何for循环或while循环它。

相关问题