On this page

Guided Exercise: Build a Mini RPG Class System

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Project Overview​

This guided exercise applies the core principles of object‑oriented programming by having you design a modular RPG character system.

You will build:

This mirrors real‑world OOP design: a shared interface with specialized implementations.


Step 1 — Define the Base Character Class​

The base class establishes the shared structure and behavior for all characters.


class Character:

    def __init__(self, name, health):

        self._name = name

        self._health = health



    @property

    def name(self):

        return self._name



    @property

    def health(self):

        return self._health



    def take_damage(self, amount):

        self._health = max(0, self._health - amount)

        return self._health



    def attack(self, target):

        raise NotImplementedError("Subclasses must implement attack().")

Key ideas:


Step 2 — Create Subclasses with Specialized Behavior​

Warrior Class​

A melee fighter with strong physical attacks.


class Warrior(Character):

    def attack(self, target):

        damage = 10

        target.take_damage(damage)

        return f"{self.name} strikes {target.name} for {damage} damage!"

Mage Class​

A spellcaster with powerful but costly magic.


class Mage(Character):

    def __init__(self, name, health, mana):

        super().__init__(name, health)

        self._mana = mana



    def attack(self, target):

        if self._mana >= 5:

            damage = 15

            self._mana -= 5

            target.take_damage(damage)

            return f"{self.name} casts a spell on {target.name} for {damage} damage!"

        return f"{self.name} is out of mana."

These subclasses demonstrate:


Step 3 — Add Another Specialized Class (Optional Extension)​

Archer Class​

A ranged attacker with variable damage.


import random



class Archer(Character):

    def attack(self, target):

        damage = random.randint(5, 12)

        target.take_damage(damage)

        return f"{self.name} shoots an arrow at {target.name} for {damage} damage!"

This class highlights:


Step 4 — Simulate a Simple Battle​

Create characters and let them interact.


w = Warrior("Thorin", 50)

m = Mage("Elora", 40, mana=20)

a = Archer("Rowan", 45)



print(w.attack(m))

print(m.attack(w))

print(a.attack(w))

Because all subclasses implement attack(), they can be used interchangeably:


party = [w, m, a]



for member in party:

    print(member.attack(w))

This demonstrates polymorphism in action: each object responds according to its own class.


Step 5 — Add Encapsulation and Validation (Optional Enhancements)​

Healing Method​


def heal(self, amount):

    if amount > 0:

        self._health += amount

    return self._health

Read‑Only Attributes​

Use properties without setters to prevent external modification.

Controlled State Changes​

Ensure health never becomes negative or exceeds a maximum.

These refinements reinforce encapsulation and safe state management.


Step 6 — Organize the System into Modules​

A clean project structure:


rpg/

    __init__.py

    character.py

    warrior.py

    mage.py

    archer.py

    battle_demo.py

Each class lives in its own module, improving clarity and scalability.

Example import:


from warrior import Warrior

from mage import Mage

from archer import Archer

Summary​

In this guided exercise, you built a complete mini RPG class system using:

This project demonstrates how OOP enables clean, extensible, and expressive program design—exactly the skills required for building real‑world software systems.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Magic Methods

Next

File Handling & Persistence