- Courses
- 💻Programming Languages
- 🐍Python
- Object‑Oriented Programming (OOP)
- Guided Exercise: Build a Mini RPG Class System
On this page
Guided Exercise: Build a Mini RPG Class System
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:
- A base character class
- Specialized subclasses (e.g., Warrior, Mage)
- Encapsulated attributes
- Overridden methods
- Polymorphic combat behavior
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:
- Encapsulation via non‑public attributes
- Properties for controlled access
- A polymorphic
attack()method that subclasses must override
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:
- Inheritance
- Method overriding
- Additional attributes
- Polymorphic behavior
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:
- Randomized behavior
- Same interface (
attack) as other classes - Interchangeability through polymorphism
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:
- Inheritance to share structure
- Method overriding for specialized behavior
- Polymorphism to treat different classes uniformly
- Encapsulation to protect internal state
- Modular organization for maintainability
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!
Previous
Magic Methods
Next
File Handling & Persistence
-
Project Overview
-
Step 1 — Define the Base Character Class
-
Step 2 — Create Subclasses with Specialized Behavior
- Warrior Class
- Mage Class
-
Step 3 — Add Another Specialized Class (Optional Extension)
- Archer Class
-
Step 4 — Simulate a Simple Battle
-
Step 5 — Add Encapsulation and Validation (Optional Enhancements)
- Healing Method
- Read‑Only Attributes
- Controlled State Changes
-
Step 6 — Organize the System into Modules
-
Summary
