Console Games - Snake - Part 5

December 19, 2014

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 = 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

I’m still working through this game, and with a catch game (which I’ll also post at some stage) with the children. The way that I’ve been addressing this is, after an initial explanation phase, asking the children to complete each small section; for example, in the above section, I would have asked them to complete three separate tasks: To create a new boolean variable, to use that variable in the while loop and to re-write the GameOver() function so that it sets the variable to false. Roughly speaking, the posts are arranged in small sections, and they could be treated as separate exercises.

Please leave a comment if you found any of these helpful, or with any suggestions for improvements.

If I get the time or the inclination, I might break these posts down into individual exercises and post that as well.

Profile picture

A blog about one man's journey through code… and some pictures of the Peak District

© Paul Michaels 2024