Require help in detection collison between 2 snakes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 11
Reputation: Nyaato is an unknown quantity at this point 
Solved Threads: 0
Nyaato Nyaato is offline Offline
Newbie Poster

Require help in detection collison between 2 snakes

 
0
  #1
Jan 26th, 2009
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:

  1. bool snake::collide(void)
  2. {
  3. //Traverse through all the nodes to check whether snake collide with itself
  4. for(node *temp = start; temp->pnode != NULL; temp = temp->pnode)
  5. {
  6. if(temp->dirtyX == posX && temp->dirtyY == posY)
  7. {
  8. return true;
  9. }
  10. }
  11. return false;
  12. }

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

  1. bool snake::collide(int x, int y)
  2. {
  3. //Traverse through all the nodes to check whether snake collide with itself
  4. for(node *temp = start; temp->pnode != NULL; temp = temp->pnode)
  5. {
  6. if(temp->dirtyX == x && temp->dirtyY == y)
  7. {
  8. return true;
  9. }
  10. }
  11. return false;
  12. }

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!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Require help in detection collison between 2 snakes

 
1
  #2
Jan 26th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC