我试图使用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")
我想在这种情况下将trold命令从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
答案 0 :(得分:0)
您已将enemy
密钥添加到rooms
数据结构(即字典),但是当您访问players[id]["room"]["enemy"]
时, 访问rooms
数据结构 - 您正在访问players
,而在players
中,room
值是一个字符串 - 房间的名称。所以你不能要求它的“敌人”索引,因为那将毫无意义,这就是错误string indices must be integers, not str
的来源。
要修复当前设计的代码中的错误,您必须找到播放器所在的房间,然后访问房间的“敌人”值,并以类似的方式找到值对于monsterName
。