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.

 

Return to main page

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

  2. Hello All.. (just for info)

    Had The same Problem , with VS2017 if use .NET Standard for the Engine , The Engine submenu will be ‘Dependencies’ Instead Of Having ‘Properties And References’ As the sub menu
    Also end up with different results In the Build Action Options when add the images and try set there Properties. No ‘Resource’ option Only ‘Embedded Resource’ in the list

    Using The Class Libarry.NET FRAMEWORK ,, worked and all sub menu now match with yours

    1. Thank you. I’ve updated the lesson on creating the class library project. It’s frustrating that Microsoft decided to have several different types of class libraries, with different features unavailable in the different versions.

Leave a Reply

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