Press "Enter" to skip to content

Lesson 01.1 – Defining classes and objects for the game

Lesson Objectives

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:

  • Player
  • Location
  • Item
  • Quest
  • Monster
  • Weapon
  • Potion

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).

 

Summary

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 easily 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.

 

Next Lesson: Lesson 02.1 – Installing Visual Studio Community 2017

Previous lesson: Lesson 00.3 – The parts of Visual Studio

All lessons: Learn C# by Building a Simple RPG Index

6 Comments

  1. Hentus
    Hentus February 1, 2016

    Hi,

     

    I am actually thinking about making a none visual game for D&D so that friends can play together via the internet.

     

    Since getting together is not allways easy :(.

     

    Could you do a “multiplayer networking” part ?

     

    Regards

    Hentus

    • Scott Lilly
      Scott Lilly February 1, 2016

      Hello Hentus,

      I might do a multi-player networking version in the future, but it would be a very advanced set of lessons. Handling the communications, and ensuring all the players are synchronized, is a difficult task. When you get to the final lesson, with everything from the Engine project using events to communicate with the UI project, it would be easy to make a browser-based front-end. But it would still be single-player.

      • Ryslngrd
        Ryslngrd May 10, 2016

        Scott, good tutorial. I think overall, this is a pretty decent starting point. I believe there are some things that could be done to make it even better. It is apparent you felt you needed to write all the code yourself. However, what about adding a “Do it yourself” after each segment which would add a little more room for thought. This would provide ideas on how to do things differently, more efficiently or to add additional content on the fly. But moreover, it would require me to go in and immerse myself in the code by necessity. Not just typing verbatim or copying from your source safe.

        Also, the person above was asking for multiplayer functionality… At this point I see you have yet to add this code. I am very much interested in learning this as well. But, I don’t believe you need to write the code for EVERYTHING to be helpful. I believe that most people will benefit from this tutorial 10 fold if you give them the breadcrumbs and make them find the solutions.

        I have gone through most of this tutorial. I found that you jammed quite a bit more information in the lessons the later in the tutorial I got. I also started getting overwhelmed just a little bit since you were dropping large portions of code and getting my head around what you were trying to do and how it was working with in C#. Started getting lost in the code. That is purely my opinion and mine alone.

        It may have sounded like I was knocking the tutorial, but I really wasn’t. This is a fantastic launching point into programming in C#. And I really believe a lot of people are very likely to be interested in coding something like this to start of with. Makes it much more fun.

        thank you

        • Scott Lilly
          Scott Lilly May 11, 2016

          You’re welcome. Right now, my focus is on the new WPF version of the course, which will have improvements based on the feedback from this course (which was originally only was only going to be up to lesson 17 🙂 ). Once that’s done, I might do more advanced courses for the game (web-based version, more complex combat, armor, jewelry, skills, recipes, crafting, enchanting items, multi-player, etc.). I think my “to do” list of ideas is growing about five times faster than I have time to complete them. 🙁

  2. ChilliBeef
    ChilliBeef January 5, 2022

    Hi Scott,

    So far I am enjoying this tutorial. It’s one of the more simpler, easy-to-understand ones that I’ve found which makes it easier for me to grasp the beginnings of C#.

    Also, where you said, “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.”, it should be “easily fix”. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *