The Confusing World of Object-Oriented Programming in an RPG

Object Oriented Programming (or OOP) is an abstract concept that is often difficult to grasp when you are new to programming. The blog “Invent with Python” offers a great analogy that makes OOP more understandable if you’ve ever played RPG-style video games like World of Warcraft or Dungeons & Dragons.

Let’s say you are creating your own game. You will need to present all the characters (heroes and monsters), their statistics (health and magic points), inventory items, etc., and also take into account the damage done to the health of your characters, increase or decrease in inventory and other conditions. changes. Defining each thing with variables (e.g. monster1Name = 'Goblin' , monster2Name = 'Dragon' ) and using lists or a list of dictionaries quickly gets out of hand and makes the code more complex (and error-prone) than it should be. For example: monsters = [{'name': 'Goblin', 'health': 20, 'magic points': 0, 'inventory': {'gold': 12, 'dagger': 1}}, {'name': 'Dragon', 'health': 300, 'magic points': 200, 'inventory': {'gold': 890, 'magic amulet': 1}}]

This is where OOP comes in, with its classes and methods:

Classes are schemas of a new data type in your program. Object Oriented Programming provides a fresh approach to modeling armor, monsters, and the like, much better than a jumble of lists and dictionaries, although OOP concepts take some getting used to.

In fact, since the hero will have the same traits as the monsters (health, inventory, etc.), we can simply have a common LivingThing class common to the hero and the monsters.

You define the LivingThing class once, and then you can quickly and easily create new monsters from it. You can update the LivingThing class to, say, now track your RPG character’s hunger by simply adding one line of code instead of updating each object individually. As explained in another laundry analogy , objects carry complexity.

Check out Al Swigart’s post below for a complete example of RPG-OOP interaction. If you want to create your own RPG game or other program, this explanation is much more interesting to read than most OOP tutorials.

Why is Object Oriented Programming useful? (On the example of a role-playing game) | Invent With Python Blog

More…

Leave a Reply