Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was thinking about creating a snake game with C#, so I ran ideas in my head, and some problems came up.

How can I track and output in the correct location the blocks that run after the snake's head?

If the snake is built of five blocks, and the user starts going in a circle, how can I print the snake body in the right location?

Also, how can I create an action that will run on the background, which will move the snake forward, no matter what the user does?

What structure should my code have? (code design structure)

This should be a console application, since it's the only framework I am familiar with.

I am not looking for finished code, since I want to really understand how it should work.

share|improve this question
add comment

2 Answers 2

up vote 20 down vote accepted

Representing the Snake

Creating a Snake game is pretty simple. The first thing you need is a way to represent your snake's body. If you consider your snake to be made up of blocks or tiles, your body can simply be a list of these blocks coordinates.

In your case, if you intent do a console application, each of these will be a character on the console, and the position would correspond to one line or row of the console output. So you start with this:

// List of 2D coordinates that make up the body of the snake
List<Point>() body = new List<Point>();

At this point, your list is empty, so your snake has no body. Let's say you want a snake of length 5 and the head should start in position (5,2) and grow towards the bottom. Just add those positions to the list when the game starts, for instance:

// Initialize the snake with 5 blocks starting downwards from (5,2)
for(int i=0; i<5; ++i)
{
    body.Add(new Point(5, 2 + i));
}

Rendering the Snake

For rendering just draw a character at each position on the body list. The example above for instance could be drawn as:

...................
...................
.....O............. -- y = 2
.....#.............
.....#.............
.....#.............
.....V.............
...................
     |
     x = 5

You can get more complicated by choosing different characters for the character's head (first element on the list) and tail (last element on the list) and even orient them depending on the positions of the adjacent blocks. But for starters, just render everything as a # or something.

You could for instance start with a 2D char array containing the background like this:

// Array with map characters
char[,] render = new char[width, height];

// Fill with background
for(x=0; x<width; ++x)
    for(y=0; y<height; ++y)
        render[x,y] = '.';

And then iterate over the snake's body drawing it to the array:

// Render snake
foreach(Point point in body)
    render[point.X, point.Y] = '#';

And finally iterate over the array again and write each character to the console, with a linebreak at the end of each row.

// Render to console
for(y=0; y<height; ++y)
{
    for(x=0; x<width; ++x)
    {
        Console.Write(render[x,y]);
    }
    Console.WriteLine();
}

Moving the Snake

Finally, movement. The first thing you need is to keep track of the Direction the snake is moving. This can be a simple enum:

// Enum to store the direction the snake is moving
enum Direction { Left, Right, Up, Down }

And the act of moving the snake can be done just by removing the last block from the list and adding it on the front, in the correct direction. In other words something like:

// Remove tail from body
body.RemoveAt(body.Count - 1);

// Get head position
Point next = body[0];

// Calculate where the head should be next based on the snake's direction
if(direction == Direction.Left) 
    next = new Point(next.X-1, next.Y);
if(direction == Direction.Right) 
    next = new Point(next.X+1, next.Y);
if(direction == Direction.Up) 
    next = new Point(next.X, next.Y-1);
if(direction == Direction.Down) 
    next = new Point(next.X, next.Y+1);

// Insert new head into the snake's body
body.Insert(0, next);
share|improve this answer
5  
Good call on the moving the last block to the front for movement! –  SpartanDonut Mar 1 '12 at 16:46
    
Great answer. I love how you are moving the snake - it's quite elegant. –  Cypher Mar 1 '12 at 21:22
add comment
  1. Each time a timer ticks you update the location of the snake and draw the shapes at the new locations

  2. Use a System.Windows.Forms.Timer for the background action to trigger the game to move the snake around.

  3. Use classes and inheritance for the structure

share|improve this answer
    
This is a very vague answer. Step number three is especially so. –  Byte56 Jan 29 '13 at 1:18
add comment

protected by Community Jan 29 '13 at 2:33

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.