1. Learn How To Program Games – C#

This tutorial is the first in a new series aimed at absolute beginners who want to learn how to program and make games. Even if your interest isn’t games, these tutorials will give you a thorough grounding in the basics of C# programming you can apply in other areas. Later series will build on these foundational skills.

Software You Will Need

Visual Studio is great for programming in C#. It’s something known as an Integrated Development Environment (IDE). What that means is that it has everything you need to make your projects.

Go to the Microsoft Website and download the latest version of Visual Studio. At the time of writing, the latest version is 2017. You want the Community Edition, which is free.

Here are instructions from Microsoft on how to install it. Even if you are computer savvy I suggest looking at it, because you have to select which languages you want to install. When you get to the section called Workloads, I recommend installing the following:

  • .Net
  • Desktop Development with C++ (later tutorials will go into C++)
  • Game Development with Unity (for later tutorials)

You can always add Workloads later by re-opening the installer and selecting modify. But basically, the workloads download the files Visual Studio needs to understand various languages and program advanced features.

What Is An IDE

So what is an IDE? When you write code, it’s just plain text, but saved as special instructions to tell the computer what to do. On it’s own, however, a computer doesn’t know what to do with that code. You need something called a compiler to take the code you’ve written and convert it into binary, which is the native language of computers.

This is part of what the IDE does – it has a compiler in-built. So Visual Studio will compile your code, and then display your program. It does lot’s of other cool stuff too, like underline errors or try and guess what you are going to type next and offer helpful hints.

Create A New Project

Open Visual Studio now, then:

  1. Click: File > New > Project
  2. On the left pane, select Visual C#
  3. In the middle window pane, select Console App (.Net Framework)
    You won’t see the option for C# .Net apps if you didn’t install the .Net workload as instructed in the previous tutorial. If you missed it, go back to the previous tutorial and look again at the instructions.
  4. Call the project MyProgram
  5. Set the Location to your desktop or where you want it on your PC
  6. Click OK

New Project

Visual Studio will create a template document for you. This document contains some default code that C# needs to run. It looks like this:

Default Code

What Are Libraries

I’ll explain it briefly what this new code means, but we will go into more detail later. The first section of code is this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

These are the libraries we are using. Libraries are packages of code that other people have written that you can re-use, helping you do basic tasks such as displaying text and doing basic math. This allows you to focus on creating new things with their building blocks, like the old saying, “Why re-invent the wheel?”

using is a key word to say that you will be using a library. The word after using is the name of the library that you are using.

using System.Text;

The above means we are using the System.Text library.

What Are Namespaces

Next we have this:

namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

It can be broken down into sections. First let’s talk about the namespace.

Notice how the namespace has the same name as your program? Later on you can make lots of different files that can all talk to each other, and using name spaces can help with identifying which files and bits of code you want to talk to.

namespace MyProgram
{
   . . .   
}

You will also notice that after the name space is a curly brace, called the opening brace, and at the end is an opposite curly brace }, called the closing brace. The three  are where code should be but I’ve hidden it so as not to type it all out again.

Code in C# is separated into code blocks, and those code blocks are defined as the code between the opening and closing braces { }. Everything between the opening and closing braces is the namespace code block, showing what belongs to the namespace of MyProgram. 

namespace MyProgram
{  
    // code written here is part of the namespace code block
   // this code block belongs to the namespace MyProgram
}

// code out here is not in the namespace code block

You’ll get used to this stuff as we go on. Don’t worry if it’s confusing.

What Are Classes

    class Program
    {
        ...
    }

Above is the code we refer to as the class. Notice how it goes between the curly braces of the namespace, because it belongs to the MyProgram namespace:

namespace MyProgram
{
    class Program
    {
        ...
    }
}

Each class must be part of a namespace. This class is called Program, and is a special class that C# creates for you. It contains the first code that is run in any C# application.

A file full of code is often referred to as a script. Each project can have multiple scripts, and multiple classes. Think of each script as a class. The more scripts you have, the more classes you have. There’s actually more to it than that, but that will help you understand the concept for now.

Also note how the class itself has opening and closing braces { }. Anything between the opening and closing braces off the class belongs to the class.

class Program
{
    // This code belongs to the class Program
}

What Is A Function

Functions are special blocks of code that do something. The default function C# creates for you is this:

        static void Main(string[] args)
        {
        }

The name of the function is Main. We’ll talk about functions in a later tutorial, but for now just know that all of your code that does something will go into some kind of function.

This is the default function C# creates for you and is the very first function your program will run.

Notice also how the function has opening and closing { } braces, and is inside the opening and closing braces of the class. That is because the function belongs to the class, and anything between the { } braces of the function belongs to the function.

 static void Main(string[] args)
{
   // This code belongs to the function
}

Let’s Print Text

Now we’re going to print some text to the screen. Modify your code as follows:

namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is my first program!");
            Console.ReadKey();
        }
    }
}

From now on, red text means new code to add. Add the text in red to your program, exactly as shown.

Now look at the top of your screen for a button called Start. If you don’t see it, you may not have installed Visual Studio correctly, in which case, run the installer again and go back through the installation instructions. You could try and “repair” it first, or “modify” and make sure you have all of the correct packages, or otherwise uninstall and go back through the installation steps.

Start

Click that and a black window will open up with text. It should be displaying your text, saying:

This is my first program!

If you press enter, the black window will close. That black window is called the console, and that’s what a console application is. The whole program will run inside a black console window, with no graphics; all text. It’s a quick way to learn the basics of programming and gain a thorough understanding.

To explain what the new code does:

 Console.WriteLine("This is my first program!");

The first line is what prints our text. Console is a special command saying you want to do something with the console. Pretty straight forward. Then we add a . to it and use the WriteLine( ) command. This literally means we want to write whatever text is between the parentheses, the ( ) non-curly braces, on a new line.

Any text we put in quotes in the parentheses will print to the screen, but it has to be between ” ” double quotes to be recognized as text. Text is also known as a string in programming terms.

Finally we put a semi-colon at the end of the line. It literally just means this is the end of a line of code. If you didn’t put the semi-colon in, the program would look at the next line of your code and treat it as one line instead of two, which would likely give an error, either in the form of the program not running, or behaving unexpectedly. Always end a line of code with a ; semi-colon.

 Console.ReadKey();

The above line of code literally just tells the console to wait for the user to press any key on the key board – it’s just a simple way of pausing the program, although it can be used for other things as well.

Conclusion

That’s it for your first program. In the next tutorial we’ll talk about a subject called variables and how they can be used to do things like store experience points, a players score, and money in a game.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s