我在制作文字冒险游戏。 我很困惑如何实现我的游戏的下一部分。 这里是我的旁边有加: “添加到捡起一个物体的能力,如果用户键入获取密钥,则:如何在Python中使用Split命令?
斯普利特用户输入,所以你拆出来,只是有一个变量等于以“钥匙”
1,采用的是内置到Python字符串分割方法,例如:。 command_words = user_command.split(”“)
这将什么类型的用户分成列表中的每个。项目根据空格分隔。
- 更新您的指示,以改为检查command_words [0]。
- 添加对get命令的检查。
2.Search列表,直到找到适合什么用户试图捡起一个物体。
这是我的代码至今:
done = False
object_list = []
room_list = []
class Object():
def __init__(self, name, description, room):
self.name = name
self.description = description
self.current_room = room
wand = Object("Holly Wand", "This wand is an 11\" supple wand made of holly and a single phoenix tail feather core.", 1)
object_list.append(wand)
dh_cloak = Object("Cloak of Invisibility", "This is part one part of the most powerful trifecta. \nThis cloak shields you from all wandering eyes. \nFind all three parts and you may just be able to escape from this castle.", 3)
object_list.append(dh_cloak)
class Room():
def __init__(self, describe, nw, n, ne, e, se, s, sw, w):
self.description = describe
self.northwest = nw
self.north = n
self.northeast = ne
self.east = e
self.southeast = se
self.south = s
self.southwest = sw
self.west = w
kitchen = Room("You are in the Kitchen. Look at the scrumptious roast chicken and kidney pudding! \nThere are doors leading to North, East, and West.", None, 4, None, 2, None, None, None, 0)
room_list.append(kitchen)
east_cooridor = Room("You apparated into the East Corridor. \nYou can apparate to the Northwest or the Southwest.", 8, None, None, None, None, None, 2, None)
room_list.append(east_cooridor)
great_hall = Room("You are in the Great Hall. What is that great smell? \nThere appears to be doors leading to the north and the south.", None, 7, None, None, None, 1, None, None)
room_list.append(great_hall)
owlery = Room("You are in the Owlery. WHOOs got mail? There is a glass door overlooking the Forbidden Forest. \nThere are doors in every direction.", None, 9, None, 8, None, 4, None, 6)
room_list.append(owlery)
room_list.append(forbidden_forest)
current_room = 4
while not done:
print(room_list[current_room].description)
key = current_room
i = 0
while i < len(object_list) and object_list[i].current_room != key:
i += 1
if i < len(object_list):
print("There is an object you can pick up in this room.")
print()
direction = input("Which direction would you like to travel? ")
print()
if direction.lower() == "n" and current_room == 9 or direction.lower() == "north" and current_room == 9:
print("You wandered too far into the Forbidden Forest with out all of the Deathly Hallows to protect you. \nYou have died.")
done = True
elif direction.lower() == "nw" or direction.lower() == "northwest":
next_room = room_list[current_room].northwest
if next_room == None:
print("TROLL! Troll in the dungeon!!")
else:
current_room = next_room
print()
elif direction.lower() == "n" or direction.lower() == "north":
next_room = room_list[current_room].north
if next_room == None:
print("Run away!!!!")
else:
current_room = next_room
print()
elif direction.lower() == "ne" or direction.lower() == "northeast":
next_room = room_list[current_room].northeast
if next_room == None:
print("Oh cool! Pixies! Wait...yikes! Bad idea!")
else:
current_room = next_room
print()
elif direction.lower() == "e" or direction.lower() == "east":
next_room = room_list[current_room].east
if next_room == None:
print("Don't go over there! The Whomping Willow is over there!")
else:
current_room = next_room
print()
elif direction.lower() == "se" or direction.lower() == "southeast":
next_room = room_list[current_room].southeast
if next_room == None:
print("Don't go in there...")
else:
current_room = next_room
print()
elif direction.lower() == "s" or direction.lower() == "south":
next_room = room_list[current_room].south
if next_room == None:
print("AHHH! It's Fluffy, the three-headed dog!")
else:
current_room = next_room
print()
elif direction.lower() == "sw" or direction.lower() == "southwest":
next_room = room_list[current_room].southwest
if next_room == None:
print("The third floor corridor is forbidden.")
else:
current_room = next_room
print()
elif direction.lower() == "w" or direction.lower() == "west":
next_room = room_list[current_room].west
if next_room == None:
print("I wouldn't go that way if I were you. You may run into something dangerous!")
else:
current_room = next_room
print()
elif direction.lower() == "q" or direction.lower() == quit:
done = True
else:
print("What kind of sorcery is this!? Try going an actual direction.")
print()
我一直奉行给我的方向,但我们没有教有关拆分命令,我无法找到任何解释它很好的在线。
我希望有人能够向我解释我如何使用split命令将用户键入的内容“拆分”为列表。我不太明白如何或为什么我会这样做。 任何建议从哪里去这里将不胜感激。谢谢!
如果您发布一个简洁的问题,您更有可能获得帮助。你可以很容易地删除一些法术或房间,它会让你更容易理解你的代码。 – Jaco
添加引起问题的简明代码部分也会有所帮助。 – Learner
请优化您的问题。 –