I recently started learning pythong with Udemys 100 days course and i wanted to build a project based on the stuff in the course.
so i tired my hands on a text adventure. And everything is going good up untill the part where your character have to pick a door.
print("What to do you do? \n")
print("To open the Wooden door, enter: d1 \n ")
print("To open the Metal door, enter: d2 \n ")
print("To read whats on the wall, enter: r1 \n ")
while True:
c1 = input("\n d1 \n d2 \n r1 \n> ")
if c1 == "d1":
time.sleep(0.5)
print("you enter the wooden door . \n ")
# continue advanture in file1
if c1 == "d2":
time.sleep(0.5)
print("you enter the metal door. \n ")
# continue advanture in file2
elif c1 == "r1":
print("You walk closer to the wall and see that it has a bunch of names carved in to it")
print("most of the names are crossed over with a red X..")
print("your eyes quickly goes to the end of the list and see something that sends a chill down youw spine")
print("the name " nm " carved in to the stone wall")
print("without a red X... \n")
print("*You go back to the center of the room*")
else:
print(" Please Enter Either d1, d2 or r1 ")
I know how to exit with the using "break" but how do i continue on different paths so to say ?
My first idea was to open a new file or something but that dosent feel right
I tried to google but having a hard time finding what i want
CodePudding user response:
I tried the following
def doorChoice(): c1 = input("\n d1 \n d2 \n r1 \n> ") if c1 == "d1": time.sleep(0.5) print("you enter the wooden door . \n ") return woodenDoor() if c1 == "d2": time.sleep(0.5) print("you enter the metal door. \n ")
return metalDoor()elif c1 == "r1": # print what you read on the wall return doorChoice() else: print(" Please Enter Either d1, d2 or r1 ") def woodenDoor(): print("woodwork") def metalDoor(): print("metalwork")
Without the while True . But that exits the code instantly. with the while True i get a red line under
def doorChoice():
CodePudding user response:
Define a function for the interaction in every room.
Use a dictionary to store the global gamestate. The gamestate includes what room the player is currently in.
The main loop calls the appropriate function for every room based on the gamestate. To move from one room to another, update the information in the gamestate.
Pass the gamestate to each function so that it can access and update it.
import time
# function for start room
def startroom(gs):
print("You are in a dark room. \n")
print("What to do you do? \n")
print("To open the Wooden door, enter: d1 \n ")
print("To open the Metal door, enter: d2 \n ")
print("To read whats on the wall, enter: r1 \n ")
c1 = input("\n d1 \n d2 \n r1 \n> ")
if c1 == "d1":
time.sleep(0.5)
print("you enter the wooden door . \n ")
# Move player to hallway
gs["room"] = "hallway"
elif c1 == "d2":
time.sleep(0.5)
print("you enter the metal door. \n ")
# Move player to exit
gs["room"] = "exit"
elif c1 == "r1":
print("You walk closer to the wall and see that it has a bunch of names carved in to it")
print("most of the names are crossed over with a red X..")
print("your eyes quickly goes to the end of the list and see something that sends a chill down youw spine")
print("the name " gs["playername"] " carved in to the stone wall")
print("without a red X... \n")
print("*You go back to the center of the room*")
# function for hallway
def hallway(gs):
print("You are in a hallway. \n")
print("What to do you do? \n")
print("To go trough the wooden door, enter: d1 \n ")
print("To open the glass door, enter: d2 \n ")
c1 = input("\n d1 \n d2 \n> ")
if c1 == "d1":
time.sleep(0.5)
print("you go through the wooden door . \n ")
gs["room"] = "start"
elif c1 == "d2":
time.sleep(0.5)
print("You try to open the glass door. \n ")
print("It is locked. \n ")
####################### MAIN APPLICATION STARTS HERE ###########################
# globval variables that represent the state of the game
gamestate = {}
#start in the start room
gamestate["room"] = "start"
#init player name
gamestate["playername"] = "NikkoV" # TODO: Read that from console
# main loop. Depending on current room call function for room
while gamestate["room"] != "exit":
if gamestate["room"] == "start":
startroom(gamestate)
elif gamestate["room"] == "hallway":
hallway(gamestate)
else:
#unknown room, return to start
gamestate["room"] = "start"
print("You have left the maze.\n ")
If the player picks up an item or unlocks a locked door or does any other activity, to store that information in the gamestate dictionary so that the game can react appropriately.
CodePudding user response:
You could use a different file however I would suggest the following:
After the door has been selected you call a function, eg. doorWooden() for the wooden door, from which point you can continue.
Also consider moving the door choice to a function, like so:
def doorChoice():
c1 = input("\n d1 \n d2 \n r1 \n> ")
if c1 == "d1":
return woodenDoor()
if c1 == "d2":
return metalDoor()
elif c1 == "r1":
# print what you read on the wall
return doorChoice()
else:
print(" Please Enter Either d1, d2 or r1 ")
return doorChoice()
# example for woodenDoor()
def woodenDoor():
print("you chose the wooden door")
while True :
# this is the game loop, call your functions here
# ...
doorChoice()
As per your other question, I added the game loop below this should be what you seek I hope that helps you.
CodePudding user response:
Maybe try something like this:
doorchoice = input ("What do you want to do:\n")
wood = "d1"
metal == "d2"
wall_paper == "r1"
if doorchoice == wood:
time.sleep(1) #Try it with 1 second
print ("You entered wooden door.\n")
elif doorchoice == metal:
time.sleep(1)
print ("You entered metal door.\n")
elif doorchoice == wall_paper:
time.sleep(1)
print ("You see paper\n") #and that things you typed in
else:
print ("You must enter valid character.\n")