DEV Community

Paul Michaels
Paul Michaels

Posted on • Originally published at

Writing a game in a console application - Part 0

I wrote and published this some time ago here. 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()


            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.SetCursorPosition(_left, _top);

And the function to read the input would be:

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

            switch (key.Key)
                case ConsoleKey.LeftArrow:
                case ConsoleKey.RightArrow:
                case ConsoleKey.UpArrow:
                case ConsoleKey.DownArrow:


Finally, the main function and variables:

        private static int _left;
        private static int _top;

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

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.

Top comments (3)

peledzohar profile image
Zohar Peled

You might want to check out my ExtendedConsole github project that provides easy ways to write colorful texts and menus, and to read client input.

For more details:

pcmichaels profile image
Paul Michaels

Looks interesting. In the following posts, I go into the process of things like collision detection, etc.

If you're interested, the original posts are here:

peledzohar profile image
Zohar Peled

Will check it out as soon as I can, thanks!