HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Skill creation in a Python text-based RPG

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textskillpythonbasedrpgcreation

Problem

I am in the process of creating a text-based RPG for the purpose of learning more about OOP in Python. Right now things are going well, but I'm facing a problem that I don't exactly can find a good answer: Skill implementation. I also think that I'm going to face similar problem with the Items.

At this moment I have 3 classes(professions): Fighter, Rogue, and Mage. Each of these classes should have their own skills, but, skills have similar things(name, description, MP usage), so I created a BasicSkill class, and 3 subclasses: PassiveSkill, ActiveSkill and ChanelledSkill.

PassiveSkill are skills that doesn't require MP to use, are always active, and in most of cases, give the player buffs, like dual-wielding, more resistance, etc. ActiveSkill are skills that require MP to be used, and, in majority, cause damage or a instant buff. Finally, ChanelledSkill are skills that require more than one turn to be used, cost more MP than the others, and can be interrupted

But, when comes to the skill creation, I can only think in something like this:

def hack_and_slash(self):
        """This is a Fighter skill, it's melee and has no cool down."""
        from skills import ActiveSkill
        hack_and_slash = ActiveSkill("Hack n' Slash", "A powerful double attack that has a chance of causing bleeding.",
                                     8, 3)
        self.mp -= hack_and_slash.
        # more about the skill goes here, like what it does in terms of damaging, buffing, etc.


Which I think is not exactly what I'm aiming for, since I have to instantiate it every time I use, or something in the same lines.

I want to create the skills in a way that would require less instantiation, and also make more possible to cast. Also, all characters(player, npc, and enemies) can cast skills.

There's a bunch of files in this project, so I will post the ones I think are more relevant here, and, if you want, you can check out the project here.

This is my `Player

Solution

Aside from Pimgd's comments, I'd like to add that from an architecture point of view, you might want to reconsider your abilities. To make them easier to maintain, you should design all (or nearly all) abilities as just a dictionary of pure data. Figure out all the possible interactions and make key-value pairs for them.

For example:

{ 
  'name': 'Hack and Slash',
  'description': 'This is a...',
  'type': 'ActiveSkill',
  'affects': 'target',
  'damage': 30,
  'damage-type': ['melee', 'iron'],
  'cooldown': 0,
  'mp-cost': 0
}


Or

{
  'name': 'Dual Wield'
  'description': 'Use two weapons!'
  'type': 'PassiveSkill'
  'tags': ['dual-wield']
}


Or

{
  'name': 'Health Potion',
  'description': 'Regain Health',
  'type': 'Item',
  'tags': ['destroy-on-use'],
  'target': 'self',
  'damage': -30,   # negative damage is health gain
  'inventory-space': 1
}


By making your code generic (rather than one function per skill/ability) and making all skills and items just data it will make your project easier to debug and items and skills easier to modify or add to.

Code Snippets

{ 
  'name': 'Hack and Slash',
  'description': 'This is a...',
  'type': 'ActiveSkill',
  'affects': 'target',
  'damage': 30,
  'damage-type': ['melee', 'iron'],
  'cooldown': 0,
  'mp-cost': 0
}
{
  'name': 'Dual Wield'
  'description': 'Use two weapons!'
  'type': 'PassiveSkill'
  'tags': ['dual-wield']
}
{
  'name': 'Health Potion',
  'description': 'Regain Health',
  'type': 'Item',
  'tags': ['destroy-on-use'],
  'target': 'self',
  'damage': -30,   # negative damage is health gain
  'inventory-space': 1
}

Context

StackExchange Code Review Q#121083, answer score: 7

Revisions (0)

No revisions yet.