DEV Community

Paul Michaels
Paul Michaels

Posted on • Originally published at pmichaels.net

Console Games - Snake - Part 4 (Collision Detection)

Collision detection is pretty much necessary for any arcade game. Super Mario would be pretty boring if he just walked through the mushrooms (or whatever they were supposed to be).

mushroom

This post continues from a little series on writing console games (start here).

In our game, we have three possibilities for collision: the wall (or edge of the console), our own tail, and food. Two of these are game over, and collecting food should make the tail longer. Let's start with game over:

The Move() function currently looks like this:


        private static void Move(ConsoleKeyInfo key)
        {
            Position currentPos;
            if (points.Count != 0)
                currentPos = new Position() { left = points.Last().left, top = points.Last().top };
            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);
            CleanUp();
        }

Off the screen

Since we're moving anyway, this may be the best time to see where we are; since the new point is created here, let's just see if it's off the screen. Here's the revised bottom of the Move command:

            }

            // Check if we're off the screen
            if (currentPos.top < 0 || currentPos.top > Console.WindowHeight 
                || currentPos.left < 0 || currentPos.left > Console.WindowWidth)
            {
                GameOver();
            }

            points.Add(currentPos);
            CleanUp();

Use Shift-Alt-F10 to get VS to generate the GameOver() function; which will just look like this:

        private static void GameOver()
        {
            throw new NotImplementedException();
        }

Crash into the tail

For this, we need to make a few small changes. The first is to change the _lastKey variable to look like this:

static ConsoleKeyInfo? _lastKey;

This means that we can determine whether a key has been pressed or not; next, check the collision; here's the latest bottom of the Move function (we'll refactor later):

                GameOver();
            }

            // Check if we've crashed into the tail
            if (points.Any(p => p.left == currentPos.left && p.top == currentPos.top))
            {
                GameOver();
            }

            points.Add(currentPos);

Just a simple Lambda to check if the head has crashed into the tail. In UpdateGame() the call to Move() now needs to deal with a nullable value:

            if (_lastKey.HasValue)
            {
                Move(_lastKey.Value);
            }

Food

Finally, we need to detect if we've eaten the food; We'll refactor the bottom of the move function and create a DetectCollision() function:

        private static void DetectCollision(Position currentPos)
        {
            // Check if we're off the screen
            if (currentPos.top < 0 || currentPos.top > Console.WindowHeight
                || currentPos.left < 0 || currentPos.left > Console.WindowWidth)
            {
                GameOver();
            }

            // Check if we've crashed into the tail
            if (points.Any(p => p.left == currentPos.left && p.top == currentPos.top))
            {
                GameOver();
            }

            // Check if we've eaten the food
            if (_foodPosition.left == currentPos.left && _foodPosition.top == currentPos.top)
            {
                _length++;
                _foodPosition = null;
            }
        }

All we've done here is checked the food position against the current one, increased the length of the tail, and then removed the food so that it will be recreated somewhere else on the screen; the Move() function now looks like this:

        private static void Move(ConsoleKeyInfo key)
        {
            Position currentPos;
            if (points.Count != 0)
                currentPos = new Position() { left = points.Last().left, top = points.Last().top };
            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;

            }

            DetectCollision(currentPos);

            points.Add(currentPos);
            CleanUp();
        }

Is that it?

To all intents and purposes, that is the game. It now does everything bar one feature, which is to keep score. For the final post, we'll address this, along with the speed (it should speed up gradually to make the higher levels difficult). Also, there is a little tidying up to do.

Remember that the latest version of this is here.

Top comments (0)