
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.
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: If you already know the basics of C# programming (classes, properties, functions, “if” statements, etc.), you might want to look at the newer “Build a C#/WPF RPG” lessons. The code in those lessons is more like how I would write a “real” professional program – using better design and architecture.
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 Community Edition
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 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
Lesson 26.1 Displaying a World Map
Lesson 26.2 – Hiding Unvisited Locations on the World Map
Bug Fixes
Lesson 99.1 – Preventing duplicate quests
Lesson 99.2 – Setting CurrentWeapon when the player has multiple weapons
Hey Scott!,
I added some extra quests, but when i put 2 on the same spot it only showed the new one (i added them only by creating it in world and adding it to the location)
Hello!
To do this, you’d need to start by change the Location QuestAvailableHere property’s datatype from “Quest” to “List” (and probably rename it to “QuestsAvailableHere”, to make it clear it can hold multiple quests).
Then, in the World PopulateLocation function, instead of saying “farmhouse.QuestAvailableHere = QuestByID(QUEST_ID_CLEAR_FARMERS_FIELD);”, you’d say “farmhouse.QuestsAvailableHere.Add(QuestByID(QUEST_ID_CLEAR_FARMERS_FIELD));”. You could add as many Quest objects to that list as you want.
Finally, in the movement code (when the player moves to a new location), you’d need to loop through the list of Quest objects to see if the player should receive the Quest, or if they can complete an existing quest.
Let me know if that wasn’t clear.
WOW! amazing tutorial, this helped me so much to learn more about programming with C#. I would LOVE you to expand on a few things with tutorials if you have the time though. I’m pretty sure it would be great tutorials for others to learn as extras for their games also.
1. A tutorial for an Advanced Bag system, something similar to how you did the World Map but instead for a bag holding all the items you have collected and amounts you have. So, push a button and another screen opens with all your bag items and amounts in. It would use icon pics for the item and not text for item name and then the amount maybe in one of the corners of the picture. The picture could be grayed out until you have collected it maybe?
2. Maybe add a picture box to the main SuperAdventure design view window that shows a single picture of the location you are currently in when you enter a new area.
3. Maybe in the text information window have options.. for example the text says.. “you have entered a house, there are two people here” then it gives option to say “talk to person 1 or talk to person 2”. The ‘person 1’ and ‘person 2’ could maybe be different colors and cickable for choosing option to talk to them. Or maybe that’s already available and you could give a tutorial on how to do that?
In any case, thanks again for these awesome tutorials, ive learned a lot to this point from these.
You’re welcome, Sylar.
I’m not doing much more with these lessons, but am doing a new version in WPF at https://www.scottlilly.com/build-a-cwpf-rpg/.
There is a bit more graphics in the WPF version, showing a picture of the location you’re at and the monster you’re fighting. Adding an inventory bag would be a cool feature to add. Adding conversations with NPCs would be another cool feature. I’ll put your suggestions on my notes for new lessons to add to the WPF course. There’s some more cleanup I want to do with the code in those lessons, once I finish a business project that’s been using up more of my time than I wish it was.
why when i putting Location at LocationToWest or other than can’t be read and been problem
I’m not sure what the proglem is. Can you upload your solution (including the directories under it, and all the files in those directories) to GitHub or Dropbox, so I can look at it?
Hi Scott!
I could really use your help, as I’m in chapter 16.3 and for some reason I constantly get errors for rtbMessages and it wouldn’t run my code.
I tried looking at from every angle, but I just can’t figure it out!
I’ve uploaded it to https://github.com/bence0307/RPG-Game
Could you please tell me what I did wrong and where? What I need to fix this?
Thank you very much for your help!
Kind Regards
Bence
Fresh C# student.
Hi Bence,
Can you please upload your solution file (the .sln file)? I’m having trouble building with only the .csproj files.
Sure thing gimme a couple of minutes.
Hi Scott,
I updated my GitHub, adding the solution .sln file to the list and a few more files, just in case.
Thank you for your help.
Kind Regards
Bence
Hi Bence,
I found two problems. The main one was in the MoveTo() function. One of the closing parentheses was in the wrong place. So, if the location did not have a quest, the program also tried running the code to give the player the quest at the location – cause a null reference exception. Also, the Engine project was .NET Standard, and will need to be .NET Framework. This might cause problems when you try to add the images for the map. So, I created a .NET Framework version of the project in the fixed solution.
You can get a fixed version here: https://www.dropbox.com/sh/15uzvauhl6isslj/AAAM_vuF_GHTxMm47VGkL8hta?dl=0 This version does not have the music or background image, since I didn’t see those files in GitHub.
Let me know if you try this and have any problems or questions.
Hi Scott,
Thank you very much. Thanks to the solution you provided I was able to see where my error was, and my teacher today helped me with the music issue I had.
For some reason before I was able to play the music by just calling the title it, but Today I’ve added the location as well and that helped it to reach it and play it as well.
So thanks to you, everything is working superbly now.
I only have one more question.
In the World class when we call on the rat variable:
“Monster rat = new Monster(MONSTER_ID_RAT, “Rat”, 5, 3, 10, 3, 3);”
Just to make sure I understand everything correctly (as I would like to expand it, once I completed your tutorial.)
When we open the parenthesis we give the first 4 details based on the Monster class, and the last three comes from the LootItem class that comes from the Item class.
I know that I must not understand something from my own, and that may be coming from that I’m not a mother tongue English speaker and sometimes that results in some errors and misunderstanding from my part.
But I don’t understand that the chance where is coming from, I’m guessing it’s either the last ‘3’ or the ‘3’ beforehand.
The thing I don’t get that how does this gets the data that is being referred to.
Thanks again 🙂
Bence
Hi Bence,
You’re welcome. This is a good demonstration of why it’s best to make functions small, without too many levels of parentheses. We do improve this code in later lessons, which makes it easier to read and decreases the chance of errors.
For the rat variable, when you instantiate an object (use “new” on a class), the parameters in the parentheses match the parameters in the constructor. So, those values are passed into these parameters:
public Monster(int id, string name, int maximumDamage, int rewardExperiencePoints, int rewardGold, int currentHitPoints, int maximumHitPoints)
The rat’s possible loot items, and their chance to appear, are in the next two lines in World.cs:
rat.LootTable.Add(new LootItem(ItemByID(ITEM_ID_RAT_TAIL), 75, false));
rat.LootTable.Add(new LootItem(ItemByID(ITEM_ID_PIECE_OF_FUR), 75, true));
If you look at the constructor for the LootItem class, the second parameter is the dropPercentage value (75% for these two lines).
Let me know if that is not clear, or if you have any other questions.
Thank you Scott, I understand it now!
I think the problem was that I got a bit confused that where does the rat get its function the parenthesis.
I’ll continue the tutorial and if I have other questions I’ll let you know. 🙂
Thanks Again.
Bence
Hi Scott!
Sorry to bother you but I have ran into some issues which I don’t quite understand.
Currently I’m on chapter 20.3-20.4 and I have some issues in the SuperAdventure.cs.
I get errors for the .PropertyName all of the .Any-‘s, and lastly it doesn’t understand the _player.PropertyChanged += PlayerOnPropertyChanged; at the Potions.
I’ve uploaded my current solution to GitHub.
If you have the time could you please check it for me?
What am I missing? Why doesn’t it work? What do I need to change?
https://github.com/bence0307/RPG-Game/blob/master/RPG.rar
Thank you again for your help 🙂
Kind Regards
Bence
Hi Bence,
I found a few problems in SuperAdventure.cs. I uploaded a version that should be fixed to: https://www.dropbox.com/sh/15uzvauhl6isslj/AAAM_vuF_GHTxMm47VGkL8hta?dl=0
The problems were:
1. “using System.Linq;” and “using System.ComponentModel;” needed to be added to the top of the file
2. Missing a closing curly-brace on line 512
3. In the PlayerOnPropertyChanged function, the datatype of second parameter needs to be PropertyChangedEventArgs
4. Lines 223 and 336 have typos in the function name “AddExperiencePoints”
Let me know if that doesn’t fix the problems, or if you have questions.
Hi Scott,
I am brand new at learning c#. I am working through Mosh’s beginner course. This question feels like it should be obvious but what workload should I be using to create an RPG if I am using Visual Studio 2019 and why? So far I have only worked in Console App(.Net Core).
I see that you said to use the Windows Form Application but there are two workloads like this to choose from. They are .Net Framework and .Net Core.
Thank you!
Hi Alyssa,
You can use this course with Visual Studio 2019. Just make sure you select the “.NET Framework” projects – for both the UI and Engine class library projects. If you select “.NET Core”, or “.NET Standard”, there are a couple things that won’t work with this source code.
Hey there Scott,
I’m interested in learning some C# and getting familiar with the language but don’t want to dive into the .NET environment right now with all of the ASP.NET stuff etc
Would this still be current enough to get going?
Hi Jason,
What are you worried about with ASP.NET? Microsoft is switching to .NET 5 around the end of this year, which is based more off of .NET Core than .NET Framework. This course is several years old, and uses .NET Framework and Windows Forms. There is a newer version of the course that uses .NET Framework and WPF at https://www.scottlilly.com/build-a-cwpf-rpg/.
If you want to learn .NET Core, you might look at courses by Tim Corey https://www.youtube.com/user/IAmTimCorey. He has more videos on .NET Core and some on Blazor (a new UI technology).
But, if you take this course, you should still get a good knowledge of C#, which is mostly the same in the .NET Core version of C#.
Hi Scott,
Sorry to bother you at this time of hour. I could really use your help. My rtbMessages does not show any message when I click the movement button.
I’ve upload to https://github.com/Nelson2306/RPG
Could you please explain for me what is the problem?
Thank you for your kind help.
Regards,
Nelson.
Hi Nelson.
I don’t see the files for the SuperAdventureConsole project in GitHub. Can you check if you can upload them? If there are problems doing that in GitHub, you can post the files to a file-sharing site (like Dropbox).
lesson 16 ( all of them ) give an error when i try to open them.
Thanks for letting me know.
I didn’t see any obvious problem with those pages, but will work on them after I finish my client work for the day. There isn’t anything tricky about those pages, so I hope it won’t take long to fix.
Hi I was wondering where to find the link to these files on github?, and I was also wondering how to go about adding different character classes in this text adventure, or is it possible, thank you much for your time.
Hi Jon,
If the lesson has code on GitHub, there will be a link at the bottom of the lesson saying “Source code on GitHub” or “Source code on Dropbox”. If you want to get the final source code for the completed project, it’s available on GitHub at: https://github.com/ScottLilly/SuperAdventure.
These lessons don’t use a character class to do anything different, but you could create a new “CharacterClass” property on the Player class and use that in the rest of the code – maybe add spells that can only be used by wizards.
There is a WPF version I’m actively working on at: https://www.scottlilly.com/build-a-cwpf-rpg/. I’m about to expand the Player class in those lessons to include more attributes (strength, wisdom, etc.) and character class.
Hello! I have a quick question, and it is going to sound silly. Honestly, you probably referenced it somewhere in the instructions, but I cannot find it.
I see where we mapped out the locations in pseudocode.. and I am aware that we store our current location as a variable in the code. However, where did we create the “map” of the world within the program? I guess, what I am asking is, how does the code know which locations are “to the East of our current location”, etc.
I hope that made sense.
Never mind, I figured it out! (I see them linked within World.cs)
No problem! It’s good that you were able to figure it out. That’s a definite sign of starting to really understand programming.
Thank you so so much for creating this. I’ve really been struggling in my C# class at my university and just the first few lessons helped me actually understand concepts that I was struggling to understand in 12 weeks of reading the very dry textbook. I have to take intermediate C# next term and I know that completing this game with give me a huge head start. Thank you!
You’re welcome, Rob! I’m glad the lessons helped
Hi Scott, thanks for this tutorial – as a beginner, learned a lot seeing a game built start to finish. I’ve written all code for base game (thru Ch 16) with no errors. But when I “Start” the game from VS the UI is off. Some of the direction buttons are missing and text like “Gold” is smushed together with the corresponding value.
Is there any obvious reason why this could be?
In short, SuperAdventure.cs [Design] looks perfect in Visual Studio but the UI in “Start” runtime looks different and has issues describe above. I don’t see any other users with UI issues. Happy to post the code if that’s easier.
I’m using VS Community 2017 4.8 on Windows 10
(Boot Camp partition on MacBook Pro)
Hi! I can’t think of an obvious source for that problem. Can you upload the code for the whole solution to GitHub, or some other file-sharing location, so I can look at it?
Here you go! https://github.com/SirMullich13/SimpleRPG1
Thanks for taking a look.
For the buttons, it looks like the problem is that they aren’t “connected” to their functions in SuperAdventure.cs. There are “eventhandlers” that tell the program what function to run when an “event” happens – like the user clicks on a button. One way to create eventhandlers for buttons is to double-click on them in the designer screen. If you do that, Visual Studio will create the eventhandler and an empty function in SuperAdventure.cs. However, since we already have the functions in SuperAdventure.cs, we need to do something different. You can learn more about eventhandlers in lesson 21.3.
To connect the buttons, edit the SuperAdventure form in design mode. Click on each button once (be careful to not double-click), and try these steps:
1. In the Properties section (lower-right of Visual Studio), click the lightning bolt (see the image below for an example). This will show you the events for the button.
2. Find the “Click” event and click on the box to its right. This should show you a dropdown of available event functions for buttons.
3. Choose the appropriate function for each button: “btnNorth_Click” for the “North” button, “btnUseWeapon_Click” for the weapon’s “Use” button, etc.
That should let you move North when you start the game. When you move to a new location, you should see different direction buttons available. When you get to a location with a monster, then you should see the “Use” buttons.
I think the smushed controls are probably because the SuperAdventure form had AutoScaling set to “Font”. Try editing SuperAdventure.Designer.cs. Search for “AutoScaleMode” and change the line to this:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
You might need to move and resize the controls once, to get them the way you want. But, this might fix the problem for the future. Windows Forms had some strange behavior with auto-scaling and determining DPI (dots per inch).
Let me know if that doesn’t work, or if you have any questions.
Thanks Scott that worked! I owe you a coffee. My UI buttons did not have event functions assigned. Changing AutoScaleMode from “Font” to “None” completely fixed the UI with no rework required. Hopefully this helps others.
Unrelated question – I’m thinking of going straight from Ch 17 to Ch 22. Is that feasible or is 19.4 (Saving/Loading with XML) a pre-req for SQL Server to work in Ch 22?
You’re welcome!
You probably could go from lesson 17 to lesson 22, if you just wanted to learn about SQL. There are a few bug fix lessons you’d miss. But, if you’re mostly interested in learning programming, instead of building a game to play, that would be OK.
I think the only issue you’d run into is in Step 3 of Lesson 22.3. Just leave out the lines that try to read (or save) the saved game from an XML file, and that “should” work. Let me know if you do that and encounter any problems.
Hey Scott!
Would it be regardless a problem if I use this as base for my developed game?
As you would expect you would claim credit.
I wouldn’t ask if I exactly wanted to make it for me but, im thinking about commercializing. More of the game would be made by my team graphic quest map, etc. Say what you think.
Hi Igor!
Yes, it is OK to use this as a base for your game. If you look at the code on GitHub (https://github.com/ScottLilly/SuperAdventure), it uses the MIT license. The only thing you need to do is include a copy of the MIT license when you release your game. You might want to look at the newer version that uses WPF, instead of WinForms. It is at: https://github.com/ScottLilly/SOSCSRPG and also uses the MIT license.
Good luck with your game!
Hi Scott,
I am new to programming and this tutorial has helped greatly to learn about object oriented programming.
A couple of questions:
Why does my code require the lines:
private void SuperAdventure_Load(object sender, EventArgs e)
{
}
If I don’t include these lines, I get a CS1061 code. Finally, nothing happens when I click the “North” button… Is there any way I can send my code for some debugging help?
Hi Denis,
It sounds like you have an “eventhandler” setup for the form load. This could happen if you accidentally double-clicked on the form, while in design mode. If someone does that, Visual Studio “helps” by creating an eventhandler and function. It sound like the “North” button is missing its eventhandler that tells the program what function to run when someone clicks on the “North” button.
You can learn more about eventhandlers in lesson 21.3. If you delete the form’s “Load” eventhandler in SuperAdventure.Designer.cs, and add in the one for the North button, that should fix the problem.
If you try that, and it doesn’t work, can you upload your solution (including the directories under it, and all the files in those directories) to GitHub or Dropbox, so I can look at it? I might not be able to look at it right away. I’m in Texas and we’re having electricity problems, due to the weather. I just got electricity again, after a day and a half without it. 🙁
Scott,
Thanks for the reply! That worked great and the program is working as expected! Sorry to hear about the weather issues in Texas, hoping it’ll all get better for you guys down there soon.
You’re welcome! The electricity just came back on. Hopefuly it will stay running.
I was having such a good time following along, and even made a few nice changes and edits, added a few things and was doing good…. until 20.4 where we suddenly just have to copy+paste over the 2 biggest files in the solution…. Man, that sucks. I would have rather you left that part up to us, because I skipped several sections (like LINQ and the XML) and my code was no longer compatible with the base….
I’m gonna still continue, but man, every unique part that I added from my project just got wiped. #feelsbadman
I’m sorry about that Yohoki. Originally, this was only going to go to lesson 18. But people kept asking for more features, and the original code couldn’t really handle them well. Unfortunately, I don’t know a good way to show a lot of small changes.
Couldn’t seem to get the XML File to get made on closing the form. I’m not sure why it’s not working. Couldn’t even get a MessageBox.Show to work inside the FormClosing method. Here’s what I had to do to get it to create the XML:
Change-
private void SuperAdventure_FormClosing(object sender, FormClosingEventArgs e)
{…}
to-
protected override void OnFormClosing(FormClosingEventArgs e)
{…}
Hi Yohoki,
If you want me to look at your code, and let you know why that happened, can you upload your solution (including the directories under it, and all the files in those directories) to GitHub, Dropbox, or some other file-sharing location?
Sure thing! I’ve already been uploading it to my github as I’ve been going along. LINK REMOVED FOR PRIVACY
At the moment, it’s more or less exactly as the tutorial files show, but I’ll be editing it more and more later on. As far as I can tell, it SHOULD work the way you put it, and other people in forums have said to do it exactly that was as well. This was just one solution that did actually work for some reason. I had even added a MessageBox.Show(“Test”,”Test”); inside the method and it would not appear.
Hi Yohoki,
I found the source of the problem. The OnFormClosing function did not have an eventhandler. The eventhandler tells the program what function to run when some “event” happens (in this case, closing the form). You can read more about eventhandlers in lesson 21.3.
The way to identify this type of problem is to look above the function in Visual Studio. It should show you a count of the references – the other places in the program that use this function/property/class.
The OnFormClosing function has 0 references, but the btnEast_Click function has one reference.


To fix this, you can add the line below inside the InitializeComponent() function in SuperAdventure.Designer.cs (click on the triangle to the left of the SuperAdventure.cs form in Solution Explorer to see its Designer page). This will connect the form closing event with the function.
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.OnFormClosing);
Let me know if that does not work, or if you have any questions.
Hi, I want learn C#, but i’d like to use other editor, for example Visual Studio Code. I can use this editor to be able to program.
Thanks
Hello!
Yes, you should be able to use Visual Studio Code or other editors like JetBrains Rider. But, you do have to write this on a Windows computer. Windows Forms programs only work on Windows computers.