Category Archives: VB.NET

Writing a Game in a Console Application – Teaching Programming

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.

Add a settings file from another project (VB.NET)

I recently asked this question on Stack Overflow and, although the answer I got was correct, I thought I would write it up so that a clearer explanation was available for when I forget that I asked in the first place.

Here’s the link to the SO question (as I certainly wouldn’t want to claim credit for the solution): http://stackoverflow.com/questions/20427328/sharing-settings-file-between-projects-in-vs2013

I believe that the process for C# is different, and will try to cover that in a future post.

Disclaimer

If you do read the question, you’ll see that it was suggested to create the settings file in a single library and then expose that through a public method. I do agree that this would be the best way to do things. I’m not even saying that the way here is a good way to do things. However, I was in a position where it was the only feasible way to do things, given time constraints and project architecture. I intend, in the future, the refactor, and to have just such a library of settings. If you have time do do this now, or if you haven’t yet created your settings file then I would strongly suggest you go down this route and NOT read on.

Otherwise, read on

Okay, if you’re still reading then you’re either in the same position I was, or you didn’t read the disclaimer.

Adding a Settings file as a link

In the project that you wish to add the settings file, first, delete any existing settings files from inside (or outside) the “My Project” folder. Note that by default, “Show All Files” is turned off, and this can hide the “My Project Folder”, so make sure it’s on:

ShowAllFiles

MySettings

Now that’s deleted, selected the project and select “Add Existing Item”, and in the dialog that appears, find your settings file; then, and this is the important part, select “Ass as Link”:

AddAsLink

Okay, now you have a settings file from another project. Now you need to set the namespace and the custom tool. They should look like this:

SettingsSettings

Finally, you need to manually tell the settings file to generate itself by right-clicking, and selecting “Run Custom Tool”.

Conclusion and Caveat

One of the reasons that this is a bad idea is the last step. Although you are linking to the settings file, you need to manually generate each time.

So, it’s not a good way to do things, but if you need it, it does work.