Ask coding questions

← Back to all posts
Can't understand How Snake Grows
SaiTheCyclone

So I've been following a tutorial on how to make a snake in the terminal with Python to gain more coding experience and there's a piece of code that I find confusing. I am confused about the following code on how it makes the snake increase its length when it eats the food. I'm hoping that someone gives me a more clear explanation on how the following code makes the snake grow.

Here is the full code:

Many thanks for help

Answered by JustCoding123 [earned 5 cycles]
View Answer
Voters
SaiTheCyclone
Comments
hotnewtop
JustCoding123

If I understand the code correctly every time the snake moves it checks if it is on the food if it is it increases the score and repositions the food else it removes the last part of the snake. So if it hits the food it will not remove the last part causing the snake to grow.