gpt4 book ai didi

python 3 : How to remove items from an inventory in a text-based game?

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:22 26 4
gpt4 key购买 nike

作为个人项目,我一直在开发一款类似 Zork 的基于文本的冒险游戏,目的是通过“速成类(class)”自学 Python。我是一般编码的新手,所以我对基础知识很模糊。

我已经在我的游戏中成功导航我创建的 map ,并可以自由添加新房间/项目。我目前正在努力使“库存”系统正常工作。

目前,我的 Room 类中有一个名为“roominv”的字典,它用作每个特定房间的 list 。我在播放器类中也有一个名为“包”的字典,用作播放器 list 。

目前游戏存在问题且未完成,因此要拾取元素你必须拾取当前房间中的所有元素,但这不是我遇到的问题。

一旦玩家拿起那个房间里的元素,我不知道如何清空 roominv 字典。现在我可以将元素添加到玩家包中,但它会创建一个副本而无需将它们从 roominv 中移除。

我尝试过使用 del、delattr 和 pop,但要么我没有正确使用它们(这是我的假设),要么这不是正确的方法。我也尝试了 .remove 我留在下面的代码中。它返回此错误消息

Traceback (most recent call last):
File "C:/Users/Daniel/Desktop/PythonPR/Flubbo'sLatestBuild.py", line 104, in <module>
player = Player("Jeff", 100, [], 'introd', command)
File "C:/Users/Daniel/Desktop/PythonPR/Flubbo'sLatestBuild.py", line 97, in __init__
addToInventory(self.room.roominv)
File "C:/Users/Daniel/Desktop/PythonPR/Flubbo'sLatestBuild.py", line 82, in addToInventory
Room.roominv.remove(item)
AttributeError: type object 'Room' has no attribute 'roominv'

这与使用 del 或 delattr 时出现的错误相同。任何提示、建议或伪代码将不胜感激,如果我只是以完全错误的方式解决这个问题,请告诉我哈哈。

要开始游戏,您必须键入 n、w、e 或 s 3 次(Bug),然后您可以使用这些键四处走动或使用“Take Items”在房间中拾取元素。

world = {}
command = input('>>> ')


class Items:
def __init__(self, name, info, weight, position):
self.name = name
self.position = position
self.info = info
self.weight = weight


class Weapon(Items):
def __init__(self, name, info, damage, speed, weight, position):
super().__init__(name, info, weight, position)
self.damage = damage
self.speed = speed


sword = Weapon("Sword", "A sharp looking sword. Good for fighting goblins!", 7, 5, 5, 0)
knife = Weapon("Knife", "A wicked looking knife, seems sharp!", 5, 7, 3, 5)
stick = Weapon("Stick", "You could probably hit someone with this stick if you needed to", 2, 3, 3, 2)
shackkey = Items("Shack Key", "A key! I wonder what it opens.", .01, 3)
cottagekey = Items("Cottage Key", "An ornate key with an engraving of a small cottage on one side", .01, 5)
Moonstone = Items("Moonstone", "A smooth white stone that seems to radiate soft white light", .05, 6)
flower = Items("Flower", "A beautiful wildflower", .001, 1)


class Room:

def __init__(self, name, description, exits, actions, roominv): # Runs every time a new room is created
self.name = name
self.description = description
self.exits = exits
self.actions = actions
self.roominv = roominv


world['introd'] = Room('introd', "You are in a forest, you can hear wildlife all around you. There seems to be a clearing in the distance.", {'n' or 'north' or 'go north': "clearing"}, {"Search the ground", "Go North"}, {'sword': sword})


world['clearing'] = Room('clearing', "You are in a clearing surrounded by forest. Sunlight is streaming in, illuminating a bright white flower in the center of the clearing. \
To the South is the way you entered the forest. A well worn path goes to the East. In the distance a harp can be heard.", {'s' or 'south' or 'go south': "introd", 'e' or 'east' or 'go east': "forest path"}, {"Take flower", "Go south", "Go East"}, {'flower': flower})



class Player:

def __init__(self, name, health, bag, room_name, move):
self.name = name
self.health = health
self.bag = bag
self.room = world[room_name]
self.move = move

def travel(self, direction):
if direction not in self.room.exits.keys():
print("You can't go that way!")
else:
new_room_name = self.room.exits[direction]
print("moving to", new_room_name)
self.room = world[new_room_name]
print(self.room.description)
print(self.room.actions)

def addToInventory(item):
self.bag.append(item)
Room.roominv.remove(item)

command = input('>>> ')
while command != "":
command = input('>>> ')
if command in {'n', 'e', 's', 'w', 'north', 'south', 'east', 'west', 'go north', 'go south', 'go east', 'go west'}:
travel(self, command)
elif command == 'look':
print(self.room.description)
print('Exits', self.room.exits.keys())
elif command == '':
print('You have to say what it is you want to do! Unfortunately due to a bug you must now restart the game. We are working on fixing this as soon as possible. Sorry!')
elif command == 'search':
print(self.room.roominv)
elif command == 'Take Items':
addToInventory(self.room.roominv)
elif command == 'Inventory':
print(self.bag)
else:
print('Invalid command')


player = Player("Jeff", 100, [], 'introd', command)

也可以随意批评我的代码!我正在努力学习,来自更有经验的人的任何建议/评论都会很棒。

最佳答案

虽然不是代码审查,但我也会添加一些提示来修复您的其他错误,但首先是手头的问题:

Room.roominv.remove(item)

Room 是一个类,而不是一个对象,所以你不能请求它的 roominv相反,你需要 self.room.roominv,但是这不会起作用,因为 .remove 不是 dict 的成员,我建议以下更改:

你的'Take Items'命令应该是:

elif command.split()[0] == 'Take':
for key in list(self.room.roominv.keys()):
if self.room.roominv[key].name == command.split()[1]:
addToInventory(key)

这将允许用户只拿走可用的特定元素,并允许用户指定他们想要拿起的元素。

然后您的 addToInventory 函数可以是:

def addToInventory(self,key):
self.bag.append(self.room.roominv[key])
del self.room.roominv[key]

如果房间中有多个项目,这将只删除给定的项目。

del 关键字从字典中删除给定的键/项对。


关于你的其他问题,你的错误在这里:

elif command == '':
print('You have to say what it is you want to do! Unfortunately due to a bug you must now restart the game. We are working on fixing this as soon as possible. Sorry!')

可以简单地通过更改command是什么来修复:

elif command == '':
print('You have to say what it is you want to do!')
command = '#'

为了解决您需要多次输入指令的问题,我建议您执行以下操作:

从代码顶部删除 command = input('>>> ')

while command != "" 循环上面的 command = input('>>> ') 替换为 command = ' '

并更新您的 Player 以不进行移动(这意味着编辑 __init__ 函数以及 的初始创建>player 在代码底部:player = Player("Jeff", 100, [], 'introd'))


最后,吹毛求疵,为什么不把你的 while 循环放在一个函数中呢?所有这一切都会导致:

class Player:
def __init__(self, name, health, bag, room_name):
self.name = name
self.health = health
self.bag = bag
self.room = world[room_name]

def travel(self, direction):
if direction not in self.room.exits.keys():
print("You can't go that way!")
else:
new_room_name = self.room.exits[direction]
print("moving to", new_room_name)
self.room = world[new_room_name]
print(self.room.description)
print(self.room.actions)

def addToInventory(self, key):
self.bag.append(self.room.roominv[key])
del self.room.roominv[key]

def play(self):
command = " "
while command != "":
command = input('>>> ')
if command in {'n', 'e', 's', 'w', 'north', 'south', 'east', 'west', 'go north', 'go south', 'go east', 'go west'}:
self.travel(command)
elif command == 'look':
print(self.room.description)
print('Exits', self.room.exits.keys())
elif command == '':
print('You have to say what it is you want to do!')
command = '#'
elif command == 'search':
print(self.room.roominv)
elif command.split()[0] == 'Take':
itemTaken = False
for key in list(self.room.roominv.keys()):
if self.room.roominv[key].name == command.split()[1]:
self.addToInventory(key)
itemTaken = True
if not itemTaken:
print("I can't find that")
elif command == 'Inventory':
print(self.bag)
else:
print('Invalid command')

player = Player("Jeff", 100, [], 'introd')
player.play()

这些更改会导致需要进行一些其他更改,例如在查看您的库存时,打印出每个项目的 .name,而不是对象本身。

还可以通过完整的代码审查进行更多更改,但是为此您应该在 Code Review 上发布您的代码,请注意,Code Review 应该只用于完整、有效的代码,因此在发布之前确保其中没有任何破坏游戏的错误。

运行示例:

>>> n
moving to clearing
You are in a clearing surrounded by forest. Sunlight is streaming in, illuminating a bright white flower in the center of the clearing. To the South is the way you entered the forest. A well worn path goes to the East. In the distance a harp can be heard.
{'Go East', 'Take flower', 'Go south'}
>>> s
moving to introd
You are in a forest, you can hear wildlife all around you. There seems to be a clearing in the distance.
{'Search the ground', 'Go North'}
>>> n
moving to clearing
You are in a clearing surrounded by forest. Sunlight is streaming in, illuminating a bright white flower in the center of the clearing. To the South is the way you entered the forest. A well worn path goes to the East. In the distance a harp can be heard.
{'Go East', 'Take flower', 'Go south'}
>>> s
moving to introd
You are in a forest, you can hear wildlife all around you. There seems to be a clearing in the distance.
{'Search the ground', 'Go North'}
>>> search
{'sword': <__main__.Weapon object at 0x00000000040B9470>}
>>> Take sword
I can't find that
>>> Take Sword
>>> Inventory
[<__main__.Weapon object at 0x00000000040B9470>]
>>> search
{}
>>>

关于 python 3 : How to remove items from an inventory in a text-based game?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45571588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com