943,840 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 447
  • C++ RSS
Jan 26th, 2009
0

Require help in detection collison between 2 snakes

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nyaato is offline Offline
11 posts
since Nov 2008
Jan 26th, 2009
1

Re: Require help in detection collison between 2 snakes

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Getting a value between tags out of an array.
Next Thread in C++ Forum Timeline: Sorting/Returning a Linked List





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC