DEV Community

Paul Michaels
Paul Michaels

Posted on • Originally published at pmichaels.net

Console Games - Snake - Part 2

Following on from this post, we were creating a snake game for the purpose of teaching a 9 year old to program. This post will not make sense without its predecessors.

Clean up the tail

The next part of this game is to get the snake game to tidy up after itself (to remove the last part of its own tail). This was done (for us) in two parts.


private static int _length = 3;        

private static void CleanUp()
{
    while (points.Count() > _length)
    {
        points.Remove(points.First());
    }
}

This is called from AcceptInput:

private static bool AcceptInput()
{
    . . . 
    points.Add(currentPos);
    CleanUp();

    return true;
}

If you run that now, it simply does what it did before; the final part is to re-introduce the clear:


private static void DrawScreen()
{
    Console.Clear();
    foreach (var point in points)
    . . . 

So now we have a 3 star snake game; try extending the length manually to play with it. It is strangely addictive, even in this immature state.

Top comments (0)