# Snake game - Snake Collision

So I am making a snake game as final assignment for semester 1 of my game dev study.

We have to write it in C# in Visual Studio 2015. I am using the .NET 3.5 framework.

So basically how my snake works is as follows:

I made a vector2 List to store the snake parts. It starts only with the head obviously. When collission with the food occurs, I add a vector2 to the list. I draw a rectangle in the paint function and I make it take over the last position of the previous snakepart. That is how my snake works and how my snake gets longer as you eat the food.

I am having trouble with the snake collision itself though.

This is the code I came up with to check if the snake collides with itself:

for (int i = 1; i < Snake.Count; i++)
{
if (Snake[0].X > Snake[i].X + (i * 64) || (Snake[0].X + (i * 64)) < Snake[i].X || Snake[0].Y > (Snake[i].Y + (i * 64)) || (Snake[0].Y + (i * 64)) < Snake[i].Y)
{
SnakeCollission = false;
}
else
{
SnakeCollission = true;
}
}


So I check whenever the snake is NOT having collission, then snakecollission is false, else it's true.

What happens now though is that when I eat one piece of food, the snakecollission turns true, while the snake is not touching itself.

This is the link to my complete code: http://pastebin.com/gVUuazQN

Any help would be appreciated, I hope I have given an understandable description :)

• Did you notice that if snake[0] is touching snake[2] and there are 4 pieces, then you set SnakeCollission to false, then you set SnakeCollission to true, then you set SnakeCollission to false, so it ends up being false even though there's a collision? – user253751 Nov 13 '16 at 23:45

## 3 Answers

First of all, your code detecting collision is wrong in 2 ways.

1. To check if your head collides with an other part you should just check if the 2 positions are the same

if (Snake[0].X == Snake[i].X && Snake[0].Y == Snake[i].Y)


is doing that just fine.

2. Whenever you find a collision, you need to break the loop, else SnakeCollision will go back to false if you are not colliding with the tail of your snake.

Now, your problem is about getting collision just after eating an apple. Well, it is pretty normal you are colliding with yourself as you add the new part at the same spot where you already are, so during next collision detection it will think you ate yourself. I believe you need to add a special case just after eating an apple to deal with this case.

By the way your code is really messy. The paint function should be used only to paint, but you are using it to add new snake parts, check user inputs, etc... That makes your code really hard to read and understand!

Try use same test collision of the apple. It's the same principle I think

if (Snake[0].X > (Snake[i].X + 64) || (Snake[0].X + 64) < Snake[i].X || Snake[0].Y > (Snake[i].Y + 64) || (Snake[0].Y + 64) < Snake[i].Y)
{
Collission = false;
}
else
{
Collission = true;
}


I hope that help.

In the original code, it will only detect collision if colliding with the tail of the snake (last iteration of the for loop). Any collisions with previous sections of the snake will be overwritten by the last value.

One solution (note that the if line is inverted):

SnakeCollission = false;
for (int i = 1; i < Snake.Count; i++)
{
if (Snake[0].X <= Snake[i].X + (i * 64) && (Snake[0].X + (i * 64)) >= Snake[i].X && Snake[0].Y <= (Snake[i].Y + (i * 64)) && (Snake[0].Y + (i * 64)) >= Snake[i].Y)
{
SnakeCollission = true;
break;
}
}


By the way, you can simplify much code by using 0,1,2,3... as values for X/Y instead of 0,64,128,192..., and multiply by 64 in a single place (when drawing the snake on screen) instead of everywhere else in the code. Code will be simpler and much easier for you to change the scale if needed.) For example the "if" line above could be reduced to:

if (Snake[0].X == Snake[i].X) && (Snake[0].Y == Snake[i].Y)

• Did you just pretty much copy my answer? :) – realUser404 Jan 20 '17 at 17:03
• Haha, great mind think alike, I guess :) Didn't see your answer until now, I swear! – JanErikGunnar Jan 20 '17 at 17:11