Lesson 16.3 – Functions to use weapons and potions

Lesson Objectives

At the end of this lesson, you will know…

  • Nothing new. We’re just finishing the program, using the same things you learned in the previous lessons.


Now we’ll write the functions the player will use when fighting monsters.

We have two things they can do (besides click on one of the direction buttons, to run away from the battle): use a weapon on the monster or use a healing potion on themselves – if they have one in their inventory.

Here is the pseudo-code for the two functions:

Use a weapon function

  • Get the currently selected weapon from the cboWeapons ComboBox
  • Determine the amount of damage the player does to the monster
  • Apply the damage to the monster’s CurrentHitPoints
    • Display message
  • If the monster is dead (zero hit points remaining)
    • Display a victory message
    • Give player experience points for killing the monster
      • Display message
    • Give player gold for killing the monster
      • Display message
    • Get loot items from the monster
      • Display message for each loot item
      • Add item to player’s inventory
    • Refresh player data on UI
      • Gold and ExperiencePoints
      • Inventory list and ComboBoxes
    • “Move” player to current location
      • This will heal the player and create a new monster
  • If the monster is still alive
    • Determine the amount of damage the monster does to the player
    • Display message
    • Subtract damage from player’s CurrentHitPoints
      • Refresh player data in UI
    • If player is dead (zero hit points remaining)
      • Display message
      • Move player to “Home” location


Use a potion function

  • Get currently selected potion from cboPotions ComboBox
  • Add healing amount to player’s CurrentHitPoints
    • CurrentHitPoints cannot exceed player’s MaximumHitPoints
  • Remove the potion from the player’s inventory
  • Display message
  • Monster gets their turn to attack
    • Determine the amount of damage the monster does to the player
    • Display message
    • Subtract damage from player’s CurrentHitPoints
      • Refresh player data in UI
    • If player is dead (zero hit points remaining)
      • Display message
      • Move player to “Home” location
  • Refresh player data in UI


These are much simpler functions than the MoveTo() function.

We’ll be able to use some of the smaller functions we created during the refactoring lesson – for example, the AddItemToInventory() function, from the Player class, if the player defeats the monster and receives loot items.


Adding functions for monster battles

Step 1: Start Visual Studio Express 2013 for Desktop, and open the solution.


Step 2: Right-click on the SuperAdventure.cs form in the SuperAdventure project, to start working with the code for the UI.

To add the ability to use weapons and potions in combat, you can add in the code below, or replace the code for SuperAdventure.cs with the code here: https://gist.github.com/ScottLilly/b20787650f2ab2a78362


For the btnUseWeapon_Click function, add this:


Then, for the btnUsePotion_Click function, add this:


There isn’t really anything new in these two functions. Just more “if”s and “foreach”s to handle the player’s actions in battle.



Now you have a working game. The player can move around in the world, get quests, battle monsters, receive loot, and complete quests.

These new functions could use some refactoring, since they are long and do several things. I’ll leave that to you to figure out what refactoring you’d do.


Source code for this lesson

Source code on GitHub

Source code on Dropbox


 Next lesson: Lesson 17.1 – Running the game on another computer

Previous lesson: Lesson 16.2 – Refactoring the player movement function

All lessons: Learn C# by Building a Simple RPG Index

231 thoughts on “Lesson 16.3 – Functions to use weapons and potions

  1. Hi Scott,

    First, thank you for the tutorial. It has been very helpful for me.

    I’ve followed the lessons up to this point and the game works otherwise, but the monsters don’t spawn at the locations. When debugging I noticed that the “newLocation.MonsterLivingHere” always stays at “null” at every location and I cannot figure out why. I’ve copied your code to World.cs, Location.cs and SuperAdventure.cs while troubleshooting, but to no avail. I’d appreciate any tips or help you can give me. Thanks.

    By the way, I use Visual Studio 2019, if that matters.


  2. Hello Scott. Firstly, I wanted to say thank you for making such a great tutorial for C#, I’m actually very new to OOP and coding in general, and I’m getting use to how everything works (Though I still have a long way to go.)

    But I have a problem in regards to this project. I’ve gotten to 16.3 with no errors, and seemingly everything is running just fine. However, there is one problem, no text is appearing in the rich text boxes! The UI and everything else seems to be fine, it’s just that the text boxes are completely blank. Did I mistakenly skim over a section without seeing something, or is it just a rookie mistake?

    I’m looking forward to hearing back to you. I got this far and don’t want to give up just yet 🙂

  3. Hello Scott, I got a problem with my Quests. I dont get the RewardGold/RewardPoints from the quests. When I fight against monsters, everything is working fine.. I tried to find a solution by myself, but I failed…

    Would be cool if you can help me out there 🙂

      1. Ok I think I found the problem. Its not updating the counter. If I kill a monster after that, the gold+experience gets updated.. Never even thought of that… xD

    1. Hi Isaac,

      I got your code and it looks like some of the classes in the Engine project are not set to “public”. For example, line 9 of Item.cs needs to be “public class Item”. Check that in HealingPotion.cs, Item.cs, Location.cs, Monster.cs, Quest.cs, RandomNumberGenerator.cs, and Weapon.cs.

      Let me know if you have any questions, or if that doesn’t eliminate the errors.

Leave a Reply

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