I'm trying to make a MUD using MUD-PI and I'm stuck at making rooms contain monsters and having the kill command know what monster to calculate the attack for
Here is my code for the kill command
`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")]
and the code for the rooms
#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,
}
}
and finally the monster code
#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()
When I try the current code I get this error:
if players[id]["room"]["enemy"] == 'True':
TypeError: string indices must be integers, not str
I would like to know what is causing this error and if possible, how I can get this to work the way I want.
EDIT: I got this to work but when i use the command in the game it comes out like this ( You attack ) for 3 damage I would like it to say you attacked the name of the monster in the room.
here is my edited code
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")
I would like to also change the command from kill to kill [monsterName] in this case troll, i tried to do so like this
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")
but when i try to use it i get this error
if mn in rm["monsterName"]:
TypeError: argument of type 'instance' is not iterable
You've added the enemy
key to the rooms
data structure (i.e., dictionary), but when you access players[id]["room"]["enemy"]
you are not accessing the rooms
data structure -- you are accessing players
, and in players
the room
value is a string -- the name of the room. So you cannot ask for its "enemy" index, since that would be meaningless, and this is the source of the error string indices must be integers, not str
.
To fix the error in the code as it is designed currently, you must find the room the player is in and then access the room's "enemy" value, and in a similar fashion find the value for the monsterName
.