At the end of this lesson, you will know…
- The most important thing to know about object-oriented programming.
- How to discover the classes/objects your program will need.
- The difference between classes and objects.
What is object-oriented programming?
There are whole books devoted to answering this question.
However, the main aspect of object-oriented programming (OOP) is that the program tries to imitate the real world by thinking of things as the objects they represent in the real world.
So, if you’re writing a program for a bank, your classes/objects will be things such as customers, accounts, deposits, withdrawals, checks, loans, etc.
There are other important aspects of OOP, and we’ll talk about some of them as we write the game. However, if you keep this one thing in mind, you’ll be halfway there.
What are the classes/objects in our game?
An easy way to figure out what your objects are going to be is to write down what you want your program to do, and underline all the nouns. For our game, we want to do these things:
- The player goes to locations.
- The player may need to have certain items to enter a location.
- The location might have a quest available.
- To complete a quest, the player must collect certain items and turn them in.
- The player can collect items by going to a location and fighting monsters there.
- The player fights monsters with weapons.
- The player can use a healing potion while fighting.
- The player receives loot items after defeating a monster.
- After turning in the quest, the player receives reward items.
So, the nouns (classes/objects) in this game will be:
We’ll need a few others, but we can start with these.
What’s the difference between a class and an object?
A class is basically a blank form, or blueprint, for an object.
It defines the object, but it isn’t the object.
Think back to the days of playing Dungeons & Dragons. Back when you had character sheets on paper.
The blank character sheet has spaces for your character’s name, class, level, experience points, attributes, armor, weapons, inventory, skills, spells, etc. But, until you filled in those spaces, it was just an outline (a class).
Once you filled in your character’s information, you had a character (an object).
Thinking of physical things, a blueprint for a house is a class (it says what the house will be like). Once you have builders follow the blueprint, with wood, steel, concrete, etc., you’ll have a house (the object).
There’s an old programmer’s saying, “You aren’t writing a program for the computer to read. You’re writing a program for a programmer to read.”
It means that your program should be easy to understand, so a future programmer (who may be you) can easier fix or change things in your program.
One of the most difficult parts of programming is for the programmer to keep track of everything in their mind. With objects that mirror real-world ones, it’s easier to remember what’s happening – you can almost picture it.
Previous lesson: Lesson 00.3 – The parts of Visual Studio
All lessons: Learn C# by Building a Simple RPG Index