Apr-24-2019, 06:57 AM
Hello. I'm new to Python and I was following along with a tutorial to make an rpg game. i was doing okay until it got to the part where if the player uses magic its supposed to reduce the amount of magic (mp)
When I run and use the mp it does not decrease it stays constant. I am getting no errors, the output is not doing what I need it to. My suspicion is the function I call in line 38 of the main file as it's not reducing as intrnded
This is my code. It has class file and main files:
When I run and use the mp it does not decrease it stays constant. I am getting no errors, the output is not doing what I need it to. My suspicion is the function I call in line 38 of the main file as it's not reducing as intrnded
This is my code. It has class file and main files:
import random class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' class Person: def __init__(self,hp, mp, atk, df, magic): self.msx_hp = hp self.hp = hp self.max_mp = mp self.mp = mp self.atkl = atk - 10 self.atkh = atk + 10 self.df = df self.magic = magic # dictionary of items to be passed self.actions = ["Attack", "Magic"] def generateDamage(self): return random.randrange(self.atkl, self.atkh) def generateSpellDamage(self, i): mgl = self.magic[i]["dmg"] - 5 mgh = self.magic[i]["dmg"] + 5 return random.randrange(mgl, mgh) def takeDamege(self,dmg): self.hp -= dmg if self.hp < 0: self.hp = 0 return self.hp def getHP(self): return self.hp def getMaxHP(self): return self.msx_hp def getMP(self): return self.max_mp def getMaxMP(self): return self.max_mp def reduceMP(self, cost): self.mp -= cost return self.mp def getSpellName(self, i): return self.magic[i]["name"] def getSpellMPCost(self, i): return self.magic[i]["cost"] def chooseAction(self): i = 1 print("Actions") for item in self.actions: print(str(i) + ":", item) i += 1 def chooseMagic(self): i = 1 print("Magic") for spell in self.magic: print(str(i) + ":", spell["name"], "(cost:", str(spell["cost"]) + ")") i += 1Main file:
from classes.game import Person, bcolors magic = [{"name": "Fire", "cost": 10, "dmg": 100}, {"name": "Thunder", "cost": 10, "dmg": 150}, {"name": "Blizzard", "cost": 10, "dmg": 120}] player = Person(460, 65, 60, 34, magic) enemy = Person(1200, 65, 45, 25, magic) running = True i = 0 print(bcolors.FAIL + bcolors.BOLD + "AN ENEMY ATTACKS!" + bcolors.ENDC) while running: print("================================") player.chooseAction() choice = input("Choose action: ") index = int(choice) - 1 if index == 0: dmg = player.generateDamage() enemy.takeDamege(dmg) print("You attacked for:", dmg, "damage.") elif index == 1: player.chooseMagic() magicChoice = int(input("Choose magic: ")) - 1 magicDamage = player.generateSpellDamage(magicChoice) spell = player.getSpellName(magicChoice) cost = player.getSpellMPCost(magicChoice) currentMP = player.getMP() if cost > currentMP: print(bcolors.FAIL + "\nNot enough MP\n" + bcolors.ENDC) continue player.reduceMP(cost) # issue wih reducing mp enemy.takeDamege(magicDamage) print(bcolors.OKBLUE + "\n" + spell + " deals", str(magicDamage), "damage" + bcolors.ENDC) enemyChoice = 1 enemyDamge = enemy.generateDamage() player.takeDamege(enemyDamge) print("Enemy attacks for: ", enemyDamge, "damage.") print("--------------------------------------------") print("Enemy HP:" + bcolors.FAIL + str(enemy.getHP()) + "/" + str(enemy.getMaxHP()) + bcolors.ENDC + "\n") print("Player HP:" + bcolors.OKGREEN + str(player.getHP()) + "/" + str(player.getMaxHP()) + bcolors.ENDC) print("Player MP:" + bcolors.OKBLUE + str(player.getMP()) + "/" + str(player.getMaxMP()) + bcolors.ENDC + "\n") if enemy.getHP() == 0: print(bcolors.OKGREEN + "You win!" + bcolors.ENDC) running = False elif player.getHP() == 0: print(bcolors.FAIL + "Enemy has defeated you!" + bcolors.ENDC) running = False