Python Forum

Full Version: Text Adventure
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Hi I have a problem. I am making a similar game to a dragon realm (https://inventwithpython.com/chapter6.html) But I always have a two choice (correct and wrong)
Each time a player heads down to a wrong path I'd like the playAgain function. I can load the whole code if needed. Now I have sys.exit(), but can I have it as a playAgain function?
import time
import sys

def displayIntro():

    print("EMERGENCY PREPAREDNESS COURSE - SHELTERING TEXT ADVENTURE GAME")

    time.sleep(3)
     
    print("")
    print("")
    print("It's a normal day at school")

    print("You are outside during the break")

    print("Suddenly you and your friends hear a high noice")

    print("It is the public speakers, the tone goes up and down")

    print("It is extremely loud")

    print("Maybe it is better to go inside...")

    print ("1 = go inside")
    print ("2 = stay outside")
    
def choosePath():
    path = ""
    while path !="1" and path !="2": # input validation
        path = input("Do you go inside or stay outside? (1 or 2): ")
    
        return path

def checkPath(chosenPath):
    print ("")
    print()
    time.sleep(1)

    correctPath = 1

    if chosenPath == str(correctPath):
        print("You are going inside")
        print("You are heading to your classroom")

    else:
        print("You chose to stay outside")
        print("There is a white mist coming your way")
        print("You feel a tingling on your face")
        print("Now it's too late")
        print("I should've gone insiiiiiiide!")
        print("GAME OVER, START AGAIN")
        sys.exit()



playAgain = "yes"
while playAgain == "yes" or playAgain == "y":
    displayIntro()
    choice = choosePath()
    checkPath(choice) # choice is equal to "1" or "2"
    choice = choosePath2()
    checkPath2(choice) # choice is equal to "1" or "2"
    choice = choosePath3()
    checkPath3(choice) # choice is equal to "1" or "2"
    choice = choosePath4()
    checkPath4(choice) # choice is equal to "1" or "2"
    displayMed()
    choice = choosePath5()
    checkPath5(choice) # choice is equal to "1" or "2" or "3"
    playAgain = input("Do you want to play again? (yes or y to continue): ")
n. Currently my game includes 4 path choices. So how do I ask in the middle of the game to "play again?"
(Sep-24-2016, 10:35 PM)Yelmos Wrote: [ -> ]
    displayIntro()
    choice = choosePath()
    checkPath(choice) # choice is equal to "1" or "2"
    choice = choosePath2()
    checkPath2(choice) # choice is equal to "1" or "2"
    choice = choosePath3()
    checkPath3(choice) # choice is equal to "1" or "2"
    choice = choosePath4()
    checkPath4(choice) # choice is equal to "1" or "2"
    displayMed()
    choice = choosePath5()
    checkPath5(choice) # choice is equal to "1" or "2" or "3"

What I would do is put this section of code into a function and create a custom exception. Then instead of calling sys.exit(), you can raise your GameFailed exception and catch it inside your while loop. There are some other ways to solve this problem, but this one requires the least refactoring.