Press "Enter" to skip to content

Lesson 03.3: Creating the Player Class

Summary

  • A program represents activities in “the real world”.
    • Or, for us, the game world.
  • The program needs to know how to represent “things”.
    • To find the “things” in a program, look for the nouns in the requirements.
    • We will create “classes” for each of those things.
    • We will also create other classes. But, for now, we will focus on the “things”.
    • Another term for these classes is “model”, because they are a “model” of something in the real world. They are also sometimes called “business objects”.
  • A class is an outline of a “thing”.
    • It is like a blank form.
    • It has “properties” – details about the “thing”. These are the boxes you would fill in, on a form.
  • The “properties” have a “datatype”, or “type”.
    • This tells the program what type of information can be stored in the property.
  • Some common datatypes are:
    • “string” which hold letters, numbers, punctuation, spaces, etc.
    • “int” which holds whole (integer) numbers
    • “decimal” which holds numbers with decimal values, like 1.5, 2.7
    • “DateTime” which holds dates and times
  • Properties hold values.
    • You can put a value into a property – using “set”.
    • You can read a value from a property – using “get”.
  • We will put our “models” in a separate project in the solution
    • This solution is a “class library”, and will hold all the classes for our models.
    • We want the “models” separate from the “view” (the WPFUI). This makes it easier to re-use, or test, these classes (which we will do later).

Source Code

Create a new project, to hold the “models”

Step 1: Right-click on the SOSCSRPG solution, and select Add -> New Project…

Step 2: If you’re using Visual Studio 2015, choose a Visual C# -> Windows -> Class Library project. If you’re using Visual Studio 2017, use Visual C# -> Windows Classic Desktop -> Class Library (.NET Framework). Name this project “Engine”. Delete the Class1.cs file from the project.

Step 3: Right-click on the Engine project, and select Add -> New Folder. Name it “Models”

Step 4: Right-click on the “Models” folder, and select Add -> New Item -> Visual C# Items -> Class. Name the class “Player.cs”

Step 5: Create this code, in the Player.cs file, to define the properties of the Player class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine.Models
{
    class Player
    {
        string Name { get; set; }
        string CharacterClass { get; set; }
        int HitPoints { get; set; }
        int ExperiencePoints { get; set; }
        int Level { get; set; }
        int Gold { get; set; }
    }
}

NEXT LESSON: Lesson 03.4: Creating the GameSession (ViewModel) Class

PREVIOUS LESSON: Lesson 03.2: Creating and Using a Subversion Repository

2 Comments

  1. Ted Bunny
    Ted Bunny 2022-01-14

    Why “Models?” I’ve never run into this term in programming.

    • SOSCSRPG
      SOSCSRPG 2022-01-14

      Hi Ted,

      “Model” is a fairly popular term when discussing software architecture. It’s used for architectural patterns like Model-View-ViewModel, Model-View-Controller, and Model-View-Presenter.

      The “Model” is a digital model of some real-life object. For this program, the Player, Monster, GameItems, etc. If you describe what you want a program to do, in regular sentences, the nouns will probably become the Model classes. For example, you might describe this game by saying, “The player moves to a location. The player fights a monster. The player collects items.” All those bolded words are the nouns that will become the Model classes in our program.

      “View” is the term for the user interface. In this program, the View are the WindowsForms forms. A View could also be something else, like a web page. Presenters, Controllers, and ViewModels are other parts (classes) of a program that manage connecting and communication between the View and the Models.

      Another term you might encounter is “Service”. This is usually for classes that do some sort of service for the other classes. For example, a FileService class might write values from Model classes into files – or read a file and create Models.

      Let me know if that wasn’t clear, or if you have other questions.

Leave a Reply

Your email address will not be published.