Я успешно смог «заклинать» (украсть) метод из одного класса (darkMage), чтобы передать классу игрока (Player).
Однако, «украденный» метод все еще кажетсяпринадлежат к классу darkMage - другими словами, когда Игрок разыгрывает украденное заклинание, он все равно читает (во всех отношениях), как темный Маг, разыгрывающий заклинание.
Вы можете помочь?Я сделал это:
- Добавил украденное заклинание darkMage () в книгу заклинаний Игрока (список)
- Успешно соединил команду "cast" с предметами в книге заклинаний
Я хочу: - сделать так, чтобы Player () читал заклинание, а не darkMage (когда вызывается украденный метод, он запускается как darkMage, а не как игрок)
class Player(livingThing):
def __init__(self,name="The Stranger", HP=10, MP=5, strength=1, intellect=1, spirit=1, luck=5, gil=6):
self.name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.luck = luck
self.spellbook = []
def act(self, enemy):
actions = {
"a" : self.attack,
"heal" : self.heal,
"flee" : self.flee,
"cast" : self.cast,
"siphon" : self.spellsiphon
}
#Takes input from the player
self.safe = False
while ((self.HP > 0) and (enemy.HP > 0)) and (self.safe != True):
decision = input("What would you like to do? ")
#Sets the user's input as lower-case and checks for it within the dictionary
if decision.lower() in actions:
actions[decision.lower()](enemy)
if self.safe != True:
enemy.agreact(self)
self.printHP(enemy)
else:
print("That didn't workkkkkk! Try again.")
# Prints both player and enemy HP
def printHP(self, enemy):
print("{0}'s' HP: {1} \n{2}'s HP: {3}".format(self.name, self.HP, enemy.name, enemy.HP))
# Allows the player to attack an enemy (currently functional)
def attack(self, enemy):
enemy.HP -= self.strength
print("You strike {0} for {1} damage!".format(enemy.name, self.strength))
#player.printHP(enemy)
# Allows the player to heal a certain amount of health based on its "spirit" stat (currently functional)
def heal(self, enemy):
healed = randint(0, self.spirit)
self.HP += healed
print("You've healed for {0}!".format(healed))
#player.printHP(enemy)
#Allows the player to attempt to run away
def flee(self, enemy):
randluck = randint(0, self.luck)
if randluck > 3:
print("You successfully escaped!")
self.safe = True
else:
print("You weren't able to escape!")
def cast(self, enemy):
if len(self.spellbook) != 0:
spellchoice = randint(0, len(self.spellbook)-1)
self.spellbook[spellchoice](self)
else:
print("You don't have any spells to cast!")
### SPELLSIPHON IS CURRENTLY BROKEN; IT -DOES- SIPHON THE SPELL BUT ONLY ALLOWS THE DARK MAGE TO CONTINUE CASTING ###
def spellsiphon(self, enemy):
if len(enemy.spellbook) != 0:
randspell = randint(0, len(enemy.spellbook)-1)
stolenspell = darkMage().spellbook[randspell]
#(type(enemy).__name__).__init__(self, stolenspell)
self.spellbook.append(stolenspell)
print("You've successfully stolen {0} from {1}!".format("stolenspell", enemy.name))
else:
print("You can't steal a spell from {0}!".format(enemy.name))
# Anything that can act with/against the player
class Actor(livingThing):
def __init__(self, name="Unknown Entity", HP=10, MP=2, strength=1, intellect=1, spirit=3, gil=3):
self. name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.abilities = [self.strike, self.heal]
def printabilities():
print(self.abilities)
# Chooses how your opponent will respond to your attack
def agreact(self, player):
choice = randint(0, len(self.abilities)-1)
self.abilities[choice](player)
# A basic "hit back" reaction from your opponent--everyone can do this
def strike(self, player):
player.HP -= self.strength
print("{0} hit {1} for {2}!".format(self.name, player.name, self.strength))
def heal(self, enemy):
healed = randint(0, self.spirit)
self.HP += healed
print("{0} healed for {1}!".format(self.name, healed))
### CURRENT VERSION SUPPORTS THE ADDITION OF NEW ABILITIES IN CHILD CLASSES VIA THE "super().__init__()" method! ###
class Enemy(Actor):
def __init__(self):
super().__init__()
self.abilities.append(self.cast)
self.name = "Unknown Enemy"
self.HP = 600
self.spellbook = []
def cast(self, opponent):
if len(self.spellbook) != 0:
spellchoice = randint(0, len(self.spellbook)-1)
self.spellbook[spellchoice](opponent)
class darkMage(Enemy):
def __init__(self):
super().__init__()
self.player = Player()
self.name = "Dark Mage"
self.spellbook.extend((self.fireball, self.icenova))
def fireball(cls, opponent):
choice = randint(cls.intellect*1, cls.intellect*2)
spellname = "Fireball"
opponent.HP -= choice
print("{0} casted fireball for {1} damage!".format(cls.name, choice))
def icenova(cls, opponent):
opponent.HP -= cls.intellect
choice = randint(0,1)
name = "Ice Nova"
if choice == 1:
frozen = True
else:
frozen = False
print("{0} casted ice nova for {1} damage!".format(cls.name, cls.intellect))
if frozen == True:
print("{0} has been frozen solid!".format(opponent.name))