Writing a Game in a Console Application - Teaching Programming

November 11, 2014

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:

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

Me: Err, well - we’ll do that next time!

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 this.

The hardest part

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:



Console.SetCursorPosition(left, top);

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



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

And the function to read the input would be:



        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:



        private static int \_left;
        private static int \_top;

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

Is this efficient?

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



            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.

Not much of a game?

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.



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024