我正在尝试使用 MUD-PI制作 MUD ,但我一直坚持让房间包含怪物并且让 kill 命令知道要计算攻击的怪物
这是我的 kill 命令代码
`elif command == "kill":
if players[id]["room"]["enemy"] == 'True':
if players[id]["room"]["monsterName"].hp >= 0:
mud.send_message(id,"You attack %s for %d damage" % (players[id]["room"]["monsterName"], players[id]["ATK"]))
players[id]["room"]["monsterName"].hp -= players[id]["ATK"]
else:
players[id]["room"]["monsterName"].death()
else:
mud.send_message(id,"You dont see a monster")]
和房间的代码
#import monsters
from Monsters import *
# structure defining the rooms in the game. Try adding more rooms to the game!
rooms = {
"Tavern": {
"description": "You're in a cozy tavern warmed by an open fire.",
"exits": { "outside": "Outside" },
},
"Outside": {
"description": "You're standing outside a tavern. there is a troll.",
"exits": { "inside": "Tavern" },
"enemy": 'True',
"monsterName": troll,
}
}
最后是怪物代码
#monsters
import sys,random,os,time
#Troll
class Troll():
def __init__(self):
self.name = "Troll"
self.ATK = 2
self.hp = 10
self.max_hp = 10
def death(self):
mud.send_message(id,"you killed the troll")
self.hp = self.max_hp
troll = Troll()
当我尝试当前代码时,我收到此错误:
if players[id]["room"]["enemy"] == 'True':
TypeError: string indices must be integers, not str
我想知道是什么导致了这个错误,如果可能的话,我怎样才能让它以我想要的方式工作。
编辑:我得到了这个工作,但是当我在游戏中使用命令时,它会像这样(你攻击)造成 3 点伤害我想说你攻击了房间里怪物的名字。
这是我编辑的代码
elif command == "kill":
rm = rooms[players[id]["room"]]
if rm["enemy"] == 'yes':
if rm["monsterName"].hp >= 0:
mud.send_message(id,"You attack %s for %d damage" % (rm["monsterName"], players[id]["ATK"]))
rm["monsterName"].hp -= players[id]["ATK"]
else:
rm["monsterName"].death()
else:
mud.send_message(id,"You dont see a monster")
在这种情况下,我还想将命令从 kill 更改为 kill [monsterName] 巨魔,我尝试这样做
elif command == "kill":
mn = params.lower()
rm = rooms[players[id]["room"]]
if rm["enemy"] == 'yes':
if mn in rm["monsterName"]:
monster = rm["monsterName"]
if monster.hp >= 0:
mud.send_message(id,"You attack %s for %d damage" % (rm["monsterName"], players[id]["ATK"]))
monster.hp -= players[id]["ATK"]
else:
monster.death()
else:
mud.send_message(id, "you dont see a %s" % mn)
else:
mud.send_message(id, "you dont see an enemy")
但是当我尝试使用它时,我得到了这个错误
if mn in rm["monsterName"]:
TypeError: argument of type 'instance' is not iterable