Build a C#/WPF RPG

This is a new WPF/XAML game, based on the game from my Windows Form tutorial: Learn C# by Building a Simple RPG


For the latest version of the complete solution, visit:



Lesson 00.1: Overview



Lesson 01.1: Installing Visual Studio Community 2015 – Update 3

Lesson 01.2: Installing TortoiseSVN and VisualSVN



Lesson 02.1: Planning the Game



Lesson 03.1: Creating the Solution and Game Screen

Lesson 03.2: Creating and Using a Subversion Repository

Lesson 03.3: Creating the Player Class

Lesson 03.4: Creating the GameSession (ViewModel) Class

Lesson 03.5: Displaying the Player Object

Lesson 03.6: Update Player data with the PropertyChanged event

Lesson 04.1: Creating the Location class

Lesson 04.2: Creating the World

Lesson 04.3: Moving in the game world

Lesson 04.4: Improving the World – Inheritance and INotifyPropertyChanged

Lesson 04.5: Improving the World – Factory and Guard Clauses

Lesson 05.1: Creating the Game Item Factory

Lesson 05.2: Creating the Player Inventory

Lesson 06.1: Creating the Quest Factory

Lesson 06.2: Using Quests in the Game

Lesson 07.1: Creating Monsters

Lesson 07.2: Adding Monsters to Locations

Lesson 07.3: Sending Messages from the ViewModel to the View

Lesson 07.4: Monster Combat

Lesson 07.5: Monster and Combat Refactoring

Lesson 08.1: Completing Quests

Lesson 09.1: Creating Traders

Lesson 09.2: Adding the Trade Screen


Cleanup/refactoring, before adding more features

Lesson 10.1: Refactoring Base Class for Player, Monster, and Trader

Lesson 10.2: Grouping GameItems in Inventories

Lesson 10.3: Refactoring – Encapsulating LivingEntity Properties (Hit Points and Gold)

Lesson 10.4: Bug Fix – Removing multiple items from GroupedInventory

Lesson 10.5: Encapsulating Level and ExperiencePoints Properties

Lesson 10.6: Clean up property setters and PropertyChanged notifications


Create a base for automated tests

Lesson 11.1: Creating the Unit Test Project


New game features

Lesson 12.1: Making the GameItem class more flexible

Lesson 12.2: Creating the AttackWithWeapon command

Lesson 12.3: Making the Action class more flexible with an interface

Lesson 12.4: Letting the Monster use AttackWithWeapon

Lesson 12.5: Creating the first consumable GameItem

Lesson 12.6: Refactoring after adding Actions

Lesson 12.7: Creating recipes

Lesson 12.8: Crafting items with recipes

Lesson 13.1: Add keyboard input for actions, using delegates

Lesson 13.2: More keyboard actions (and fixes)


Load game world objects from XML files

Lesson 14.1: Moving game data to external files

Lesson 14.2: Creating extension methods

Lesson 14.3: Read World (Location) data from an XML file

Lesson 14.4: Read Monster data from an XML file

Lesson 14.5: Move Remaining Game Data to XML Files



Lesson 15.1: Bug Fixes, Unit Tests, and Tooltips

Lesson 15.2: Catch and log exceptions

Lesson 15.3: Building a “functional” inventory class

Lesson 15.4: Using GitHub to upload and download a solution


Better combat

Lesson 16.1: Adding centralized messaging

Lesson 16.2: Creating the Battle class

Lesson 16.3: Complex attack initiative and hit success logic


Continued lessons

Lesson 17.1: Saving and loading game state

Lesson 17.2: Reducing serialized data in save game file

Lesson 17.3: Add a menu to save and load the game state

Lesson 17.4: Automated test for saved game reader


Lesson 99.1: Fix event subscriptions

172 thoughts on “Build a C#/WPF RPG

  1. Hi Scott,

    I was wondering what would be the best way to customize the character. I’m just looking to change the name, I’ve tried many things but none of the work since I have to instance GameSession before altering the name, thus it instancing the player then changing the name. What would be your take on this?

    1. Hi David,

      Have you reached lesson 09.2, where we add the Trade screen, and set its DataContext? If so, I’ll give you some hints to (hopefully) help you. If you try it, and have problems, let me know so I can give you more details.

      1. Create a new window in the WPFUI project. I’ll call it CharacterCreation.xaml, for this example.
      2. Modify WPFUI\App.xaml. Change the StartupUri attribute from “MainWindow.xaml” to CharacterCreation.xaml”. This will make CharacterCreation the first screen displayed when you run the program.
      3. Put your character name textbox in this CharacterCreation, and a “Start Game” button with a “Click” event handler function in CharacterCreation.xaml.cs.
      4. Modify the constructor in MainWindow.xaml.cs to accept a string parameter for the characterName.
      5. In the CharacterCreation “Click” function, you’ll do the code similar to creating the TradeScreen – except, instead of setting the DataContext, pass in the character’s name when you instantiate the MainWindow object.
      6. In the CharacterCreation “Click” function, do “Show()”, instead of “ShowDialog”. Then, add “this.Close();” (which will close the CharacterCreation window).
      7. Modify the GameSession constructor to accept a string parameter of the character’s name.
      8. In the MainWindow.xaml.cs constructor, set “_gameSession” to “new GameSession(characterName)”
      9. In the GameSession constructor, use the characterName parameter when you instantiate the CurrentPlayer object.

      Let me know if any of that isn’t clear, or if it doesn’t work (I did that from memory, and might have missed something).

        1. Hi Caleb,

          The constructor for a window works the same as a constructor for any other class. So, you would change “public MainWindow()” to “public MainWindow(string playerName)”. Then, you can pass the “playerName” parameter into the GameSession object and use it to set the Player’s name.

          1. Thanks for the reply now I have another question how do I go about doing the following
            In the CharacterCreation “Click” function, you’ll do the code similar to creating the TradeScreen – except, instead of setting the DataContext, pass in the character’s name when you instantiate the MainWindow object.

          2. When you instantiate a new Window, you can pass data into it by setting the Window’s DataContext property, like this:

            TradeScreen tradeScreen = new TradeScreen();
            tradeScreen.Owner = this;
            tradeScreen.DataContext = _gameSession;

            But, if you change the window to accept a parameter, like the player’s name, you would need to pass that value in when you instantiate the Window, like this (pretend the TradeWindow constructor expects the player’s name as a parameter):

            TradeScreen tradeScreen = new TradeScreen(_gameSession.CurrentPlayer.Name);
            tradeScreen.Owner = this;
            tradeScreen.DataContext = _gameSession;

            Does that make sense?

  2. Hey Scott

    Thank you very much for your tutorial. It give me a lot of knowledge and i wait for next lesson. But i have a questions. Maybe you know so good tutorial how use WPF, Entity and SQlite or some other database for my little project? I search but i dont see any good tutorial :/

    1. I’ll definitely keep a player-creation lesson in mind. Right now, I keep getting new projects from clients, and haven’t been able to work on any more lessons. Hopefully, those extra projects will slow down, so I can get back to this project.

  3. Hi from indonesia here.
    your content are nice,,, i learn a lot from dis site
    i just want to suggest to add an online feature..
    like partying with local friend.
    joining through an ip address,,,

    it would be nice,, thank you

  4. Hey Scott,
    If you plan on doing future lessons for this still, I recommend a lesson where you show us how to rework the quests system so that traders, or perhaps a new object, quest givers, would need to be spoken to in order to receive quests. So, something like the trade screen but with quests, so that quests aren’t so automatic as they are now.

    Thanks Scott for this tutorial series!

    1. Thanks Matt,

      I’m continuing the lessons in January. I’m finally starting to get some free time in my schedule.

      I’ll keep your suggestions in mind (this one and the location connection one).

  5. this is a fantastic resource! it reminds me of a game engine i wrote years ago using high level assembly language called arayna which was a gui based text adventure game engine.

    i had designed a scripting language where you could “program” the interface with your adventure rooms, items, monsters and even create buttons to add to the interface.

    if you are interested in taking a look, maybe it will give you some ideas for your own game engine. i went as far as to create several test and demo mods to showcase what the engine can do.

    1. Cool! I’ll take a look at your program this weekend. It would be nice to add a scripting engine to this game and make it more flexible.

      Thanks for sharing your program.

  6. Hi! I just started programming a few weeks ago. I have been wanting to start making a simple game just like this one, but I had no idea where to start. I did not want to make a boring console version with a main menu, a while loop and a million if statements, where I spend way more time writing new dialogue than actually programming game functionality. This is exactly what I’ve been wishing for these last few weeks. I just wanted to thank you in advance for making this awesome resource – and for offering it for free. I am going to start on lesson 2 right now. Also the design patterns section looks very useful and well written.

  7. I just wanted to say thank you for doing these tutorials. I’ve been learning Java for over a year in college, and when I saw that I was going to be learning C# from here on I wanted to get a head start on it. Your C# RPG tutorial caught my eye on google, and I went through it in a couple days.

    You make it all so easy to learn, and the idea of building a video game is brilliant. I appreciate you putting the effort into these tutorials for all of us noobies.

  8. Thank you so much for these tutorials, they are really helpful with getting into programming. I have two questions for you.
    One, if I wanted to add a lot of stats to the character but do not want to have a huge number of variables all piled up at the start of the character class is there a good way of braking those off. Inheritance seems alright but someone was telling me I should use interfaces. I am confused about the interfaces through as it looks like they force you to have variables in the function.
    Two, is there a good way to set up the gamesession class to be accessible from multiple windows. I am trying to have a character setup process at the start of the game that would involve at least 3 steps, name and sex, choosing basic stats and then perhaps buying gear at the start. It seems to add complexity to have to pass the values from one menu to another.
    Thanks again I know that you must spend quite a bit of your time on this and I certainly appreciate it.

    1. You’re welcome, Ben!

      You might be able to add some of the new properties as a list. For example, with a traditional Dungeons and Dragons type of character, I would probably have individual properties for things like Race, Gender, Strength, Dexterity, Wisdom, etc. But, I might make a List (or Dictionary) of skills. That list would store the skill name and the player’s level (Stealth 50, Lockpicking 23, Alchemy 72, etc.). If I understand what you’re trying to do, you probably don’t need inheritance of interfaces. If you want to know more about using interfaces, I have an old video here:

      Have you gotten to lesson 09.2 yet (where we add a trader screen)? If you look at lines 55-61 of MainWindow.xaml.cs, you’ll see how to instantiate a new forma, set it’s DataContext to the current GameSession object, and open the window. When you do this (set the DataContext to the current GameSession object), the new screen has a “reference” to the existing GameSession object. There is only one GameSession object in memory, and both screens are using it. So, when you make a change in one screen, you would see the changes in the other.

      Let me know if you still have questions.

  9. Hey Scott can you create a lesson on how to change the characters name and customize classes. I think we could handle a lesson on this.
    Thanks Scott for what you have done so far.

  10. Hey Scott,

    I started to dabble in C# a couple of years ago and worked my way through your original tutorial (as it was back then) for SuperAdventure. Using what you had taught me I made my own Dopewars-esque trading game, though more TradeWinds themed!

    Sadly I lost the solution in a PC transfer (always backup!!) and never got back into coding until recently.

    I struggled through a few YouTube videos (mostly of people explaining things so slowly I had to watch on 2x speed!) of some of the basics as a refresher, but was so happy to find your website again!

    Anyway, I’ve just worked through the ‘old’ tutorial again, upto refactoring and I wanted to thank you for keeping this going, and for free!

    I quite fancy something ambitious (for my skill), maybe text-based Runescape or something!

    I would also like to get into Unity for coupling graphics with C#…

    Thanks again!

  11. Very nice tutorial…I’ve not been all the way through it yet. I’m a Sr. Software engineer but I haven’t worked with WPF before so it’s a great intro to that. If you haven’t already done it would love to see you add a section making it compatible with running on IIS/Browser. even if it is only windows browser that would be nice.

    Thanks Again!

    1. You’re welcome, Dale!

      I haven’t done any web development in many years, but may need to take it up again for work. If I do, I’d have to think about whether to create an MVC/Razor front-end, or do a Javascript-based UI (Angular 2 is really popular where I’m at).

  12. Thank you for the great tutorial, very neat introduction to WPF.

    Just wanted to ask if you have any recommendations for resources to use/read up on in regards to introducing saving/loading to the game?


    1. You’re welcome, Max!

      I haven’t gotten to adding a save game feature to this version yet (work projects keep getting in my way). But, you could see how it would be done in the lessons from the previous (Windows Forms) version of the game. There is a lesson for saving it to XML and three lessons on how to save it to a SQL database, starting at:

  13. This has been excellent. I’m really enjoying this tutorial. The explanations are excellent and you cover important details.

    One thing I think would be really neat, is if we could have a map feature. Perhaps we could populate locations on a grid based on the information in the WorldFactory.cs

  14. Thank you so much for these lessons! I have a pretty extensive background in Admin scripting so writing code or understanding programming logic isn’t an issue, but I didn’t know how to create “real” programs and how to organize a project in Visual Studio. Finally, the light bulb has gone off!

  15. Hey Scott, been a minute. Have you thought about setting up a Slack so that these guys can maybe collaborate with each other a bit more rather than just them suggesting features for you to create? I would think as they get later in the lessons, some light bulbs should be going off and there should be some ideas to mull around and try to come up with reasonable answers on their own. Heck, even setting this up in GIT making it open source and really teach these guys some industry standard stuff. just a couple ideas. Great job.

    1. Hi Rylsngrd,

      I haven’t thought about a Slack channel. I might do one, but I need to decide if I can continue with this project. The .NET world, and what most employers are looking for, is different from when I started this project. Now, most companies (at least the ones around me) are looking for .NET Core to create REST APIs that run in Azure, or other containers. I recently started a new long-term project for a new client, and need to figure out what’s the most useful thing I can do on this site.

      The source code is open source (MIT license) on Git at:

  16. Hi,

    I’m in the process of interviewing for a Jr. C# position with no prior experience/education in C#. I’m looking for one project to put on my portfolio before that interview (late next week.) If I learn the fundamentals of C#. How long should this project take

    1. This would probably take more than a week, unless you have experience in a similar language, like Java. Without similar experience, I would guess two or three weeks. But, everyone progresses at different rates.

Leave a Reply

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