0
Project link: https://github.com/ThomasCaio/RPG2
self.enemy.Damage returns : {'Physical': 45, 'fire': 25}
self.Defense returns : {'Physical': 50}
def receive_damage(self):
damage = self.enemy.damage
defense = self.defense
full_damage = 0
for dmg in damage:
full_damage += damage[dmg]
for dmg in damage:
if defense.get(dmg):
damage[dmg] -= defense[dmg]
if damage[dmg] < 0:
damage[dmg] = 0
final_damage = 0
for dmg in damage:
final_damage += damage[dmg]
print(f'{self.enemy} atacou {self} em {full_damage} de dano.')
print(f'{self} bloqueou {full_damage-final_damage} de dano e recebeu {final_damage}.')
self.health -= round(final_damage)
The player’s first damage is working perfectly. This function (receive_damage) has both player and monster, but when an attack occurs:
damage[dmg] -= defense[dmg]
this line of code reduces the damage of the monster. I am unable to solve this!
Because then, you shouldn’t just reduce the value of the damage variable instead of reducing the damage of self.enemy.Damage?
– Thomas Caio
Not if you’re gonna use a fixed damage. Example: Let’s say the enemy has a damage of 90, what you can do is create an auxiliary variable to undergo all the changes like reducing it by the player’s defense. This way, in a given round, the damage suffered can be 75 as it was calculated plus the player’s defense, but the monster’s damage remains 90. If you change the damage directly from the monster in the next rounds, the monster’s damage will be less as in the example I gave in the answer.
– JeanExtreme002
And isn’t this auxiliary variable what I did on Damage? I put her to take the monster’s damage and suffer the reduction but still alters the monster’s damage.
– Thomas Caio
If your "auxiliary variable" is on line 3 of your function, then you are wrong. From what I see, the variable "Damage" is a dictionary or some kind of object. You should remember that when you pass an object to a variable, you are not passing the object but a memory address. That is, the changes of the variable "Damage" are also happening in "self.enemy.Damage". For this not to happen just use the "copy" method of the dictionary ( if it is a dictionary ) to return an object with the identical elements but different from the original in memory.
– JeanExtreme002
Perfect! It worked right.
– Thomas Caio