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; }
    }
}
Leave a Reply

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