Console Games - Snake - Part 4 (Collision Detection)
Tags: gamedev, csharp
Published: 2019-10-19 16:14:44

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

<a href="http://pmichaels.net/wp-content/uploads/2014/11/mushroom.png"><img src="http://pmichaels.net/wp-content/uploads/2014/11/mushroom-300x298.png" alt="mushroom" width="100" height="100" class="alignnone size-medium wp-image-805" /></a>

This post continues from a little series on writing console games (<a href="http://pmichaels.net/2014/11/11/writing-a-game-in-a-console-application-teaching-programming/">start here</a>).

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:

```csharp

        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();
        }

```

<strong>Off the screen</strong>

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:

```csharp
            }

            // 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:

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

<strong>Crash into the tail</strong>

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

```csharp
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):

```csharp
                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:

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

<strong>Food</strong>

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:

```csharp
        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:

```csharp
        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();
        }
```

<strong>Is that it?</strong>

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.

<a href="https://github.com/pcmichaels/ConsoleSnake">Remember that the latest version of this is here.</a>