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.



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

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

  1. I like this tutorial a lot. It’s exactly what I was looking for. I’ve already finished through 13.1. I can’t wait for your future lessons as I’m trying to learn C#, while also building a simple text based game (although it doesn’t feel that simple yet). I think you’re doing a great job and I’ll keep checking back for the rest of your lessons. I bookmarked this website. Cheers.

    1. In case you haven’t seen, there are several new lessons available.

      Lessons 14.1, 14.2, and 14.3 cover variables, “if” statements, and “foreach” loops.
      Lesson 15.1 adds a class to let you create random numbers.
      Lessons 16.1, 16.2, and 16.3 add code to move the player around, let them get quests, fight monsters, and complete quests. These lessons are a bit long, since they cover a lot of code.

      At the end of lesson 16.3, you’ll be able to play the complete game, running it from within Visual Studio. The next lesson will be on how to run the game on other computers, without Visual Studio installed.

  2. I just finished this series, and I have learned so much! This series was amazing, and it was very well done. Thank you so much for making this series! It really gave me a huge boost in the right direction.

    1. Thanks for letting me know it helped you.

      If you want to see how the game would look with some more advanced techniques (such as, LINQ and raising events to communicate from the Engine project to the UI), I’m currently working on it here: https://github.com/ScottLilly/ShinyRockHunter. I don’t have any lessons for LINQ or event-handling (at least, not yet). But I may write some when the app is closer to being completed, if people are interested.

  3. I was looking for a website that teach C# by doing project. Seems like I come to the right place!

    Thanks for all your effort. Bookmarked this website and going to start learning now :)

    1. I see you offered to answer questions to another poster, I would love to ask a couple myself, if you are willing to answer them or explain some more about the language and class system.

      1. Certainly. I’m working on a few projects right now. So, if they’re difficult questions, it make take me a few days to write up an answer for them. But feel free to ask.

        1. Thanks Scott. Your WinForm RPG tutorial was great. I have taken your exampled and converted it to a WPF. I am still trying to wrap my head around how to set things up on the back end. I have been playing around with the code and got a very basic attribute system (strength, endurance, et cetera) and a class/role system (knight, wizard, et cetera). But I think I may have not set it up very well, because it is clunky and difficult to use/reference. My question is how can such a system be designed to be versatile enough to allow for the application of bonuses (from for example equipped equipment stacked on top a base build) but simple enough not to require too much coding. Forgive the newbie question, but this is probably just me not being fully versed in how C# class and objects propagate and get passed through loops of code.

          1. There are a few different ways you can implement applying bonuses. My first questions are 1) What things do you want to receive bonuses (attributes like strength and endurance, things like “to hit” percentage, etc.) and 2) What items do you want to affect the attributes (just wearable/usable items, or do you also want to be able to add gems to wearable/usable items – for customized bonuses)?

            Either way, I think the basics would be for you to have a list of the current items the player is using/wearing, within the Player class. You could do this with some class-level variables or properties for the Player class (e.g., CurrentWeapon, CurrentHelmet, CurrentNecklace), or add an “InUse” property to the Item class, and manage setting the player’s currently active items through the Player class, when they want to wear/use a different item. You could also add some new properties to the Item class for ToHitPercentBonus, ProtectionFromMagicBonus, etc. Set those values to zero, except for the properties you want that item to give a bonus for. Then, in your “AttackMonster” method (for example), you would check the player’s inventory for items that have bonuses that affect the battle.

            Let me know if that makes sense. If it isn’t clear, or you think your code still looks clunky, maybe you can put the relevant classes in a GitHub Gist for me to take a look at. Although, I may not be able to check it out until this weekend. I’ve got a lot of work and meetings this week.

  4. I’m new to coding and I really want to create my own RPG game and I think this tutorial will help. Bookmarked on my browser and thanks a lot.

  5. Hey, I was going to put a comment earlier about how none of the code you gave worked. I have a little bit of knowledge in other programming languages such as C++ so i was able to understand the code to a certain extent. I found nothing that was wrong with the programs that I had made but there were still errors in a lot of the coding. I just could not figure it out. Since i am new to C# and visual studio, I am not too familiar with the default settings and some of the other things that go along with these two things. I saw how in one of the early lessons that you said that visual studio would automatically put some code into a new class. When i created a new class, that coding didn’t even show up so i just took the code from the source code you gave and tried it out. it still didn’t work. Well, after doing all of that trying to figure out what was wrong with it I just gave up and went on to try to find a different tutorial for C# thinking that these tutorials might have been out of date or something like that. After a few minutes of searching for something else about programming in visual studio, I found out that visual studio has more than just C#. At first that was all i thought it had but after I figured out that it also had C++ and visual basic, i thought that maybe I was not using the right language when i made a new project. So, I went into the project that i was working on with these tutorials and it turns out that it was the problem. By default, visual studio set the language to visual basic and that was what it was trying to compile in but I was trying to write in C++. So, anyway, just wanted to say that these are great tutorials and i really like them. I just have one question though. Is there a way tp change the language the project is set to after it has been created?

    1. Hi Levi,

      Yes, Visual Studio supports several languages: C#, VB.Net, C++, and F#. I’ve heard there are ways to use it for some other languages, like Ruby and Python – although I’ve never used it for that. So, when you create a project, you need to be sure to select the correct language for the classes you’ll create in the project.

      Unfortuntely, there isn’t really a way to change the language of a project after it’s been created. It’s best to delete the project with the incorrect language and create it again with the correct one.

  6. Hey!

    I wrote an e-mail about 2 hours ago (about 2015.02.02 19:20). Before this tut i could do nothing in windows form, just console. Now i won’t say i’m good at it, but i could add picture for every location, add new location, items, monsters, and stuff. My next goal will be talking with ,,npcs” (meaning more quest and shop). Thanks for everything. :)

  7. Great tutorial. I am having a few issues in running the program. Hope you can help. I got through all the lessons and ran the program. The program starts successfully. However, when I move around, I noticed that the combo boxes did not appear when there was something to do in a room such as an Item to pick up. It didn’t pick up the sword at the fountain and when I get to the rats, there is nothing to use against them. Any idea where I should look to make the combo boxes visible at the appropriate time? Not sure if you have enough info to answer this question.


    1. Thanks Tom.

      Did you start the tutorial a while ago? In an older version, I had a sword at the fountain, but don’t have that in the current one. Instead, I have this line in the Player class constructor:

      player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_RUSTY_SWORD), 1));

      That gives the player a starter weapon at the beginning of the game.

      You can double-check the Player and World classes here: SuperAdventure on GitHub. Let me know if that doesn’t help you. I’m going to be offline until next Tuesday, but can help you after then, if there is still a problem.

  8. Hi Scott,

    Thanks a million for the tutorial. I thoroughly enjoyed revising the basics, and watching the final product evolve. It’s always an exciting thing to watch the project compile and pop up on the screen.

    Keep up the great work!

  9. I dont know where to put this but i got an error message that says Error 2 ‘VerySimpleGame.VerySimpleUI.Dispose(bool)’: no suitable method found to override C:\Users\Peter\Documents\DoFE\C#\VerySimpleGame\VerySimpleGame\VerySimpleUI.Designer.cs 14 33 VerySimpleGame

    1. This error usually happens if you rename a form – in this case, changing SuperAdventure.cs to VerySimpleUI.cs. In Visual Studio’s solution explorer, double-click on the “VerySimpleUI.Designer.cs” file and make sure the line near the top says “partial class VerySimpleUI”, and not “partial class SuperAdventure”. Also, make sure the code behind VerySimpleUI.cs has “public partial class VerySimpleUI : Form”, and not the SuperAdventure name.

      The *.Designer.cs file contains some of the methods the *.cs form class uses. That’s why they are both “partial” classes, since they each contain “part” of the logic needed by the whole class. And you need to be sure their class names match.

      Let me know if that isn’t the problem.

  10. thanks for the tutorial! I am new in this and i want to ask how to create a boss monster (i haven’t figure out how create a monster than only appear one time then disappear after you defeat it), raise the life when the player level up and add a item shop.

    1. Hi Rodrigo,

      I’m sorry it’s taken me so long to reply. I didn’t have Internet in my new apartment until last night.

      To have a boss monster that could only be fought once, I would do this:

      1. Add a “BossMonster” property to the Location class (with a datatype of Monster).
      2. Add a “KilledBossMonster” property to the Location class (with a datatype of boolean).
      3. In the SuperAdventure.cs “MoveTo()” function, before the section where you see if there is a monster at the location, add some code to see if there is a boss monster at that location that has not been killed. If so, set the boss monster as the _currentMonster and skip over the section of code that looks for another monster at that location.

      To raise the player’s health when they are a new level, I would create an “AddExperience(int experiencePoints)” function in the Player class. Whenever the player gains experience, use that function, instead of directly adding the experience to the player’s ExperiencePoints property. Then, inside that function, you would add the points to the player’s ExperiencePoints property and increase their MaximumHitPoints if they gained a level.

      For an item shop, I would add an inventory list to the Location class. It would work similar to the Player’s inventory list, but you’d need to add an extra button column to the inventory datagridviews to let the player buy or sell items.

      Let me know if you have any other questions. I should have some time this weekend to go into more details.

      1. Hi again, yeah xD

        if (newLocation.KilledBossMonster == false)
        newLocation.BossMonster = _currentMonster;

        _currentMonster = null;

        been trying to get the boss thingy working, but i just cant make anything work, sigh…. I’ve been also trying to make some monsters only happear if the player has the quest, but it always fails.

        1. I think you probably want something like this:

          if (newLocation.KilledBossMonster == false)
          _currentMonster = newLocation.BossMonster;
          _currentMonster = null;

          This way, if the BossMonster was not killed, you will set the currentMonster to the BossMonster. Otherwise (if the BossMonster was killed), there won’t be a monster to fight (_currentMonster = null).

          You could also write it in the reverse order. Sometimes that makes if/elses a little clearer.

          if(newLocation.KilledBossMonster) // This is the same as “newLocation.KilledBossMonster == true”
          _currentMonster = null;
          _currentMonster = newLocation.BossMonster;

  11. Great tutorial, i have been building up an great idea for an RPG for quiet some time, but was waiting to learn the basics of c# in school. I am really glad i found this, it will give me a huge boost in school, and for the game. But i will probably need to watch more tutorials since the game has some really complicated stuff, cause its gonna be a sort of RPG that i have never seen done before. will take me a long time to make thats for sure. Thanks! will start learning soon probably.

  12. Great tutorial! But.. I can’t get past step 2.. because I don’t have Visual Studio and it doesn’t run on Windows Vista. Any workarounds? I’m using SharpDevelop. Lots of problems trying to follow along, and I can’t get VS without getting a new computer, which isn’t happening for a LONG time.

    1. I couldn’t find a good solution. I’m going to keep looking and see if I can find anything, but it doesn’t look like the odds are very good. If anyone reads this and has an idea, please post a comment.

  13. Hey there Scott,

    i can’t figure out how to properly add the health scaling per level. I tried the “AddExperience(int experiencePoints)” function you mentioned in another comment but I can’t get any further.
    While im adding new Items, Monster, Location and Quests, i really need to be able to raise player health at some point.
    A source code for the health increase per lvl would be awesome if that’s not too much to ask :p

    I really enjoyed your tutorial and it tought me a lot so far.

    Thank you!

  14. Hi Scott,

    i’d like to show all the movement buttons at any given time but grey out those, that can’t be used for the current location. How do I do this? Would be cool if you could help me with some specific source code :p.


    Thank you!

    1. woops, never mind, just changed .Visble to .Enabled, didn’t think it would be that easy, sorry :p

      But another thing:

      I’m trying to get the UI resized when the user is changing the window size, can’t get it to work as I want it to D:

      thanks again!

      1. Hi Sarah,

        Good job on finding the property! If you’re going to work a lot with the UI, you should take a look at the other properties and events on the different UI controls. That will make you familiar with what you can do to them.

        To resize items, you’ll want to use the “Anchor” property. That will let you say something like (for example), “no matter how big the form is, I want this button to stay 10 pixels from the right edge and bottom edge”. You may run into a problem if you want controls to stay a certain size from the center of a form. Then you’d need to do something like put a splitter control on the form, and put the other controls (buttons, datagrids, etc.) inside the splitter panels.

        There is also the form’s OnResize event. You can hook an event handler to that event. Then, when the user changes the form’s size, you can have a function that will adjust the controls. However, that can get a little complex – especially if you have a lot of controls on the form.

  15. Have been away from from C# for  several years. I am a retired truck driver. I wrote a small simple wargame about 4 years ago, VS express has changed a lot and my mind is like a sieve. Am going to try to get my feet wet with VS 2015 using your tutorial.

    wish me well, Joe

  16. This is exactly what I needed before I step into unity.  This will give a great foundation I can use in the future.  Thanks

  17. Hi,

    I followed this a little while ago and added simple levelling and then tried using progress bars to represent health and exp (which is pretty bad when moving OS enviroments. it worked fine on vista did what I expected then on windows 7 it didn’t behave the same which is a shame) unfortunately I lost all that code due to a HDD failure and have only now some years later download your source code to start again. So thank you for the work you have done it is definitely the only resource of this type available and if time permits it you should do more!!

    Now, I’ve been toying with the idea of having a map for the locations shown to you. I was thinking of drawing a few small square tiles that just have a solid border but then gaps that show the directions you can move too. Do you reckon this is possible without completely re-doing your location and movement code?

    1. Thanks. I’m currently working on a series of videos for setting up a .Net development environment, with Visual Studio Community 2015 and Windows 10. Once that’s done, I’m going to continue on with a more advanced version of the game that I started on a while back (you can see it here on GitHub).

      To add a simple graphic map to the game, I started playing around with an idea of adding an X and Y coordinate to the Location class. Then, I’d also have property to hold the name of a graphic file to use for the location (forest, house, lake, mountain, etc.). Then, I’d add a control to the UI to hold a 9 x 9 grid of these images (each one probably a 50 pixel square). The player would always be drawn in the center square, and I’d display the graphics for the nearby locations. You can see the idea I had here: http://scottlilly.com/shiny-rock-hunter-c-rpg-26-august-2014-update/. I want to do this app in WPF, so I can get more experience with doing front-end XAML work.

  18. Hello Scott,

    I have no xp in programming of any kind, however, I have decided to make my own rpg. I found your tutorial while searching for a way to do this. What all do I need make the rpg to run C# (per your tutorial) in Windows 7? Just Visual Studio? pardon me for being an absolute beginner

    1. Hello James,

      To follow this tutorial, the only tool you need is Visual Studio Express 2013. If you are using Windows 7, you need to have Service Pack 1 (for Windows 7) installed on your computer. You can get that by running “Windows Updates” from the “Control Panel”, if you haven’t already installed it (or if Windows hasn’t already done an automatic update).

      Please let me know if you have any questions while using the tutorial.

  19. Thanks so much. I was afraid there would be all kinds of things to get. I do have SP1 and forgot to mention, my system is Ultimate Edition. Now, I will acquire VSB13 and give her a test run.

  20. Hey,

    I’m really excited to try this out, but I don’t have Visual Studio Express 2013 and can’t get it as my computer (Win7) will absolutely not install Service Pack 1. It’s been like this for a while and no matter what I’ve tried, it simply won’t install. So my question is, must we use Visual Studio Express 2013 for this or would VS2010 work, too?

    1. I think everything will still work with Visual Studio 2010, as long as you have the .NET Framework 3.5 (or more recent) installed. The lessons cover the basics, which should be the same. If you do have a problem, please leave a comment and I will try to help.

  21. Thank you so much for this, currently on lesson 12. see you in lesson 19 and I hope you will make even more! thank you again ! you are awesome

    1. You’re welcome! I plan to make some more lessons and videos during the Thanksgiving holiday this week. At least one on writing unit tests for your code, and I have some ideas for more refactoring lessons for SuperAdventure (to make the code cleaner, and more like how I would write it in a real project).

  22. Hello Sir, I just finished creating this game but for some reason I cant see my weapon use button and potion use button…. And I cant see any items in my inventory all i can do at the moment is go north west east south… I cant do any quest… Please help me…

    1. Hello. You only see the weapon “use” and potion “use” buttons when you are at a location with a monster, and if you have a weapon or potion in your inventory. Try deleting the PlayerData.xml file. It should be in the SuperAdventure\SuperAdventure\bin\Debug folder (or, \bin\Release folder, if you compile the program in “release” mode). When you delete this file, the game will reset the next time you play it, and should give you a weapon to use.

      Please let me know if that does not work, and we can check other things.

      1. When I go to the location with a quest in it, I cant see a monster living there. Hence, I cant do any quest. I have no clue what the reason is for this……

        1. There aren’t any monsters at the locations with quests. When you get the quest at the alchemist hut, you need to go north to the Alchemist’s Garden location. That location has monsters. If you check the PopulateLocation() function, in the World class, you’ll see the locations with monsters.

  23. Hey again,

    Finished the game ! at least the basic of it, I really want to add more things to it but I have some space issues, is it possible to create another form that will pop up when i click a button. I want to add like inventory button and by clicking on it, it will open an inventory screen where you can equip, drop, sell your items, armor, weapons, etc.

    The problem I can’t get it to work. I don’t know how to show the form, I added a new form to SuperAdventure.

    can you please help me how to show up the form by clicking a button and from there I think I will be fine. thank you so much

  24. I managed to get the second form to show up but there’s an entire new problem.

    _player doesn’t exist in the second form. is there a way to declare it to be available to the entire program?

    1. The _player variable is a class-level variable in the SuperAdventure form, and can only be seen in the SuperAdventure class – unless you pass it to the new form. In the constructor of your new form, you can add a parameter, to pass in the _player object when you create your new form. Then, you can use the player’s inventory, quests, etc. in the new form. So, you probably have code to handle the button press that looks somthing like this:

      MyNewForm myNewForm = new MyNewForm();

      When you add a Player parameter to your MyNewForm class, you can pass the _player object by changing your code to this:

      MyNewForm myNewForm = new MyNewForm(_player);

      Let me know if there are problems getting that to work.

  25. Thank you! it does work but is there a way to update and change the text labels in the main form via the second form (by clicking on a button for example), it doesn’t let me. apart from that it works very well! I am planning to really expend the game just for practice and fun and eventually release the open source for it here so maybe more people can look at it and maybe learn  something new. but I want to add all sort of new things first!

    1. You’re welcome! I’m finishing a new lesson to show a way to update the labels when the player properties are changed from any place. I should be able to post it tomorrow, after I edit and double-check it.

  26. Sorry I ask to many questions but I am going to ask one last question.

    I’ve been looking for hours for a way to draw simple 2d sprites. I found it’s possible with XNA. Do you recommend using it? I’ve read somewhere that it is dead.

    And when I try to add the XNA libraries to the game, it gives me a warning:

    Warning    2    There was a mismatch between the processor architecture of the project being built “MSIL” and the processor architecture of the reference “Microsoft.Xna.Framework.Graphics, Version=, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86”, “x86”. This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.    RPGAdventurePlus

    How do you recommend to implement very simple 2d graphics into the game?


    1. I’ve also heard that XNA is dead. Unfortunately, I don’t do a lot of work with graphics. I did write a chess-like game that used the built-in System.Drawing library. You could probably use that, and PNG or BMP graphic files, for a simple 2-D game. That’s what I’ll probably do when I re-start working on my more advanced game engine, starting next month.

      Many C# game developers use Unity 3D to handle their graphics. It can make professional-looking games.

Leave a Reply

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