Bird Collisions
Before we wrap this section up, let's add collision detection between the bird and the pipes. We'll stop the pipe manager when the bird hits the pipe, just like we do with the ground.
Stop and Reset
We'll add a few methods to the pipe manager to stop the pipe manager and reset it.
We've also modified update()
to check if the pipe manager is stopped. If it is, we'll return early.
Detecting Collisions
After we check if the bird hit the ground, let's detect collisions with the pipes:
We refactored the original if
block to account for both hitting the ground or pipes.
We added a loop to detect collisions with any of the pipes, and differentiate between top and bottom pipes. We do this because we want to override the y
position (for the top pipe) of the rectangle in the collision check to be 0
and not use the pipes position.
Lastly, we called pipeManager.reset()
when the game is about to restart.
Running the game now you should get the full game loop experience. You can hit either the ground or the pipes and the game will stop, reset on click, and play again.