2016-07-28 5 views
0

Ich erstelle ein Rock, Paper, Scissors Spiel. Das Spiel hat ein Hauptmenü, zu dem ich von jedem Untermenü zurückkehren kann. Ich habe ein paar verschiedene Methoden ausprobiert, die ich mir vorstellen konnte, und habe hier und anderswo online nach einer Methode gesucht, um mein Problem zu lösen.Zurück zum Hauptmenü in meinem Spiel - Python

Ich möchte, dass der Benutzer in der Lage ist, eine Option aus dem Hauptmenü auszuwählen, zum ausgewählten Untermenü zu gehen und dann mit einer Option aufgefordert wird, zum Hauptmenü zurückzukehren. Wählen Sie beispielsweise das Untermenü Regeln und kehren Sie dann zum Hauptmenü zurück. Oder wählen Sie, um eine Runde Stein, Papier, Schere zu spielen, und wählen Sie, ob Sie erneut spielen oder zum Hauptmenü zurückkehren möchten.

# 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() 

Zur Zeit mein einziger Test ist im onePlayer (Modul). Die Idee hinter meinem Code besteht darin, den Benutzer zu fragen, ob er weiterspielen möchte. Wenn das nicht der Fall ist, möchte ich, dass das Programm sie zurück ins Hauptmenü bringt.

+0

Check die wieder jedes Mal Zustandes in Ihrer while-Schleife. – bhansa

+0

Ich habe den Code ein wenig aktualisiert, aber das Programm bleibt in der While-Schleife. Dieser Code befindet sich in der while-Schleife. – sedkil

+0

Bitte aktualisieren Sie Ihren Code mit der richtigen Bezeichnung und 'WEAPON' ist nirgendwo definiert. – bhansa

Antwort

0

Sie verwenden player Variable für zwei Arbeiten, stattdessen können Sie eine andere Variable verwenden, um nur die Bedingung zu überprüfen, und eine andere, Benutzereingaben zu nehmen.

Sie können auch den Zustand überprüfen wie: 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() 
+0

Nun bin ich mir nicht sicher was "wenn nochmal in [" ja "," y "]" bedeutet. Diese spezielle Syntax ist mir fremd. Ihre Anpassung an meinen Code funktionierte jedoch wie ein Zauber!Es ist genau das, wonach ich gesucht habe. Vielen Dank für Ihre Hilfe! – sedkil

+0

Lesen Sie über [ControlFlows] (https://docs.python.org/3/tutorial/controlflow.html) – bhansa

0

Versuchen Sie es und außer Befehl. Wenn sie Nein sagen, sollte dein Code beendet werden(). Wenn sie ja sagen, setzen Sie einen Fortsetzungsbefehl und es wird das Ganze neu starten.

+0

Ich erstelle dieses Programm für eine Klasse, die ich nehme. Leider wurde das, was Sie vorgeschlagen haben, nicht gelehrt. Ich habe Ihren Vorschlag nachgeschlagen, aber ich bin mir nicht sicher, wie ich das umsetzen kann, um mein Problem zu lösen. – sedkil

0

Ihre Hauptmethode in einer while (True): Schleife setzen, und wenn Option 4 genannt wird, verwenden break wie folgt aus:

elif menuSelect == 4: 
     break 

fügen Sie einen Gedankenstrich

hinzu

und anstatt main() nur player = True aufrufen, auch Ihre Waffe Array wurde nicht definiert einfache Lösung, fügen Sie einfach WEAPON = ["rock", "paper", "scissors"] an den Anfang Ihrer onePlayer(): Methode. Ich kann ein anderes Problem sehen,

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

zu

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

eine letzte Sache ändern, vergessen Sie nicht, Ihre Importe! (Put es an der Spitze des Codes.)

from random import randint 

BTW, die break Aussage sagt nur Python, was für Schleife zu verlassen oder while-Schleife drin ist.

+0

Vielen Dank für Ihr Feedback! Ihre Antwort ist für jemanden wie mich, der relativ neu in der Programmierung ist, wirklich einfach zu verstehen, und das schätze ich sehr. – sedkil

Verwandte Themen