| | |
Require help in detection collison between 2 snakes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 0
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:
Then after some improvements, I managed to allow the enemy snake's head to detect collision with the player's snake:
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!
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)
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:
C++ Syntax (Toggle Plain Text)
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!
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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.
As long as you apply the same rule to both snakes, it should be able to detect collision with objects for either snake.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Getting a value between tags out of an array.
- Next Thread: Sorting/Returning a Linked List
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





