Python Forum
An interesting Role-Play-Game battle script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An interesting Role-Play-Game battle script
#1
The following are the files:


https://pastebin.com/4EyFEGxi - game.py # Define player and his set of actions
https://pastebin.com/BSpBB7De - main.py # A RPG Battle script where players and enemies fight each other
https://pastebin.com/uUpd5rnm - inventory.py # Items which can be used only by the players and not the enemies
https://pastebin.com/NP31VP1X - magic.py # Spells which can be used by both the players and the enemies


When I run the main.py, I get the following error after attacking for a few times:

Error:
Traceback (most recent call last): File "E:/Battle/main.py", line 190, in <module> Players[target].take_dm(magic_dm) File "E:\Battle\Classes\Game.py", line 34, in take_dm self.hp -= dm TypeError: unsupported operand type(s) for -=: 'int' and 'NoneType'
And I also want to know if all the functions in the main program are working as they are supposed to be.
I'm a novice and want to understand programming better.
Thanks for the help in advance Smile
Reply
#2
magic_dm is instantiated on line 180 of main.py as "enemy.enemy_spell()", which is a Person method. In game.py, Person.enemy_spell() is:

def enemy_spell(self):
    m_c = random.randrange(0, len(self.magic))
    spell = self.magic[m_c]
    m_d = spell.gen_dm()

    pct = self.hp / self.maxhp * 100

    if self.mp < spell.cost or spell.type == "w" and pct > 50:
        self.enemy_spell()
    else:
        return spell, m_d
The core problem is the method does not necessarily return anything. Adding a return to the positive outcome of the final conditional statement should correct this. Also, the number of returned values should always be the same. This ensures all variables will be predictably instantiated; so, I'll add the m_d variable to this return as well to match the other one.

def enemy_spell(self):
    m_c = random.randrange(0, len(self.magic))
    spell = self.magic[m_c]
    m_d = spell.gen_dm()

    pct = self.hp / self.maxhp * 100

    if self.mp < spell.cost or spell.type == "w" and pct > 50:
        return self.enemy_spell(), m_d
    else:
        return spell, m_d
Another potential problem is the recursion. Hypothetically, this method could keep calling itself infinitely. Imagine the enemy only has "h" spells, 25% HP, and insufficient MP for any spell. The conditional would fail every time until there's a stack overflow.
Reply
#3
AHH !! So I changed the code a bit from line 179 of the main.py as :
https://pastebin.com/t51UPXLP

And also included return statement in line 164 of game.py.
No more errors are reflecting, but I do want to know if there are any and all the functions are working as they should be

@buran, I'll refer the help provided by you to use the proper tags. Thanks
Reply
#4
(Apr-01-2020, 06:22 PM)NitinL Wrote: And I also want to know if all the functions in the main program are working as they are supposed to be.

Good. Look up the phrase "unit testing". Python includes a module for writing tests that is imaginatively named unittest.
Reply
#5
Thanks friends, you guys are the best !!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Interesting Intro to python problem I can't solve. Honestworker 5 12,305 Mar-04-2021, 02:05 AM
Last Post: BashBedlam
Question Play again, in a guessing game banidjamali 4 11,675 Jan-22-2021, 06:23 AM
Last Post: banidjamali

Forum Jump:

User Panel Messages

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