I was wondering if someone could help me to complete this Dungeon fight game in
ID: 3725932 • Letter: I
Question
I was wondering if someone could help me to complete this Dungeon fight game in Python.
The section that has comment "#" on it need to be fixed.
I will appreciate any help!
Hint:
#Code
import random
class Monster:
def __init__(self, name):
self.name = name
self.hp = random.randint(10,30)
self.currRoom = None
def generateDamage(self):
return random.randint(1,10)
def takeDamage(self, amount):
self.hp = self.hp - amount
def isDead(self):
return self.hp <= 0
class Person:
def __init__(self, name):
self.name = name
self.hp = random.randint(10,30)
self.currRoom = None
def generateDamage(self):
return random.randint(1,10)
def takeDamage(self, amount):
self.hp = self.hp - amount
def isDead(self):
return self.hp <= 0
def display(self):
print("Hi, I am " + self.name)
print("Stats:")
print("HP:" + str(self.hp))
def processCommand(self, command):
if(command == "look"):
self.currRoom.display()
elif(command == "attack"):
#ask which monster to attack
#read in the monster name
#create a fight between the player and
#the monster similar to what we did in class
else:
#check to see if the command is one of the exits
#if it is, take the exit.
#This means, remove the player from the current
#room, add them to the destination of the exit
#and make sure to update the players currRoom
print("Command Not Found")
def start(self):
self.currRoom.display()
while(True):
print("command:> ", end=' ')
command = input()
if(command == "quit"):
print("Good Bye!")
break
else:
self.processCommand(command)
class Exit:
def __init__(self, destination):
self.destination = destination
class Room:
def __init__(self, name, description):
self.name = name
self.description = description
self.listOfPersons = []
self.listOfExits = {}
self.listOfMonsters = []
def display(self):
print(self.name)
print("Description:")
print(self.description)
print("Other Players:")
for p in self.listOfPersons:
print(p.name)
print("Monsters:")
for m in self.listOfMonsters:
print(m.name)
print("Possible Exits:")
for exitName in self.listOfExits:
print(exitName)
def addMonster(self, monster):
self.listOfMonsters.append(monster)
def addPerson(self, person):
self.listOfPersons.append(person)
person.currRoom = self
def addExit(self, name, exit):
self.listOfExits[name] = exit
class Dungeon:
def __init__(self, name):
self.name = name
r1 = Room("Room 1", "A Room")
r2 = Room("Room 2", "A Room")
r3 = Room("Room 3", "A Room")
r1.addExit("east", Exit(r2))
r1.addExit("south", Exit(r3))
r2.addExit("west", Exit(r1))
r2.addExit("southwest", Exit(r3))
r3.addExit("north", Exit(r1))
m1 = Monster("Bayo")
m2 = Monster("Mole")
r1.addMonster(m1)
r1.addMonster(m2)
self.startingRoom = r1
def enterDungeon(self, person):
self.startingRoom.addPerson(person)
person.start()
p1 = Person("Bayo")
theDungeon = Dungeon("Scary Dungeon")
theDungeon.enterDungeon(p1)
Explanation / Answer
You have messed up some of the indentation blocks, i have corrected the indentation. the code is as follows:
Code:
import random
class Monster:
def __init__(self, name):
self.name = name
self.hp = random.randint(10,30)
self.currRoom = None
def generateDamage(self):
return random.randint(1,10)
def takeDamage(self, amount):
self.hp = self.hp - amount
def isDead(self):
return self.hp <= 0
class Person:
def __init__(self, name):
self.name = name
self.hp = random.randint(10,30)
self.currRoom = None
def generateDamage(self):
return random.randint(1,10)
def takeDamage(self, amount):
self.hp = self.hp - amount
def isDead(self):
return self.hp <= 0
def display(self):
print("Hi, I am " + self.name)
print("Stats:")
print("HP:" + str(self.hp))
def processCommand(self, command):
if(command == "look"):
self.currRoom.display()
elif(command == "attack"):
#ask which monster to attack
#read in the monster name
#create a fight between the player and
#the monster similar to what we did in class
pass
else :
#check to see if the command is one of the exits
#if it is, take the exit.
#This means, remove the player from the current
#room, add them to the destination of the exit
#and make sure to update the players currRoom
print("Command Not Found")
def start(self):
self.currRoom.display()
while(True):
print("command:> ", end=' ')
command = input()
if(command == "quit"):
print("Good Bye!")
break
else:
self.processCommand(command)
class Exit:
def __init__(self, destination):
self.destination = destination
class Room:
def __init__(self, name, description):
self.name = name
self.description = description
self.listOfPersons = []
self.listOfExits = {}
self.listOfMonsters = []
def display(self):
print(self.name)
print("Description:")
print(self.description)
print("Other Players:")
for p in self.listOfPersons:
print(p.name)
print("Monsters:")
for m in self.listOfMonsters:
print(m.name)
print("Possible Exits:")
for exitName in self.listOfExits:
print(exitName)
def addMonster(self, monster):
self.listOfMonsters.append(monster)
def addPerson(self, person):
self.listOfPersons.append(person)
person.currRoom = self
def addExit(self, name, exit):
self.listOfExits[name] = exit
class Dungeon:
def __init__(self, name):
self.name = name
r1 = Room("Room 1", "A Room")
r2 = Room("Room 2", "A Room")
r3 = Room("Room 3", "A Room")
r1.addExit("east", Exit(r2))
r1.addExit("south", Exit(r3))
r2.addExit("west", Exit(r1))
r2.addExit("southwest", Exit(r3))
r3.addExit("north", Exit(r1))
m1 = Monster("Bayo")
m2 = Monster("Mole")
r1.addMonster(m1)
r1.addMonster(m2)
self.startingRoom = r1
def enterDungeon(self, person):
self.startingRoom.addPerson(person)
person.start()
p1 = Person("Bayo")
theDungeon = Dungeon("Scary Dungeon")
theDungeon.enterDungeon(p1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.