Writing a game in a console application - Part 0
Tags: gamedev, csharp, visualbasic
Published: 2019-10-02 07:01:40

I wrote and published this some time ago <a href="https://www.pmichaels.net/2014/11/11/writing-a-game-in-a-console-application-teaching-programming/">here</a>.  At the time, I thought it was an excellent way to learn the principals of game development and, as the original post suggests, a good teaching methodology for anybody wishing to learn to program.

Here is the original post.

Recently I began trying to teach my children some basics of programming.  I'd previously tried teaching them using tools like Scratch, but they seemed to get distracted by the graphics whizzing around, and they forgot about the actual coding.

This time, I started up a Visual Basic Console Application, and took them through a couple of basic programs: guess the number, guess the favourite food and calculate prime numbers.

They quickly started to ask about games (by which they meant arcade games), but I didn't want to jump into a game framework like XNA, as I felt that this time, they were actually understanding some of the constructs.  My idea was that we could write a game using a console application.  

In order to introduce this, I simply got them to swap the code they had been writing (for example):

```
        Dim name As String

        Console.WriteLine("hello, what is your name?")
        name = Console.ReadLine() ' Remember name here
```


For something like this:

```
        While True
            Dim test As ConsoleKeyInfo = Console.ReadKey()

            Console.Clear()

            If test.Key = ConsoleKey.LeftArrow Then
                Console.WriteLine("You have pressed the left arrow")
```

Which was going well - I had definitely piqued their interest.  The subsequent conversation went something like this:

<blockquote>
	9 Year Old: So, how do we use this to move something around the screen?

	Me: Err, well - we'll do that next time!
</blockquote>

The truth is I had no idea… but I do now, so I thought I'd write at least one post describing it.  This is the it.  Note that the code that follows in C#, and the preceding code is VB.  That's because I thought that a 9-year-old child would relate better to Visual Basic than C# initially, but I personally think in C# (I learnt to program using Spectrum Basic - and I think we'd have more children programming if you had to wait five minutes for a game to load).  

Translation between the two is straight-foward, but you can always use <a href="http://converter.telerik.com/">this</a>.

<strong>The hardest part</strong>

The hardest part in getting a console game working is displaying a character outside of the next position on the console; the trick here is:

```csharp
Console.SetCursorPosition(left, top);
```

Extending this, here a function to position a character on the screen:

```csharp
        private static void DrawScreen()
        {
            Console.Clear();
            Console.SetCursorPosition(_left, _top);
            Console.Write('*');
        }
```

And the function to read the input would be:

```csharp
        private static void AcceptInput()
        {
            ConsoleKeyInfo key = Console.ReadKey();

            switch (key.Key)
            {
                case ConsoleKey.LeftArrow:
                    _left--;
                    break;
                case ConsoleKey.RightArrow:
                    _left++;
                    break;
                case ConsoleKey.UpArrow:
                    _top--;
                    break;
                case ConsoleKey.DownArrow:
                    _top++;
                    break;

            }
```

Finally, the main function and variables:

```csharp
        private static int _left;
        private static int _top;

        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            while (true)
            {
                DrawScreen();
                AcceptInput();
            }
        }
```

<strong>Is this efficient?</strong>

No - it's not.  You can improve this by using:

```csharp
    if (Console.KeyAvailable)

```

I'm not planning to write Call Of Duty, but I will try and get them to optimize it slightly, just to get the idea across that the less code executed, the better.

<strong>Not much of a game?</strong>


No, it's not.  But it is the basis of every arcade game.  We had a talk, and have a snake game, and a catch the falling characters game planned; both seem eminently doable based on this starting point.