Making the snake grow

When the snake eats a fruit, it must grow. This is what makes the game increase its difficulty.

The idea: We know at every frame the snake moves its head according to its direction, a new piece is added to link the head with the rest of the body, and the tail is deleted.

To make the snake grow, we simply won't delete the tail for a given number of frames if the snake just ate a fruit. Adding a new piece without deleting anything will make the snake grow.

The development: We need a variable to know if the snake has just eaten a fruit, and eventually how many frames have passed since that moment. Add a new class level variable called justEaten that will start at 0 (the snake hasn't just eaten) and will contain the number of ...

Get Flash Game Development by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.