Console Games - Snake - Part 5 (Score)
Tags: csharp, gamedev
Published: 2019-10-26 11:36:13

Continuing on from my series of posts on writing a console game with my children, this post will cover the score and speed up the game a little to make it progressively harder.  If you haven't seen the earlier posts then <a href="http://pmichaels.net/2014/11/11/writing-a-game-in-a-console-application-teaching-programming/">start here</a>.

<strong>What's the score?</strong>

Let's start with the score; first thing to do is create a variable to store it:

```csharp
    class Program
    {
        private static int _length = 6;
        private static int _score = 0;

```

The way to increase the score is to eat food, so that's quite straight-forward:

```csharp

private static void DetectCollision(Position currentPos)
{
    …
    // Check if we've eaten the food
    if (_foodPosition.left == currentPos.left && _foodPosition.top == currentPos.top)
    {
        _length++;
        _score++;
        _foodPosition = null;
}
```

Nothing hugely complicated there.  Finally, display the score:

```csharp

private static void DrawScreen()
{
    Console.Clear();

    Console.SetCursorPosition(Console.WindowWidth - 3, Console.WindowHeight - 1);
    Console.Write(_score);

```

<strong>Speed</strong>

That's the score; next we need to speed the game up.  Currently we have an `UpdateGame()` method that determines how often the game is updated; here’s what it currently does:

```csharp
        private static bool UpdateGame()
        {
            if (DateTime.Now < nextUpdate) return false;

            if (_foodPosition == null)
            {
                _foodPosition = new Position()
                {
                    left = _rnd.Next(Console.WindowWidth),
                    top = _rnd.Next(Console.WindowHeight)
                };
            }

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

            nextUpdate = DateTime.Now.AddMilliseconds(500);
            return true;
        }
```

So, we can simply change the nextUpdate to use a variable that we already have; like this:

```csharp
nextUpdate = DateTime.Now.AddMilliseconds(500 / (_score + 1));
```

<strong>Game Over</strong>

Okay, well, the eagle eyed among you may have noticed that game over just gives a runtime error; let's try something a little more user friendly.  First, we'll create a variable to store whether the game is still in play:

```csharp
        private static bool _inPlay = true;

```
 
Next, change the game loop to use this:

```csharp
        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            DrawScreen();
            while (_inPlay)
            {
```

And finally, change the `GameOver()` method:

```csharp
        private static void GameOver()
        {
            _inPlay = false;
            Console.Clear();
            Console.WriteLine("Game over.");
            Console.ReadLine();
        }
```

<strong>Final word</strong>

In the <a href="https://www.pmichaels.net/2014/12/19/console-games-snake-part-5/">original post</a> I spoke of creating exercises, and creating a catch game.  We did create the catch game; if you're interested, it starts <a href="https://www.pmichaels.net/2015/02/01/console-games-catch-part-1/">here</a>.  My children are much older now, and interested in different things.  Hopefully, one of two people have enjoyed reading this, and even doing similar exercises with their own children.