In this part, we will complete the development of the game by adding logic that will restart the game when the player lost. Changes that we will introduce in this part reflected in this commit.
To detect when the snake eats its tail, we will use the Segment method that will return the projection of the point on the segment.
The magic involved to find the projection of the point on the segment is outside this course scope.🧙♂️
Now we are ready to add the method that will tell us if the game should be over.
First, we check if the head of the snake is outside of the game field. If the snake has more than five segments, we want to check for self-intersection.
We declare a self-intersection when the projected head is inside of one of the tail segments, and the distance between the projected point and head is less than half of the cell.
Restarting the Game
We will check if the game is over right after the call to process method.
Now we can open the web page and enjoy the game!
Reach the next level of focus and productivity with increaser.org.