Console Games - Snake - Part 5 (Score)

pcmichaels profile image Paul Michaels Originally published at pmichaels.net ・2 min read

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 start here.

What's the score?

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

    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:

private static void DetectCollision(Position currentPos)
    // Check if we've eaten the food
    if (_foodPosition.left == currentPos.left && _foodPosition.top == currentPos.top)
        _foodPosition = null;

Nothing hugely complicated there. Finally, display the score:

private static void DrawScreen()

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


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:

        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)

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

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

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

Game Over

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:

        private static bool _inPlay = true;

Next, change the game loop to use this:

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

And finally, change the GameOver() method:

        private static void GameOver()
            _inPlay = false;
            Console.WriteLine("Game over.");

Final word

In the original post I spoke of creating exercises, and creating a catch game. We did create the catch game; if you're interested, it starts here. 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.

Posted on by:

pcmichaels profile

Paul Michaels


I'm a Lead Developer. I've been programming professionally since 1997. I'm interested in finding neat solutions to difficult problems.


Editor guide

Wow this is so great, you make it look easy. I really want to be a game developer, I want to ask if a game developer should be good at math? honestly i like math but sometimes I don't like math when my teacher teaches it badly, and makes me think math is difficult