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:

https://github.com/ScottLilly/SOSCSRPG

 

Overview

Lesson 00.1: Overview

 

Preparation

Lesson 01.1: Installing Visual Studio Community 2015 – Update 3

Lesson 01.2: Installing TortoiseSVN and VisualSVN

 

Planning

Lesson 02.1: Planning the Game

 

Programming

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

 

 

Future lessons (exact content and order may change)

  • UI changes
    • RichTextBox fix
    • Tooltips for quests and recipes
    • DataTemplate for Player data
    • Progress bar for health (and experience?)
  • Save/load player game data
  • Complex battle logic

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

      Steps
      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;
            tradeScreen.ShowDialog();

            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;
            tradeScreen.ShowDialog();

            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.

    https://sites.google.com/site/highlevelassembly/downloads/arayna

    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: https://scottlilly.com/video-tutorial-on-why-and-how-to-create-class-interfaces-in-c/

      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.

Leave a Reply

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