Learn C# by Building a Simple RPG – Index

Giant Spider!
Mandatory Giant Spider!

If you want to write a Role Playing Game, but don’t know how to program, or just want to learn how to program in C#, then you’re at right the place.

These lessons will take you from a complete beginner, to being an author of a Role Playing Game, for free.

Now, this isn’t the world’s greatest game. In fact, it’s very short and kind of ugly.

However, as you create it, you’ll learn the most common C# programming practices and techniques. Then, if you want, you can improve the game, adding more features and your own special touch to it.

 

NOTE: One of the awesome students here created a PDF document of the lessons. You can get a copy of it here, or here. You can also download the source code files for the lessons here, or here. Thank you, Mark!

 

SECTIONS

Lesson 00.1 – What is in these lessons?

Lesson 00.2 – General information about programming in C#

Lesson 00.3 – The parts of Visual Studio

Lesson 01.1 – Defining classes and objects for the game

Lesson 02.1 – Installing Visual Studio Express 2013 for Desktop

Lesson 02.2 – Building the solution for the game

Lesson 03.1 – Building the first screen

Lesson 04.1 – Creating the Player class and its properties

Lesson 05.1 – Creating objects from classes

Lesson 06.1 – Creating the remaining classes

Lesson 07.1 – Inheritance and base classes

Lesson 08.1 – Setting properties with a class constructor

Lesson 08.2 – Using class constructors with derived classes

Lesson 09.1 – Using your classes as datatypes

Lesson 10.1 – Creating collections of objects

Lesson 11.1 – Using a static class

Lesson 12.1 – Add the remaining UI controls

Lesson 13.1 – Functions, procedures, and methods

Lesson 13.2 – Creating functions to handle user input

Lesson 14.1 – Variables

Lesson 14.2 – If statements

Lesson 14.3 – Foreach loops

Lesson 15.1 – Getting random numbers for the game

Lesson 16.1 – Writing the function to move the player

Lesson 16.2 – Refactoring the player movement function

Lesson 16.3 – Functions to use weapons and potions

Lesson 17.1 – Running the game on another computer

Lesson 18.1 – Future enhancements for the game

Bonus lessons (enhancements to the game)

Lesson 19.1 – Scroll to the bottom of a rich text box

Lesson 19.2 – Use a calculated value for a property

Lesson 19.3 – Clean up the source code by converting foreach to LINQ

Lesson 19.4 – Saving and loading the player information

Lesson 19.5 – Changing dropdown default values

Lesson 19.6 – Increase maximum hit points when the player gains a level

Improving SuperAdventure’s code quality by refactoring

Lesson 20.1 – Refactoring the SuperAdventure program

Lesson 20.2 – Binding a custom object’s properties to UI controls

Lesson 20.3 – Binding list properties to datagridviews

Lesson 20.4 – Binding child list properties to a combobox

Lesson 20.5 – Moving the game logic functions from the UI project to the Engine project

Adding a vendor to locations (with buying and selling items)

Lesson 21.0 – Plans for adding a vendor to locations

Lesson 21.1 – Adding a price to game items

Lesson 21.2 – Create the vendor class and add it to locations

Lesson 21.3 – Add a button and create its eventhandler in code, without the UI design screen

Lesson 21.4 – Completing the trading screen

Use SQL to save and restore player’s game data

Lesson 22.1 – Installing MS SQL Server on your computer

Lesson 22.2 – Creating database tables from classes

Lesson 22.3 – Creating the SQL to save and load the saved game data

Creating a console UI for SuperAdventure

Lesson 23.1 – Creating a console front-end for the game

Final refactoring (cleanup) of the SuperAdventure source code

Lesson 24.1 – Make the SuperAdventure source code easier to understand and modify

New game features

Lesson 25.1 – Select a random monster at a location

 

Share your personal, expanded versions of SuperAdventure here!

221 thoughts on “Learn C# by Building a Simple RPG – Index

  1. Good Morning from Germany,

    i receive the error “The type or Namespace Name ‘MessageEventArgs’ could not be found(are you missing a using directive or an assembly reference?)

    The error occurs twice in the Program.cs, twice in the Player.cs, and once in the SuperAdventure.cs.

    Additionally i receive the error “No overload for ‘Player_OnMessage’ matches delegate ‘EventHandler<MessageEventArgs>’

    I hope you can help me with this, im Close to finish this lessons and i already learned soooo much!

    Thanks and best regards,
    Gerrit

  2. Nevermind, im sorry,
    just overread an class and didnt add it at the time.. just found it now..

    Now i got no Errors and im finished, but if i try to debug nothing happens, no window or something else..

    Regards,

    Gerrit

      1. Here it is:

        LINK REMOVED, FOR PRIVACY

        But i got another Problem now.. i can debug it, but it wont work because of some exceptions =/

        1. I noticed a problem in the Program.cs class (in the UI project). It looks like it has some code pasted into it from SuperAdventure.cs. Try replacing all the code in that class with this:

          using System;
          using System.Windows.Forms;

          namespace SuperAdventure
          {
          static class Program
          {
          ///

          /// The main entry point for the application.
          ///

          [STAThread]
          static void Main()
          {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new SuperAdventure());
          }
          }
          }

          Also, because you are using a different type of database, you will need to change the connection string in the PlayerDataMapper class to this:

          private static readonly string _connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\SuperAdventure.mdf;Integrated Security=True;Connect Timeout=30";

          Let me know if that does not fix the program for you.

  3. Hi, I’ve just started using this tutorial, and I have reached the point in Lesson 5 where I must enter

    using Engine;

    Below the rest of the using lines. But it is telling me that the “using directive is unnecessary”.

    And that ‘The type or namespace ‘Engine’ could not be found (are you missing a using directive or an assembly reference?)”

    but I have created an Engine reference in the Super Adventure project and can see it in the references section.

    This is stopping the datatype Player removing the red squiggly line.

    Any help would be greatly appreciated!

    1. Can you check if the Player class has these lines near the top of the class:

      namespace Engine

      and

      public class Player (especially the “public”)

      If it has those lines, did you create the Engine project with a different name, then rename it later? If so, check the solution here: Lesson 02.2B – Fixing the namespace

      If that doesn’t help you solve the error, can you upload your solution (all the files) to Dropbox or GitHub, so I can examine it?

  4. Hi, I’ve been following your tutorial in my free time to refresh my memory of C# before I go back to university and I’d just like to say thank you!

    Ever since I completed the tutorial it’s been so much fun & headache learning to change/add all the new things I’ve been adding to the game. (some days I’d spend hours trying to figure out a problem that I’d end up solving in minutes the next day)  Right now, I’m working on a special attack button for special weapons and then I’d like to have the default selected weapon in the combo box be your most powerful weapon.

    Once again, thanks!

    1. You’re welcome! I’m glad you enjoyed the tutorial.

      It’s amazing how much a night of sleep will help with finding a solution to a programming problem. I hope you have a good year in university.

  5. Hi and greetings from Germany 🙂

    I have a problem with adding a Spawnrate for Monsters. I managed to add a Lootrate for Items, but if I try to do the same with the Monsters, I always fail. I loot different things from the Monsters, but there is always the same Monster spawning, even if I put the Spawnrate for other Monsters up to 100%. Hope you can help me with it.

    Also I would like to add Player Classes (Warrior, Mage, etc.) to the game. Would it be possible that you could write a tutorial for that?

    With best regards
    Dennis

    1. Hello from Texas 🙂

      Can you upload your solution (all the files, including the sub-folders, and their files) to GitHub or Dropbox, so I can examine them?

      For the different player classes, have you seen my demonstration of “Composition over Inheritance“? That might give you an idea how to create the different classes. The new WPF guide will use this technique to make different character classes (Warriors, Mager, etc.). If that article does not help. please tell me.

  6. Here you go:

    LINK REMOVED FOR PRIVACY

    Didn’t work on it for a while and was frustrated after several trys 😀

    Hope you can help me with it 😉

    1. I had some code to allow spawning different monsters at a location. However, I could not find it.

      I’ll write something new, post it in a few days, and let you know when it is ready.

  7. hi Scott ,i have visualy looked at few of your lessons and i think there good.i need some advice on whether i should go through them all,i am currently struggling to learn c# i want to learn it so i could make games on unity  ,do you think its worth my time? like its alot of lesson and then its like what if i dont need alot of what ive learnt from the lessons-for unity scripting .

    i would be grateful if you would reply.

    Thanks

    1. If you only want to create games in Unity, you might want to follow a different guide – one specifically for Unity.

      This is more of a guide for learning C#, while using Visual Studio. The “game” is not the main focus. Unity has a different editor, and (I believe) built-in classes and libraries, that are designed to build a game faster. The Quill18Creates channel on YouTube looks like a good one for Unity.

  8. I’ve been having issues with Lesson 16. I’m attempting to run the program to see if the MoveTo function works, so I may not be supposed to do that, but I would assume that the thing could run before I add the battle logic, right? Well, I keep getting an error on this line while building and running it in debug.

    rtbMessages.Text += “You receive the ” + newLocation.QuestAvailableHere.Name + ” quest.” + Environment.NewLine;

    System.NullReferenceException

    Is this going to be fixed in the next lesson or did I do something wrong? It’s more likely the latter since I messed up the Quest part and I had to go back to PlayerQuest and fix the constructor.

    1. That error should not be happening.

      There are four classes where I think the error might be happening, but I would need to see the source code, to find the exact location. Can you upload your solution (and all the files, in all the solution’s sub-directories) to GitHub, or Dropbox, so I can look for the error?

        1. Double-check the SuperAdventure.cs class with the code here: Lesson 16.1 source code. In the MoveTo function, it looks like there is a line missing – line 100. This is the “if” statement that checks if there is a quest at the location. If you correct the MoveTo function to have that condition, the error should go away.

          Please let me know if that does not solve the problem.

  9. Oh! Okay, I looked at it some more since I was a bit bored waiting, and saw that it was forcing it to look for a quest and give the player a quest in a no quest area. That was why it’s null. I added a simple if statement just checking if there was a quest, and if there wasn’t, it would skip that whole block of code. I showed my changes in a new branch on GitHub. I think it would have been fixed once I got to the refactoring though.

    1. That’s the basic solution. However, you would probably want to add that “if” at line 100, instead of line 234. That will correctly match it with the “else” statement on line 232.

      Because that code has several levels of curly braces, it might be better to copy the code for that function from the Gist page, and paste it over the current MoveTo function. It’s easy to get something out of position, when the “if: statements go that many levels deep (something we would want to clean, in a more “professional” version of this program).

      1. Just saw your above comment to my original post. I basically just did what you did in a different position. Thanks for the help and definitely for the entire tutorial.

  10. SoI’ve been working on this for about two weeks now and I’m at the point where I am adding my own stuff into it. However I have an issue when I go to debug the program. I get a NullReferenceException that seems to be linked to the player.XML file. When ever I delete the file, it loads up normally, obviously without any of the saved stuff from the previous run.

     

    I am getting the error in player.cs line 151. I am going to try to do a release to see if the issue persists there as well.

     

    Link to player.cs on pastebin: http://pastebin.com/S7XkuBr5

     

    Appreciate your help, without this tutorial I would never of gotten as far as I have.

      1. I didn’t see any obvious errors.

        Can you run the game, exit from it (to save the XML file), and see if the XML file has a value in the “CurrentLocation” node? If there is a value there, make sure you have a location with that ID in your World class. That’s the only other thing I can think of right now.

        If you want to see what is happening when the game reads and writes the XML file, you could use the debugger (tips for how to use it, in this article). I’d set breakpoints in the Player class, on lines 100 (to see what is happening when the game reads the XML file), and line 571 (to watch what is being written in the XML file).

        If that doesn’t help you find the source of the problem, I would need to have the complete solution, so I can run the game, and watch what is happening. You could upload it to GitHub, or Dropbox, so I could look at it.

  11. So I figured out the problem, it was in my world.cs and right after the connecting of the Cells to each other there is a place to add the locations to the static list, Noticed that the ones I could save in where listed there and the ones I could not save in where not, adding the ones not listed than going into the XML file and changing the location ID to some place that I could not previously load into, debug the game and it worked!

     

    Thanks for your help!

  12. Hello Scott, great tutorial!

    Are you able to create a tutorial for “How to exclude hard-coded stuff”?
    My World.cs is growing so fast and the performence ist dying..

    Just for the “great parts” like the mapping-code, or the item-parts, that would be extremly amazing.

    Thanks for this lessons so far, great Job.

    Tyia

    1. Thank you! I’ve thought about creating a lesson (or an app) to help create the game data in an XML file, and edit it through a simple user interface. I’ll think about what it would require, and see if I can do that. I might need to do it in the new C#/WPF version of the tutorial – although it should be easy to adapt for people using the Windows Forms version of the tutorial.

      I’ll let you know when (or if) that is ready. It will probably take a while to do.

  13. Hi Scott, thanks for the amazing tutorial. We just opened our “Game Design” undergraduate program this year and I am the instructor of the “Introduction to Programming” course given in C#. I covered the major topics using “The C# Player’s Guide” book and I will be grateful if I can use your tutorial and PDF document of the lessons for the remaining weeks. Can I integrate your lessons into my course?

  14. Hello Scott. There is something I have wanted to try, but I feel as though I am running in circles a bit. I was wondering if you might create a lesson that could help, either here or in your new tutorial. Or, should it be the case, if you might tell me which of the existing lessons could be adapted for what I am trying to do. To help explain, I added something for “fire damage,” and that can exist by itself as a “spell” or as part of another class, such as an “enchantment” on a weapon. Right now, though, I have each effect individually coded.

    What I hope to accomplish is twofold: first, to code the fire effect in one place that can be called by the spell or the enchantment, and second, to make it flexible enough that I could add, for example, a cold effect that could be run through the same method. Broadly speaking, I am curious if there is some efficient, reasonable way to code a modular system for applying varied effects with different classes. Would that be something you might add as a lesson, or would that be going beyond the “simple” scope?

  15. Um, So I’m using this tutorial as a sort of way to learn C# (Mainly because I really only need to learn the basics to start off and this is the only thing I’ve found that actually teaches me what I want and need to know without it just entirely flying by my head) Anyways I’m at the point of implementing health and damage and all that and I want to figure out how to do stats.

    My only problem with stats is that I have no clue what I’m doing. I’m sort of straying from the tutorial as I already know a bit of C# (Admittedly I learned what I do know from doing this tutorial at an earlier point). Really the only thing I’m stumped on is doing the stats. After that I’m basically home free and I know most of the rest.

    So I was wondering if you could help me figure out how to do the player stats? I mean I know how to set them but what I haven’t been able to figure out is how to access them in a battle.

    1. For the player stats, are you talking about things like strength, dexterity, constitution, etc.?

      If so, I would make them new public properties in the Player class. Then, I would modify the functions for attacking, and using potions. I would probably modify the lines that use random numbers – to determine if the player hits the monster, if the player is hit by the monster, and how much damage is done. If the player has a low dexterity, I might subtract five points from the random number that determines if the hit the monster (to make the player less likely to hit the monster). If they have a high dexterity, I would add five points. If they have a low strength, maybe subtract one point when we calculate how much damage they do to the monster. For a high strength, add a point of damage.

      Then, be sure to modify the player save and load functions to include the new properties.

      Does that make sense?

  16. I went through the tutorial and my program worked but there were a few logic errors on my end connecting things, I wanted to just use the source code so I could alter it to add things to the game at this point.

    I tried copying all of the source files into Visual Studio from the Lesson Folders but they are giving me errors. Would we be able to get a final source folder specifying which files go into Engine and which files go into SuperAdventure? Or even just a Visual Studio project folder?

Leave a Reply

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