Other Related articles
- Huo Chess Main Page
- How to Develop a Chess program for Beginners Series. Click here for the second lesson. Click here for the third lesson.
- Java chess programming ultra-fast tutorial: “How to code a chess program in one day. (C# and Java examples)“
- Programming for kids: Developing a chess program in BASIC
- HUO Writer: A simple AI writer with basic Neural Network capabilities (QB64)
- How to develop a chess program from scratch (for total beginners) Ep. 1
Is programming a game difficult?
Don’t be fooled by the title. Yes it is!
However learning the basic principles of programming and making a very simple game is… simple! And the goal of this article is to show how one can learn programming while having fun in just one day.
Yes, you read that right. One day. Do not expect (of course) to be experts. This goes without saying. But you will learn how to create your first program, which will actually do something. This is how I started programming myself. I had bought (my parents actually) a Commodore 16. And there were no games with it. Just a big manual. In which there were the source code of some games I had to type myself into the computer in order to play them! At first I did not know what I typed. But I typed it anyway. And the game was fun.
Next day, I started learning programming.
Because you see, in order to learn something you must LIKE it first! There is absolutely no point in learning programming just by reading big books with incoherent programming exercises which will at the end result in nothing more than code snippets (parts of code) which do nothing important or useful whatsoever. This is how today’s kids learn to hate programming.
The game we will develop is very small and easy: Just an old-times ultra mini text-adventure. Meaning a game with no graphics, which asks for the users to input text commands (e.g. “Open door”) in order to play it.
Well, it is!
And most importantly, it is scalable! You can – at your own pace – make it better, more complex, or even add graphics (there will be a lesson for that too). And make it your own super game that will conquer the world! Or at least your friends and parents. 🙂
No more talk. Let us begin!
Setting up the tools
Download Visual Studio from Microsoft. There is a free edition to download. Install it. Figure out how to do that on your own, it is not difficult really – just like installing any other program, just select for now the default configuration suggested by the program.
What you will need is C# to be installed – because there are other languages also supported by Visual Studio (C++, Visual Basic, F# et cetera).
Create the project
Open Visual studio and create a new C# Console Application project.
When first creating the program, Visual Studio will create a lot of code automatically. Your project will look something like the schema below.
This code is the shell of your program. Do not worry about what it does for now. What you need to remember is that the program has a Main class, inside which you should put your code (see the schema above).
What is the scenario of your adventure?
Let us keep it simple: You are a person who have lost his consciousness and is suddenly found inside a locked room. You want to get out. As simple as that. Your goal is to search the room for the key and unlock the outer door!
What will be the solution of the problem?
Again, let us keep it as simple as possible: You search the room. You see a box. You open the box. There is a key inside it. You take the key. You use the key on the door. The door opens. You get out.
It may sound simple – and it is – but this is the basis of a simple game. Remember! You will have time to improve it later on! The point here is to learn the very basics of programming.
Open the newly created project. Now you need to add the code to actually make your game. We will do this by adding the minimum lines of code to make the game. As said above, you will then be able to add additional code and improve the game. What we will show here is the basic principles.
The game has no graphics, so all we need is a way to get user input through text and then present to the user again the results of his action via text.
For a short description of the basic elements of a program (variables, functions etc) check the Huo chess tutorial here.
In short, what we will need is the following programming elements:
Variables: These are elements of different types which hold values. A variable can be an integer variable (which may hold values like 1, 4, 10), a String variable (which may hold values like “key”, “door” etc) or a variable of many other types. We will use the following variables:
- A variable to hold the command the user has entered. We will call this user_command.
- A variable to know whether the user has the key or not in his pocket. We will call the variable user_pocket.
- A variable to determine whether the game must end. We will call this exit_game.
Input/ Output commands: These are the commands we will use to get input from the user (the commands he will issue to do things in the game) and to inform him of the result of his actions with a text message on the screen. There are three major commands we will use for this:
- Console.Write: This command writes something on the screen, while leaving the cursor at the same line.
- Console.WriteLine: This command writes a line on the screen and then presses Enter to position the cursor in the next line.
- Console.ReadLine: This command reads what the user has entered. We will use this command to get the user input and store it in a variable we will call ‘user_command’. Intuitive isn’t it?
All of the above functions are existing functions in C# to process input and output in a program. The fact that they all start with ‘Console.’ simply means that these functions are part of a class called ‘Console’ – look at the class as a large collection of functions. And yes, this is a very simplistic and – to be honest – technically wrong way of putting it. But it will keep your brain from exploding while giving you some time to read in Google what Console is in C#. (remember to check again the basic tutorial for Developing a chess program here, where I have a short description of what a class and a function is)
Decision commands: The program needs to decide what to do when the user does some things. So for example, when the user enters the command “Open door” it needs to be able to decide whether or not the door will open (if the user has the key it will open, if not it will not). We will use the ‘if’ command to perform this kind of operations. The way this command works is very intuitive. You will see in the code how this happens.
The commands the game will accept are the following:
- Look around
- Open box
- Get key
- Open door
- Q will exit the game
Loop commands: We need to make the game constantly ask for user input. So after each command the user enters, the program must process it, inform the user of the result of his action and then go back and again ask for input. We will use a do… while look to perform that. With that, every line of code that is within brackets between the ‘do’ and the ‘while’ commands is constantly executed until the condition in the ‘while’ command is met. You will understand when you see the code.
TIP: Google is your friend. Don’t be shy to use it. If you are not sure how a command works just write it into Google search and find out yourself! Not sure how the ‘if’ command works? Google “C# if command” and there you go! (the C# is always needed in the search if you search for C# – but this is rather a tautology and not an advice)
Writing the code
Are you ready to actually write the code?
Well, don’t worry. I have done that for you. Look at code below and write down the program in your own project’s Main method yourself. Go to the static void Main(string args) method and write down the below code inside the brackets.
// Declare the variables
String user_pocket = "";
bool exit_game = false;
// Main program loop
// Ask for user input
Console.Write("What do you want to do? (press Q to exit) ");
user_command = Console.ReadLine();
// Process user input
if ((user_command == "Open door") && (user_pocket == "") )
Console.WriteLine("Door is locked");
else if (user_command == "Look around")
Console.WriteLine("You see a box on a table");
else if (user_command == "Open box")
Console.WriteLine("There is a key inside");
else if (user_command == "Get key")
Console.WriteLine("You got the key");
user_pocket = "key";
else if ( (user_command == "Open door") && (user_pocket == "key") )
Console.WriteLine("You opened the door! Bravo!");
user_pocket = "key";
exit_game = true;
else if (user_command == "Q")
exit_game = true;
} while (exit_game == false);
There you go!
A game that you wrote!
Your code should look like that.
- All C# commands end with a semicolon, as you may have noticed. With some exceptions as may have also noticed.
- The comments are entered after double slash ( // ). They are usually very useful not only for others but mainly for the programmer himself!
Download the code
Ideally you should write the code on your own, so that you practice. However for reference purposes, you can also download the example below.
The file contains the solution as it was developed for this tutorial.
Executing the program
In order to build your code, select Build > Build solution (or Rebuild solution , after you have built it for the first time).
In order to run your program, select Debug > Start without debugging.
The result should look something like this…
That is, if you are smart enough to find the key…
Like when I copied the code from my first Commodore computer manual, you may not know exactly what you write and why. But with what you have read above, you will certainly understand the meaning of the commands and what they do!
You now have a game that you wrote. And potentially a gate to the world of programming. At the end, it is not up to me to teach you anything. It is up to you to learn. As Buddhism says, when the student wants, the teacher appears. I am not your teacher. But I may have instilled in you the desire to search for one…
APPENDIX – Jurassic Park Adventure Game
When I was a kid, I developed an adventure called “Jussaric Park” based on the known book of Michael Crichton. It was (and still is) a simple yet fun to play text-based adventure writen in QBasic.
You can download the code from below and use your favorite QBasic IDE to view, edit and execute it. The QB64 is a good choice for that.
You can find the code below. Simply copy-paste it into a QBasic editor and execute. The IOURASIO file is the main game. The JUPASE program is called when you reach the Visitors’ Center.
As already mentioned, it is writen in QBasic and it is a playable and enjoyable (at least for those not addicted to ultra-fast 3D graphics) text-based game. It has comments inside the code to help you understand what happens and how it works, so essentially it is an extension of the tutorial you just read.
Note: You will have to update the path in the JUPASE call. (See? Even when having fun, you can alway learn something!)
And keep coding!