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

 

 

Future lessons (exact content and order may change)

  • Load world data from XML file
    • Monsters
    • Quests, Recipes, and Traders
  • Use Aspect-Oriented Programming for PropertyChanged code
  • 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

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

Leave a Reply

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