Question
I was wondering if someone could help me to complete this Dungeon fight game in Python. I need codes for this section: elif(command == attack):
I was wondering if someone could help me to complete this Dungeon fight game in Python.
I need codes for this section:
"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"
The questions could be found somewhere at the middle of the code. I was not asking to fix an indentation error.
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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started