Hello, I'm currently programming a C++ Snake game and I'm stuck at a particular part of the program.

This particular part had me wondering for sometime now, and I've got absolutely no idea how to work it out. I want to implement a collision detection between two snakes that would be in the maze.

Instead of the usual snake game which wants you to eat the food and try to score the highest score possible, I'm programming this snake game to have static food (all foods will be generated when the game starts) and have an enemy snake that will limit the player's movement.

One of the implementations that is done is to detect the collision between the player's snake itself. The code is as below:

bool snake::collide(void)
{
	//Traverse through all the nodes to check whether snake collide with itself
	for(node *temp = start; temp->pnode != NULL; temp = temp->pnode)
	{
		if(temp->dirtyX == posX && temp->dirtyY == posY)
		{
			return true;
		}
	}
	return false;
}

Then after some improvements, I managed to allow the enemy snake's head to detect collision with the player's snake:

bool snake::collide(int x, int y)
{
	//Traverse through all the nodes to check whether snake collide with itself
	for(node *temp = start; temp->pnode != NULL; temp = temp->pnode)
	{
		if(temp->dirtyX == x && temp->dirtyY == y)
		{
			return true;
		}
	}
	return false;
}

However, I've read from some other forums that this method is only computer to player detection, there is so such player to computer detection.

I'm stuck on this part and I've got a deadline coming up, so any help would be greatly appreciated!

I've always thought that the 'snake' collision detection just looked at the next square the snakes head was going to be put. If there's something in the square, its a collision. If its food, the snake eats it and that's a good thing. If it's wall or part of either snake, it's a bad thing.

As long as you apply the same rule to both snakes, it should be able to detect collision with objects for either snake.

Comments
Seems like a plan to me
This article has been dead for over six months. Start a new discussion instead.