This page is part of the Snake in MIT Scratch Tutorial.
Detect if the Snake Eats its Own Tail
The game ends if the snake’s head collides with its body.
When updating the snake a check is made to see if the next position the head will occupy is already occupied by its body.
The Check Lose function is used to perform the check:
After calculating the next position the Check Lose function is called.
The Check Lose function compares the Snake Next X and Snake Next Y values to the snake parts lists to see if the next position will cause the snake to crash.
If the snake crashed then the function will set the Snake Dead value to 1.
The Snake Dead value is checked after calling Check Lose and the Game Over screen will be shown if its value is 1.
Looping Through a List
In order to test if the snake will crash the system loops through the list of positions that the snake parts occupy and compares them all to the next position.
Looping through a list makes use of two properties that lists have:
- Index: Each position in the list is accessed using a unique number.
- Length: The number of items that the list contains is known as its length
The items in a Scratch list can have an index value from 1 to the length of the list.
In order to process every item in the list a loop count is started at one. This count is used to access the list each loop and then increased at the end of each loop.
Processing all items in a list works as follows:
- Define a loop count i with a value of 1
- Repeat until i is greater than the length of the list:
- Process the list value found at index i
- Increase i by 1
The loop count is used to record which item of the list is to be processed. It is increased at the end of each loop.
The loop exits if the count is greater than the size of the list.
The Check Lose Function
The Check Lose function loops through the list of all snake body part positions and compares them to the snake’s next position:
The first line sets the loop count i to 1. This value is used to access each index of the snake parts lists.
Next a repeat block is used to loop through all of the snake parts list indexes. It repeats the block of code that processes each snake part until the value of the loop count i is greater than the length of the list.
The repeat block also checks the Snake Crashed value so that if a crash is found the loop stops immediately without checking the rest of the list.
Within the loop an if statement is used to check if item i of the snake parts lists is equal to Snake Next X and Snake Parts Y. This would mean that the position of this body part is equal to the next position of the snake.
If it is then the Snake Crashed value is set to 1 and the Snake Crashed message is broadcast.
At the end of the loop the loop count i is increased by one so that the next index in the snake parts list will be checked.
Before moving the snake as part of the Update Snake function its next position is checked to see if it would cause the snake to eat its own tail.
The Check Lose function loops through the list of body parts and compares their position to the next position the snake will move to.
If the snake crashes a message is broadcast and the backdrop is switched to the Game Over scene.
Next Section: Moving the Apple