Lesson 07.1: Creating Monsters

In this lesson, we will create the game monsters.

Like most of the other game objects, we will use a factory class to create the monsters. When we create a monster object, we also need to give it random loot, for the player to collect after they defeat the monster.

We are also going to add images of the monsters, to display on the game screen.




Step 1: Create the new class Engine\Models\Monster.cs.

This class inherits from BaseNotificationClass, so it can update the UI when the monster’s CurrentHitPoints property changes.

The class also has an ImageName property, which is the image file we will add for the monster in Step 4.

Inside the constructor, we accept the parameters, and use them to populate the properties – including adding the location of the image file for the monster.




NOTE: If the monsters do not display in the UI, try changing the ImageName line to this:

In some environments (I don’t know which ones), the program has trouble finding the image resource. There is more information about Pack URIs here.


Step 2: Update Engine\Factories\ItemFactory.cs

We need to create some more items in the game world, to add to the monster’s inventory – for the player to loot from them.

For the monster loot items, I’m using a starting ID of 9001 – just to keep all these items grouped, and easier for us to find.





Step 3: Create Engine\RandomNumberGenerator.cs

To make the monster loot random, we need a random number generator. The standard random class is not very random. So, we can add this version, which creates numbers more randomly.

If you don’t need a complex random number generator, or if you’re using a project that does not have access to System.Security.Cryptography, you can use the SimpleNumberBetween function, at the bottom of the class.





Step 4: Add monster images to Engine\Images\Monsters folder.

Create the new folder, download the images (using the link below), and add the image files to the new folder.



Rat.png   Snake.png   GiantSpider.png

All images, in a zip file (right-click and save)

After adding the image files to the solution, right-click on each one, select “Properties”, and set:

Build Action = Resource

Copy to Output Directory = Do not copy



Step 5: Create Engine\Factories\MonsterFactory.cs

Now that we have all the pieces, we can create monster objects.

We are going to use a factory to create a new monster object for each battle. By creating a new monster object each time, each monster will have its own random loot.

We want a new Monster object, each time we call the MonsterFactory – like how we need a new, unique Item object, each time we call the ItemFactory.

In the ItemFactory, we created a list of standard items. When we needed a new Item, the factory found the standard Item and called the Clone function, to create the new, unique object.

For the MonsterFactory, I’m using a “switch” statement, so you can see how that works.

The “switch” line (line 10) looks at the monsterID value that was passed in to GetMonster. Then, it looks for the “case” line with a matching value (lines 12, 20, and 28). When it finds the matching value, it runs the code after it, until it gets to a “return” (which returns a value, and does not run any more code in that function), or a “break” line (which goes to the first line of code after the closing line for the “switch” statement – line 38, here).

On line 36, we have “default”. This is the code that should run if there is no “case” statement with a matching value. For this function, that should never happen – because we are passing in the monsterID value. But, if it does happen, we create an ArgumentException object, with a message about the invalid monsterID.

On line 41, we have a function “AddLootItem”. This function accepts parameters of the monster object, the ID of the loot item, and the percentage change that the monster has that loot item.

To make the monster loot random, this function will pick a random number between 1 and 100. If that number is less than, or equal to, the “percentage” parameter, the item will be added to the monster’s Inventory. If the random number is higher than the “percentage” parameter, the item is not added.





In lesson 7.2, we will add monsters to locations, and let the player encounter them, when they move to a location that has a monster. After that, in lesson 7.3, we will add combat – letting the player fight monsters.


Return to main page

2 thoughts on “Lesson 07.1: Creating Monsters

  1. thanks again for your videos! After watching your video, this part of the code puzzles me. I understand what it means, but I wonder… Why the property Inventory isn’t initialized in the constructor as a parameters as the other 6 properties/parameters? I mean…initialised with a lowercase and included at the top of the constructor. What makes it different compared to hitPoints, rewardGold, etc? Best regards Scott!

    public ObservableCollection Inventory { get; set; }

    public Monster(string name, string imageName,
    int maximumHitPoints, int hitPoints,
    int rewardExperiencePoints, int rewardGold)
    Name = name;
    ImageName = string.Format(“/Engine;component/Images/Monsters/{0}”, imageName);
    MaximumHitPoints = maximumHitPoints;
    HitPoints = hitPoints;
    RewardExperiencePoints = rewardExperiencePoints;
    RewardGold = rewardGold;

    Inventory = new ObservableCollection();

    1. You’re welcome!

      You can have a parameter whose datatype is a List of objects. However, I personally don’t like to do that because most people would then create a temporary List variable that had a very short “life”. Here’s some code that shows you how I prefer to add items to a List property: https://gist.github.com/ScottLilly/717fba395154c445237011058c809c5b

      Of course, there may be times where you already have the List from something else. In that situation, I might use a List parameter.

Leave a Reply

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