Python 文本地下城项目



我正在为学校制作一个文本地牢游戏,但我被困在了一步。我的角色需要能够捡起东西,并且最多有 3 件物品的库存。任何建议都会有所帮助。非常感谢!这是我现在的代码:

print("The evil monster Damayo has destroyed your village and stolen all your food. Him and his monster guards are at their hideout. Find them, destroy them, and retrieve your food.")
index = 1
level1 = ["1", "2", "3"]
level2 = ["4", "5", "6"]
level3 = ["7", "8", "9"]
monsters = ["monster", "monster guard", "monster BOSS"]
while True:
a = input("Type in what you want to do: " )
helplist = ["Type in left to move left, Type in right to move right, Type in     help for a list of all commands, Type in grab to grab an item"]
if a == "help":
    print(helplist)

if index == 2 and a == "right":
    print("You cannot move any further")
if index == 0 and a == "left":
    print("You cannot move any further")
if a == "left" and index > 0:
    print("You moved left.")
    index = index - 1
if a == "right" and index < 2:
    print("You moved right")
    index = index + 1
if a == "up" and index == 2:
    print("You moved up.")
    index = index + 4
if a == "down" and index <2:
    print("You moved down.")
您可以

创建一个user字典,该字典具有类似的items_in_hand,该字典将0。每当您尝试拿起一个项目时,请检查items_in_hand是否小于 3,然后将其递增。

为了获得更好的代码质量,您也可以在该user字典中添加index变量。

所以你的代码会像

user = {'index':1 , 'items_in_hand' : 0}
if a =='grab':
    if  user['items_in_hand'] < 3:
        print("You grabbed the item")
        user['items_in_hand']+=1
    else :
        print("you dont have space")

我没有添加任何错误检查代码。

注意:最好的方法是使用objects,但一次只能使用一个步骤。

最新更新