这是一个基于文本的游戏。
我有一个Room类,带有一个名为choose(self,room,choice)的函数
每个其他房间都是一个子类,也有一个choose()函数,并且我已经设置了它,以便各个房间从用户的输入中运行它们自己的代码,但是通用的房间,比如检查库存和打印命令列表都要经过超类,所以每个房间都是这样的
class LivingRoom:
def __init__(self):
...
...
def show(self):
...
...
def choose(self,choice): #'choice' is whatever the play inputs
if "help" or "inventory" or "take" or "drop" or "look" or "attack" in choice:
Room.choose(Room,self,choice) #It pass the current room and the player's
#input into the function
elif "north" in choice:
...
...
elif "west in choice:
...
...
else:
print("I don't know what you want to do.")我遇到的问题是,无论玩家的输入是什么,它总是运行第一条IF语句并进入Room.choose函数。
我通过取出所有or语句对它进行了测试,结果表现良好,因此出于某种原因,使用or语句会使if语句始终运行。
当我把所有的选择都放在括号里时
if ("help" or "inventory" or "take" or "drop" or "look" or "attack") in choice:
Room.choose(Room,self,choice)它解决了这个问题,因此它只在我想要的时候运行if语句,但引用当前房间的任何选项,如“take”,“drop”,“look”和“attack”(所有这些引用当前房间检查玩家想要与之互动的任何物品或敌人在该房间中)没有运行。
“帮助和”库存“工作正常,因为他们没有参考当前房间。
如果有帮助,我会完整发布Room.choose()功能。
def choose(self,room,choice):
print(" \n{{{ROOM FUNCTION RUNNING.}}}") #I put this in so I could debug and it's what showed my that my IF statement was running constantly
if "quit" in choice or choice == "q":
run = False
elif "help" in choice:
print(" [ COMMANDS ]")
print("'NORTH', 'SOUTH', 'EAST', 'WEST': move in that direction.\n")
print("'UP', 'down': move up or down a staircase,ladder,etc.\n")
print("'TAKE' or 'PICK UP' places an item in the room in your inventory. 'DROP' places an item from your inventory into a room.\n")
print("'I' or 'INVENTORY' displays your currently held items\n.")
print("'LOOK' replays a room description.\n")
print("'HIT' or 'ATTACK' to attack enemies.")
elif "inventory" in choice or choice == "i":
if P.inventory:
for i in P.inventory:
print("[ " + i.name + " ]")
elif not P.inventory:
print("")
print("Your aren't carrying anything.")
elif "intro" in choice:
showIntro()
elif "look" in choice:
room.show()
elif "take" in choice:
counter = 0
for i in room.items:
if i.name.lower() in choice:
counter = 1
P.inventory.append(i)
room.items.remove(i)
print("\nYou took a " + i.name.lower())
if counter == 0:
print("\nThere isn't anything like that here.\n")
elif "drop" in choice:
counter = 0
for i in P.inventory:
if i.name.lower() in choice:
counter = 1
P.inventory.remove(i)
room.items.append(i)
print("\nYou dropped a " + i.name.lower() )
if counter == 0:
print("\nYou're not carrying an item like that.")
for i in room.items:
if i.name.lower() in choice:
print("\n" + i.examine)
for i in P.inventory:
if i.name.lower() in choice:
print("\n" + i.examine)