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: Choose a Visual C# -> Windows -> Class Library project. If you’re using Visual Studio 2017, you want to use “Class Library (.NET Standard)”. 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.

 

Return to main page

2 thoughts on “Lesson 03.3: Creating the Player Class

  1. Hi Scott,

    For those with Visual Studio 2017 – there are multiple options for Class Library project type. The correct one appears to be the C# project template “Class Library (.NET Framework)” which builds to a *.dll. There may be many other options, depending on what is installed with Visual Studio. Some of the other options for me are:
    * “Class Library (.NET Standard)”
    * “Class Library (.NET Core)”
    * “Class Library (Portable)”
    * “Class Library (Universal Windows)”
    * etc.

    Actually, some of these might work – but the .Net Framework Class Library definitely works. I had a similar issue with adding the WPFUI project.

Leave a Reply

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