DEV Community

Paul Michaels
Paul Michaels

Posted on • Originally published at pmichaels.net

Console Games - Snake - Part 1

This continues on from a series on writing a game in a C# console application; the original post can be found here.

Based on this earlier post, we had a working console game. Admittedly it doesn't do much, apart from allow you to move a star around the screen. In order to turn this into a snake game, the first thing to do is to no longer clear the screen:

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

That gives us a snake - but you might notice that when you move left, it doesn't 'trail'. There is a possible workaround (albeit, not massively efficient - although remember that this is a game with the purpose of teaching programming).

First, create a struct to hold the position:

        private struct Position
        {
            public int left;
            public int top;
        }

This obviously could be a class. Next, create a list of these:

        private static List<Position> points = new List<Position>();

Here's what the AcceptInput function looks like with the changes:

        private static bool AcceptInput()
        {
            if (!Console.KeyAvailable)
                return false;

            ConsoleKeyInfo key = Console.ReadKey();

            Position currentPos;
            if (points.Count != 0)
                currentPos = points.Last();
            else
                currentPos = GetStartPosition();

            switch (key.Key)
            {
                case ConsoleKey.LeftArrow:
                    currentPos.left--;
                    break;
                case ConsoleKey.RightArrow:
                    currentPos.left++;
                    break;
                case ConsoleKey.UpArrow:
                    currentPos.top--;
                    break;
                case ConsoleKey.DownArrow:
                    currentPos.top++;
                    break;

            }

            points.Add(currentPos);

            return true;
        }

        private static Position GetStartPosition()
        {
            return new Position()
            {
                left = 0,
                top = 0
            };
        }

But what about the tail

In traditional snake, the last element is removed each iteration, unless you eat something. Doing it the above way lends itself to this more easily. I'll tackle that for the next post.

Top comments (4)

Collapse
 
luturol profile image
Rafael Ahrons

Loved the article. Making games it's way much more fun to learn how to code, than looking only for documentation and silly "projects" to only test the theory.

Collapse
 
pcmichaels profile image
Paul Michaels

Thank you for reading it. I suppose the only problem with learning to write games is that some of the practices that make sense for game programming, don't apply to (or are bad practice in) LOB programming.

Collapse
 
luturol profile image
Rafael Ahrons

LOB programming = Line of Business programming?

Why they are a bad practice in LOB Programming? You can have fun writting games while learn how to use some techs and test others while having fun doing it. The only problem is that the logic you apply on it is a little different, but with you still code tests, code clean, patterns you are still in LOB programming, don't you think?

I'm doing your tutorial and i'm loving it! Console applications are still fun to make and play shushushs they reminds me of childhood. The first program I made when trying to learn how to code was a tic tac toe in C# in my first internship and I remember to struggle with Console positions and Clear shshshs

Thread Thread
 
pcmichaels profile image
Paul Michaels

I'm thinking specifically of things like async / await, that you have to be careful with in games, but are basically standard everywhere else. There's a few other examples that relate to the fact that you're essentially replacing what a Web browser, WPF, or WinForms would do for you.

I'm happy that your enjoying the tutorial.