The Confusing World Of Object-Oriented Programming, Explained As An RPG

The Confusing World Of Object-Oriented Programming, Explained As An RPG

Object-oriented programming (or OOP) is an abstract concept, often hard to grasp when you’re new to programming. The “Invent with Python” blog offers an awesome analogy that makes OOP more understandable if you’ve ever played a RPG-style video game like World of Warcraft or Dungeons & Dragons.

Let’s say you’re creating your own game. You’ll need to represent all the characters (heroes and monsters), their stats (health and magic points), inventory items, and so on — and also account for health damage your characters take, inventory going up or down, and other status changes. Defining each thing with variables (e.g., monster1Name = 'Goblin', monster2Name = 'Dragon') and using lists or a list of dictionaries gets quickly out of hand and makes the code more complex (and bug-prone) than it needs to be. E.g.: 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}}]

That’s where OOP and its classes and methods come in:

Classes are the blueprints for a new data type in your program. Object-oriented programming provides a new approach for modelling the armour, monsters, etc. much better than a hodge-podge of lists and dictionaries, even though OOP concepts it take some getting used to.

In fact, since the hero will have all the same features of monsters (health, inventory, etc.) we can just have a generic LivingThing class that the hero and monsters share.

You define the LivingThing class once and then can quickly and easily create new monsters from it. You could update the LivingThing class to, say, now track your RPG character’s hunger just by adding one line of code — instead of having to update every object individually. As explained in a different laundry analogy, objects encapsulate complexity.

Check out Al Sweigart’s post below for the full RPG-meets-OOP example. Whether you want to create your own RPG game or another kind of program, this explanation is a lot more interesting to read than most OOP tutorials.

Why is Object-Oriented Programming Useful? (With a Role Playing Game Example) [The “Invent with Python” Blog]


The Cheapest NBN 50 Plans

Here are the cheapest plans available for Australia’s most popular NBN speed tier.

At Lifehacker, we independently select and write about stuff we love and think you'll like too. We have affiliate and advertising partnerships, which means we may collect a share of sales or other compensation from the links on this page. BTW – prices are accurate and items in stock at the time of posting.

Comments


2 responses to “The Confusing World Of Object-Oriented Programming, Explained As An RPG”