Bika Bika’s RPG Guide to Programming : Part 2
Links:
The Console
Soon we’re going to start writing programs. They will be a little boring, and it will be hard to see the practicality of it all for a little while.
I’ll try to show you practical examples sooner than later, but in the meantime please bear with me and keep your mind open. Most programmers
start off writing applications known as “console applications”. A “console” is a program on your computer that presents you with little more
than a black box with a place to type text into. On Windows, you can search for "" in the windows menu to open one, or hit Win+R, type in .exe
if it’s not already in the window that pops up, and hit enter. From this “console”, you can run programs and applications by typing in their file path.
For example, if a ready to run program lived in your Documents folder, you could type in C:\Users\bika\Documents\my_program.exe
and hit enter, and
this would run that program.
The console is kind of like the file explorer on your computer. At any point in time, while using the console, you are “in” a folder. To
know exactly which folder you’re in, try typing cd
and hitting enter. This will show you the “c
urrent d
irectory”. Directory is tech-speak
for folder. If you’d like to see a list of all the files and folders in the current directory, you can type in dir
and hit enter. The list
may be long, and it may be a little intimidating to look at, but it’s not all that different from viewing the list of files in your file explorer.
These things I’ve asked you to try typing in are called “commands”. The line of text that’s shown to you in the console where you can type in a command
or the name of a program to run is called the “command-line”. A console application might also be known as a command-line application. The gist
of these applications is that they don’t require or create a graphical user interface. Like dir
, you start command-line applications from
the console window and the program runs and does what it was designed to do all within the same console window.
Is this a little boring? Yeah, maybe, but you’ll find that the skills transfer over extremely well when you’re making more practical kinds of applications. Console apps are simple and give you a good mental model for how programs work. When you’re making more interesting apps in the future, you can use the things you’re familiar with from the world of console app programming as a reference point for what you want to be able to do.
As a final note, I want to mention that the console/command-line is a programmer’s friend, not their adversary. You will be forced to acquire some level of comfort with it as you get further into programming, and slowly consuming bite-sized tips and tricks on how to use it effectively is a good way to ensure you don’t end up hating it and trying to work against it (which never ends well). Don’t be scared when you’re asked to issue a few commands in your console. Sometimes things will go wrong, maybe because of a typo or because something is legitimately broken. This is always solvable, there are plenty of people online who have probably faced the issue you’re facing, and chances are if you’re reading this you can ask me directly for help.
Coding and C#
Large communities of programmers, some employed for this purpose and others doing it as nothing more than a hobby, have been kind enough to create really pleasant to work with “computer programming languages”. A programming language specifies a way for us to communicate with our computers and demand certain behaviours and resources from them. To program we must learn a language, and there are many to choose from. In this guide we’ll use C# (pronounced C Sharp). I think it’s a nice language, it was designed to be extremely well suited to programming on Windows but in recent years has become well suited to programming on other operating systems as well. It has a lot of the qualities that make up a strong modern programming language. Furthermore, it’s an extremely popular language for making video games. Toward the end of this guide, I’d like if we could try our hand at making a real playable game using this language.
We need a place to write code and run it to see what it does. I’ve written a short guide to installing the necessary tools for getting started here, so please follow those steps carefully before progressing.
Open VS Code and in the top left, select File > Open Folder. Find a place to store your first C# Project, or create a folder somewhere convenient, like your desktop or your Documents folder. Once you’ve created it, double click on it to open it and hit Select Folder to open it in VS Code.
Let’s create our first C# project within this folder. Press Ctrl + ` to open the console window within VS Code. In that window, enter the command dotnet new console
.
This will create a project in the currently opened folder, which should have a file
in it that ends with the extension .cs
, indicating that this is our C# code file.
We used the “console” template to make a C# project that has an extremely simple working program already within it. If we press Ctrl + ` to open the console
window within VS Code, we can type in dotnet run
to see the result of running this program within the console. Your screen should look something like this:
I called my project folder “First”, so if you see that text in my screenshot, it should be whatever you called your project folder in your screenshot.
Let’s talk about what’s in the code that was included in our project by default
Console.WriteLine("Hello, World!");
There’s only one line of code in this program, and it does something incredibly simple. When this program is executed, this line of code gets run by the computer in the console we execute our program in. The line says “Write the text within these parentheses and quotes to the console window”. Surely enough, this is the result of executing our program. You can change the text within the quotes to see the program output different things when we run it.
This line of code is a “statement”. A statement is essentially a command we give to the computer from within our code. Statements always end with
a semi-colon, ;
, and it’s important to keep this in mind. Your text editor will usually give you a hand if it detects you’re missing one, but the resulting
error messages can be confusing and off-putting.
Let’s explore another kind of statement, one that doesn’t print to the console window. One thing computers are great at is remembering the things you tell it to remember. We should quickly learn how to take advantage of the computer’s great memory. The way we begin doing this is by creating what’s known as a variable. A variable consists of a name for some piece of information, and the actual information it refers to. There is a real world analogue to these. Not the type you might have heard about in a math class or a science class (although they share the name for a reason), but rather the analogue I’m talking about is when we refer to things indirectly. “Bika’s birthday” can be considered a name for some piece of information. If we wrote down “Bika’s birthday: 03/30/2002” somewhere, this would be incredibly similar to how storing information in variables works in computer programs. Let’s see an example to solidify this:
var playerName = "Sakamoto";
We use the keyword “var” to communicate to the computer that we’re creating a new variable. We name it “playerName” and use the equals sign to indicate that we’re going to associate the information that follows with the name that we gave this variable. That information is, obviously, the name of the player in our made-up RPG scenario. Of course, nothing was stopping us from naming the variable something obscure and meaningless like “a” or “njk345bl”, but naming your variables in a way that describes the information they’re associated with is a smart idea. Notice that creating a variable is a statement, so we used a semicolon at the end.
Now that we’ve created a variable, we can use it later in our program, for example when we want to print things out:
var playerName = "Bika";
Console.WriteLine("Hello, " + playerName + "!");
If we issue the command dotnet run
again, this time our program will print out a greeting to the player specifically.
One kind of thing that shows up when we’re writing programs besides statements are “expressions”. Expressions are pieces of code that are one of two things:
- Information, otherwise known as “data”, in its purest form, e.g. text like “Sakamoto” or a number like 42
- Something that can be “evaluated” or “simplified” into pure information, e.g. “Hello, " + “Sakamoto” simplifies to “Hello, Sakamoto”,
and
playerName
simplifies to “Sakamoto” because we defined the variableplayerName
that way. Similarly,"Hello, " + playerName
simplifies to “Hello, Sakamoto”, and is also an expression.
If you look back at our code examples, you’ll see that expressions show up exclusively between parentheses or on the right-hand side of a variable definition. Expressions are often parts of statements, and anywhere you could’ve had some kind of pure information like text between quotes, you can substitute an expression that simplifies to pure information. This is why the third line of our program works. Similarly, we can create a new variable that uses an existing variable in its definition:
Console.WriteLine("Hello, World!");
var playerName = "Sakamoto";
var playerLastName = "el Gato";
var playerFullName = myName + " " + playerLastName;
Console.WriteLine("Hello, " + playerFullName + "!");
If you run this program, it should print out “Hello, Sakamoto el Gato!”.
We can store data that isn’t text as well; C# supports a number of “data types” by default, and has specialized names for them which are also keywords.
int
: Integers, i.e. whole numbers which may be positive or negativefloat
: “Floating point” numbers, i.e. numbers which may have decimal values such as1.12
double
: “Double-precision floating point” numbers. These are just likefloat
s, but they are generally more precise, so we will always use them instead.bool
: “Booleans”, which are values that can only be “True” or “False” and have no more to them. Consider, e.g.var hasLastName = true;
.char
: Characters, such as ‘a’, ‘b’, ‘c’, ‘1’, ‘2’, ‘.’, ‘,’, etc.string
: Text, which we’ve seen a lot of so far
When we create a variable in C#, we have the option of asking the computer to simply figure out what type of data our variable contains based on what we define it as.
For example, assigning “Bika” to a variable allows the computer to figure out that the variable contains a “string” value. If I wrote, instead, var myName = 'b';
,
it would instead believe the variable myName
contained a “char” value. To reduce this ambiguity, we can choose to be specific about the type of variable we’re
creating:
Console.WriteLine("Hello, World!");
string playerName = "Sakamoto";
string playerLastName = "el Gato";
string playerFullName = myName + " " + playerLastName;
Console.WriteLine("Hello, " + playerFullName + "!");
This doesn’t change the functionality of the program at all, it just skips the step of having the computer figure out what type of variables myName and myFullName are. We usually don’t need to bother with this, though.
Let’s see some examples of creating different types of variables and see what kinds of expressions we can make with them:
// Strings
var playerName = "Sakamoto";
string playerLastName = "el Gato";
// Chars
var playerFirstInitial = 'S';
char playerLastInitial = 'G';
// Bools
var playerIsAlive = true;
bool playerIsHuman = false;
// Ints
var playerLevel = 10;
int playerExperience = 1000;
// Doubles
var playerHealth = 12.3;
double playerMaxHealth = 50.0;
Console.WriteLine("Player Profile:");
Console.WriteLine("Name: " + playerName + " " + playerLastName);
Console.WriteLine("Initials: " + playerFirstInitial + "." + playerLastInitial + ".");
Console.WriteLine("Level: " + playerLevel);
Console.WriteLine("Exp: " + playerExperience);
Console.WriteLine("Health: " + (playerHealth / playerMaxHealth * 100) + "%");
In this example, we didn’t get to use our boolean variables or our char variables, but we’ll cover that a little later. Note that almost any value can be “added” to a string.
That’s what we’re doing when we write, e.g. "Level: " + playerLevel
. The text in quotes, "Level: "
, is an expression of type string. playerLevel
is
an expression of type int
, but because C# knows how to combine text with many kinds of non-text data, this works out just fine. If we wanted to do the opposite,
that is, add a string to an int, C# wouldn’t know what to do.
Unsurprisingly, you can do simple math on numerical types like int
and double
. I don’t think that’s worth discussing too much. The order of operations that you
learn in math class still holds, but if you don’t remember it you can always just use parentheses!
So far, all the variables we’ve stored have been given values directly in the code. In other words, we have the option to not use variables at all, and just substitute the use of each variable with the data they’re associated with. The reasons to use variables in the first place becomes more obvious as we write more “dynamic” programs. Let’s look at one way to make our program more dynamic: taking user input. In some crude sense of the word, this is a game after all, so the player should have some control over what happens as the program is running. Let’s allow the player to choose their own player name.
Similar to how we write Console.WriteLine
(which has been left somewhat unexplained, since that will be covered more completely later on) to output text to the console,
we can write Console.ReadLine
to receive input from the console. It works like this: Console.ReadLine()
is an expression that simplifies to a string, the text
that the user entered in the console window while the program was running at this point. When you run your program and reach the line that uses Console.ReadLine()
,
the program will stop executing until the user types something in and hits enter, then the program continues. Let’s see how it works:
// Strings
var playerName = Console.ReadLine();
string playerLastName = "el Gato";
// Chars
var playerFirstInitial = 'S';
char playerLastInitial = 'G';
// Bools
var playerIsAlive = true;
bool playerIsHuman = false;
// Ints
var playerLevel = 10;
int playerExperience = 1000;
// Floats
var playerHealth = 12.3;
double playerMaxHealth = 50.0;
Console.WriteLine("Player Profile:");
Console.WriteLine("Name: " + playerName + " " + playerLastName);
Console.WriteLine("Level: " + playerLevel);
Console.WriteLine("Exp: " + playerExperience);
Console.WriteLine("Health: " + (playerHealth / playerMaxHealth * 100) + "%");
The program works almost exactly the same as before. The difference is that as soon as the program runs, you must type in the player’s name and hit enter
for the rest of the program to continue. Once you do, the program uses the name you entered as the value of playerName
as expected.
The programs we write from now on will sit on the foundation of Console.WriteLine
and Console.ReadLine
. Most programs need some way to display information
to the user who ran the program and receive input from that same user. In the world of console applications, this is exactly how that’s done.
We can abuse Console.ReadLine()
in a way that can make our programs a little more interesting. Note that all our Console.WriteLine
lines execute immediately
one after the other. What if we wanted to enforce a kind of pause between them, if, for example, we wanted to show the user dialogue between characters. We can do
the following:
// Strings
var playerName = Console.ReadLine();
string playerLastName = "el Gato";
// Chars
var playerFirstInitial = 'S';
char playerLastInitial = 'G';
// Bools
var playerIsAlive = true;
bool playerIsHuman = false;
// Ints
var playerLevel = 10;
int playerExperience = 1000;
// Floats
var playerHealth = 12.3;
double playerMaxHealth = 50.0;
Console.WriteLine("Player Profile:");
Console.WriteLine("Name: " + playerName);
Console.ReadLine();
Console.WriteLine("Level: " + playerLevel);
Console.ReadLine();
Console.WriteLine("Exp: " + playerExperience);
Console.ReadLine();
Console.WriteLine("Health: " + (playerHealth / playerMaxHealth * 100) + "%");
This way, the user has to press enter to see the next line. Note, however, that this forces a line of space between each printed line.
Summary
In this post, we began programming. We discussed the console, where we issue commands to get our computer to do certain things and where we see our console applications in action. We discussed the C# language, which is a programming language, and we began learning how to use it. We learned how to execute our programs, how to print to the screen, how to store data of various types in variables, and about statements and expressions. Finally, we learned how to obtain input from the user in the console window.
If you feel like some of this stuff flew over your head, that’s okay. You can always ask questions, but if you want to be sure you understand all that’s necessary to move on to the next post, make sure you can complete the exercise that follows.
Exercise
Remove all the lines we wrote for our first program, and prepare to write a new one. In this program, we’ll show the player some dialogue, but the player will choose their name and the name of the person they’re speaking to. Between each line of dialogue, the player should have to press enter to see the next line. Feel free to look at the program we wrote above as reference while writing this program, and feel free to be as creative as you’d like to be.