Grow and collision bug in Snek Game
Grow and collision bug in Snek Game
Guys I have almost completed the snek game, but there seems to be a problem, kind of a bug.The snake collides one step ahead before actually colliding with the goal, making the goal respawn in some other location(Note:I had diasbled the Snek.Grow() function to test the collision). Also the grow is not working, whenever i eat the goal, the code crashes.(Enable Snek.Grow()). here's the repository. Thank you!!!!
https://github.com/Sheriff40/GitSnek.git
https://github.com/Sheriff40/GitSnek.git
Re: Grow and collision bug in Snek Game
Are you running in debug config? You should be bro! If you do, you will see that you've got bigger problems than just grow not working.
Rule #1, always test your stuff in debug. That why we put all those assertions in there for m8.
Rule #1, always test your stuff in debug. That why we put all those assertions in there for m8.
Chili
Re: Grow and collision bug in Snek Game
Yes i tried to run it in debug mode. It does not run at all. Just a white screen. And then i have to stop the program.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Grow and collision bug in Snek Game
First found problem:
Color c = HeadColor sets c to HeadColor, then gets destroyed at the }.
Second problem:
Same with this code, you aren't actually setting anything for the segments.
You are assigning Head and Body color to a temporary variable that gets destroyed when the function ends. The equals operator is an assignment operation, therefore, Color c = SomeColor means assign SomeColor to c.
You have a Color c in Snek::Segment, you should be assigning colors to that c instead of making a new temporary Color c in those functions.
Third problem:
You are using MaxSegments here to determine the end of the loop. You have a member variable nsegments, shouldn't that be used instead? You want to loop from the head segment to the number of segments that the snek has grown by, not the max size the snek can grow to.
Fourth problem
In your Location::Add function you aren't adding, you are assigning the X and Y so there will never be any movement.
Code: Select all
void Snake::Segment::InitHead(const Location & int_loc)
{
Loc = int_loc;
Color c = HeadColor;
}
Second problem:
Code: Select all
void Snake::Segment::InitBody()
{
Color c = BodyColor;
}
You are assigning Head and Body color to a temporary variable that gets destroyed when the function ends. The equals operator is an assignment operation, therefore, Color c = SomeColor means assign SomeColor to c.
You have a Color c in Snek::Segment, you should be assigning colors to that c instead of making a new temporary Color c in those functions.
Third problem:
Code: Select all
void Snake::Draw(Board& brd) const
{
for (int i = 0; i < MaxSegments; i++)
{
segment[i].Draw(brd);
}
}
Fourth problem
Code: Select all
void Add(const Location& val)
{
x = val.x;
y = val.y;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: Grow and collision bug in Snek Game
This is the first one I noticed as well. I think it is what trips the assert when you run.albinopapa wrote: Third problem:You are using MaxSegments here to determine the end of the loop. You have a member variable nsegments, shouldn't that be used instead? You want to loop from the head segment to the number of segments that the snek has grown by, not the max size the snek can grow to.Code: Select all
void Snake::Draw(Board& brd) const { for (int i = 0; i < MaxSegments; i++) { segment[i].Draw(brd); } }
Chili
Re: Grow and collision bug in Snek Game
Guys, these were the problems in my Master Branch. I have already created a WithoutObstacle branch a week ago without these code errors. The WithoutObstacle branch is the actual branch where I face those bugs. Could you guys please check the WihoutObstacle branch. The game runs in that Branch, but there are certain bugs and errors that i can't figure out. Thank you!!! ^_^ and i am sorry for not giving information about the WihoutObstacle Branch, i thought that it would automatically show the branch that i am currently working on.
Re: Grow and collision bug in Snek Game
+1 for not working in master branch (good habit)
-1 million for not mentioning the correct branch when asking for help
-1 million for not mentioning the correct branch when asking for help
Chili
Re: Grow and collision bug in Snek Game
Code: Select all
Location & Snake::GetNextLocation(const Location& delta_loc) const
{
Snek.MoveBy(Delta_loc);
if (goal.IsEating(Snek.GetNextLocation(Delta_loc)))
{
Snek.Grow();
goal.respawn(rng, Snek, brd);
};
}
Re: Grow and collision bug in Snek Game
Sorry Chilli and albinopapa. I thought that the repo link would automatically adjust to the current connect branch. Also, in the current branch, the program runs and the game is playable in the release mode, but does not in the debug mode. Could you check it up!!! Thank you!
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Grow and collision bug in Snek Game
Code: Select all
bool Snake::IsInTile(const Location& target) const
{
for (int i = 0; i < nsegment; i++)
{
if (segment[i].Getlocation() == target)
{
return true;
}
}
}
The way I found this was when the window was all white, I switched to Visual Studio and pressed the pause button instead of the stop button. This paused in this function:
Code: Select all
void Goal::respawn(std::mt19937 & rng, const Snake& snek, const Board& brd)
{
std::uniform_int_distribution<int>xDist(2, brd.GetWidth() - 1);
std::uniform_int_distribution<int>yDist(2, brd.GetHeight() -1 );
Location loc;
do
{
loc.x = xDist(rng);
loc.y = yDist(rng);
} while (snek.IsInTile(loc));
Loc = loc;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com