在Python的不同文件或行中继续



我最近开始用Udemys的100天课程学习python,我想基于课程中的内容构建一个项目。

所以我累了我的手在文本冒险。一切都很顺利,直到你的角色不得不选择一扇门。

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

我知道如何使用"break"但是我该如何在不同的道路上继续前行呢?

我的第一个想法是打开一个新文件或其他东西,但这感觉不对

我试着用谷歌搜索,但是很难找到我想要的

为定义一个函数每个房间的交互.

使用字典来存储全局游戏属性. 游戏属性包括玩家当前所在的房间。

主循环根据游戏属性为每个房间调用适当的函数。要从一个房间移动到另一个房间,请更新游戏属性中的信息。

将游戏属性传递给每个函数,以便它可以访问和更新它。

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

如果玩家捡到一件物品或打开一扇锁着的门或做任何其他活动,将这些信息存储在gamestate字典中,以便游戏能够做出适当的反应。

您可以使用不同的文件,但我建议如下:

选择门后调用函数,例如:doorWooden ()对于木门,从哪一点可以继续。

还可以考虑将门的选择移动到一个函数中,如下所示:

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

根据你的其他问题,我在下面添加了游戏循环,这应该是你所寻求的我希望这对你有帮助。

可以试试这样做:

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 papern") #and that things you typed in
else:
print ("You must enter valid character.n")

最新更新