[英]how to make a raw_input call a function
我正在修补我自己创作的游戏,但遇到了这个问题。 我正在以一种非常互动的小说风格制作游戏,但我无法让 raw_input 调用像take sword
to take(sword, room)
这样的函数来将它从room
移除并进入inventory
。 首先,这是我用于游戏调用以连续调用raw_input
:
GameOn=True
while GameOn==True:
a=raw_input("C:\>")
a=a.lower()
if a=="look":
look()
接下来是功能:
def take(item,room):
if item in tools:
if item[0] in room:
room.remove(item[0])
inventory.append(item[1])
print "taken"
else:
print item, "doesn't exist in this game"
def look(room):
place=areas[room]
for i in place:
print i
现在是列表:
sword=["There is a sword here", "sword"]
room=["There is a table in the corner", sword[0]]
inventory=[]
areas=[room,bedroom,living_room]
tools=[sword, shield, axe]
tools
用于显示游戏中的内容以及可以使用的内容。 areas
和noun
列表中的项目不止一个,因为一个房间和一个项目的游戏很无聊,所以忽略noun
和areas
中不是sword
或room
。 剑有两个列表的原因是sword[0]
出现在room
而sword[1]
出现在inventory
。 前任。
print inventory
"sword"
look(room)
"There is a table in the corner"
"There is a sword here"
以及它在游戏中的样子:
C:\>look
"There is a table in the corner"
"There is a sword here"
我可以让take(sword, room)
工作(如下所示),所以我知道这不是代码:
>>>room=["There is a table in the corner, sword[0]]
>>>inventory=[]
>>>look(room)
>"There is a table in the corner"
>"There is a sword here"
>>>print inventory
>
>>>take(sword, room)
>>>look(room)
>"There is a table in the corner"
>>>print inventory
>"sword"
我添加了“>>>”以显示什么是变量或函数调用,并添加“>”以显示响应。
既然已经涵盖了,我是否要重写一些代码,我是否在raw_input
做错了什么,我是否误解了raw_input
的功能,或者如果没有,我如何让take sword
工作?
看起来你做得很好。 我不确定我是否完全理解,但我认为您想跟踪用户所在的当前房间(我不确定您是否已经这样做了,尚未指定) )。 一旦你这样做,你已经拥有了用户的输入存储在a
。
然后,您希望按空格分割输入(假设用户输入正确的响应“take Sword”)并检查第一个单词是否为“take”。 然后,如果第一个词是“take”,那么您就可以调用您的方法。
a = raw_input("C:\>")
a = a.lower()
if a.split(' ')[0] == 'take': # split the array by spaces
# (if the input is 'take sword', then [0] will be 'take' and [1] will be 'sword'
take(a.split(' ')[1], current_room)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.