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.

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.

 

One of the awesome students here created a PDF document of the lessons. You can get a copy of it here. You can also download the source code files for the lessons 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 Community 2017

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

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

 

Share your personal, expanded versions of SuperAdventure here!

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

  1. Unfortunately I got to this point and it all went wrong.

    Cutting and pasting the code from Git caused unrecoverable code errors in the generated form code. I had an error message saying that I had not created a method for rtbLocation_TextChanged.

    I could not find a way past this and it is either a choice of giving up or starting all over. Not a fan of starting all over. 🙁

  2. Hi Scott,

    I just wanted to say immensely grateful I am that you have created and shared this walkthrough/guide. I am not finished yet, but I am rolling along with it. I have wanted to learn computer programming for some time now. Your book has really inspired me to do it. Are there any other guides you’ve made like this one? Programming other various games to further learn C#?

    Thank you for reading,
    Dan

    1. Thank you Dan,

      I don’t have PDF versions for anything else. But, I do have lessons on C# Design Patterns (general solutions to common programming issues), a WPF version of the SuperAdventure game (that uses better programming practices than the Windows Form lessons), and just started a new “refactoring” series where I will be improving the code quality of the Windows Form SuperAdventure game – and describing how/why the changes will improve SuperAdventure’s quality.

  3. Semi-Novice here but… I was able to add Armor items that add additional defense during battles (including a function to ensure that the defense amount is never less than zero resulting in negative damage being done):

    Armor.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Engine
    {
    public class Armor : Item
    {
    public int MaximumDefense { get; set; }

    public Armor(int id, string name, string namePlural, int maximumDefense) : base(id, name, namePlural)
    {
    MaximumDefense = maximumDefense;
    }
    }
    }

    —–

    Example items:
    Items.Add(new Armor(ITEM_ID_SHIELD, “Shield”, “Basic Shield”, 4));
    Items.Add(new Armor(ITEM_ID_LEATHER_CHEST, “Leather Chest”, “Basic Leather Chestpiece”, 8));

    —–

    damageToPlayer int calculation:
    int damageToPlayer = CheckIfLessThanZero((RandomNumberGenerator.NumberBetween(0, _currentMonster.MaximumDamage) – RandomNumberGenerator.NumberBetween(0, defParse)));

    —–

    Zero check:
    private int CheckIfLessThanZero (int input)
    {
    if (input < 0)
    {
    return 0;
    }
    else
    {
    return input;
    }
    }

    —–

    Happy to share the code with everyone or even work with anyone to make this a little better than a novice could add.

    1. Thanks for sharing Chris!

      In the damageToPlayer calculation, is “defParse” from the MaximumDefense property of the player’s armor?

      There’s also a cool trick you can do with the CheckIfLessThanZero function. There is a Math.Max function that will accept a list of integers and return the largest value. So, the function below will return 0 when “input” is less than 0. Otherwise, it returns “input”.
      private int CheckIfLessThanZero(int input)
      {
      return Math.Max(0, input);
      }

      1. Yes it’s from the MaximumDefense property in the armor, parsing into an int for the function (I may have overthought that a little bit and there is likely an easier way to get the value.)

        Will update the Math.Max function. A lot easier to code that way.

  4. When I got to lesson 11.1 and copy/paste the World.cs static class into my game I got 18 errors that were all similar to this:

    CS1061 ‘Quest’ does not contain a definition for ‘RewardItem’ and no accessible extension method ‘RewardItem’ accepting a first argument of type ‘Quest’ could be found (are you missing a using directive or an assembly reference?)

    Is this expected?

    1. That error message is saying the Quest class does not have a RewardItem property.

      Check your program’s code with the code in Lesson 10.1 – especially the code for the Quest class in step 4. Make sure the RewardItem property exists, its datatype is Item, and it is public.

      If that doesn’t fix the error, 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?

        1. The errors are all related to name differences.

          In the Quest class, the property “RewardItem” is typed as “Reward Iten” (an “n”, at the end, instead of an “m”). In the Location class, the “LocationTo” properties are named “LocationToThe” (with an additional “The”).

          That is a common reasons for this error message. It also happens if a property is not public, and you try to reference the property from a different project – the default is “internal” (only visible inside the same project), if you don’t explicitly add “public”, “private”, or “protected”.

          Let me know if that doesn’t eliminate the errors, or if you have any questions about the solution.

Leave a Reply

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