Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text Adventure
#1

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?"

Attached Files

Thumbnail(s)
   
Reply
#2
(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.
Running Manjaro Linux 16.08/OpenRC/i3wm on a Dell Precision 5510.

John 3:17
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Complete beginnner making a text based adventure Mishmaccles 2 2,635 Jul-07-2021, 05:00 PM
Last Post: BashBedlam
  Text Adventure Module Leroyrobenson 1 2,135 Feb-23-2020, 09:57 PM
Last Post: Larz60+
  Adding an inventory and a combat system to a text based adventure game detkitten 2 6,807 Dec-17-2019, 03:40 AM
Last Post: detkitten
  Advice for making a text choose your own adventure olivermcg 1 2,684 Apr-29-2019, 11:25 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020