Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Python RPG game
#1
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:
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 += 1
Main 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
Reply
#2
Hi, welcome to the forum, your player has a spell hack that causes the players mp to always be the players max mp
getMP(self) is returning self.max_mp
Note i also noticed self.msx_hp max spelt wrong but it is spelt wrong in all places used so doesn't cause any problems but may do for you later.
class Person:
    def __init__(self,hp, mp, atk, df, magic):
        self.msx_hp = hp
        self.hp = hp
        self.max_mp = mp
        self.mp = mp
        ....
        ....

    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
 
    ....
    ....
   
Reply
#3
Thank you, happy to be here.

Oh,that's the issue. Thnak you and I'll fix the spelling error as well
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020