Posted on

Scratch Coding – Snake

We are going to be making a classic game today: Snake. The idea is that the snake starts off really small as one block and then every time eats a piece of food the snake grows one block. The player must look for more food but must keep the snake off the side walls and from touching itself.


We are going to make a custom snake using the rectangle fill tool. Our costume should look like:

Be sure to make our rectangle in the centre of the drawing area.

We also use a ball as the food.

Make the Snake Move

The first step is to make our snake move around the screen. We want the player to be able to use the arrow keys to control the snake.

We start the snake in the centre of the window at 0, 0.

We want the snake to always be moving, if we want something to always happen we use the forever block:

Next we can test for some user input and point the snake in the correct direction:

Test our game.

We can see that our snake moves around, it’s a little big and doesn’t look much like a snake, so let’s make it a lot smaller:

Place the set size block just before the forever loop.

Growing Up

As our snake eats more food we want to be able to make our snake look longer. The way we are going to do this is to use a number of snake part clones and have them follow the head sprite. To make it so they can follow the lead sprite, we need to store the position that the lead sprite steps into.
For this we are going to create two lists:

  • XPositions
  • YPositions

We should end up with:

We need to make sure that our list is cleared when our game first starts:

Change our starting script to include the removal of all items from our position lists.

Now we need to keep track of each position the snake sprite lands in:

This will fill our list up quickly, so make sure you have the lists hidden on the screen.

In order to have our clone sprites know which position they need to sit in, we need to keep track of which clone number they are. Each clone will look further down the position list to work out which spot they need to sit on.

Let’s create two variables:

Now to create a clone:

The clone will find which number it is based on the score, then it will forever look in the position list to see when it should move to. If it touches itself (the green) then the game is over.

Now we need to make it such that our snake creates a clone when it touches a food pellet, this block must be placed in the main loop, just under the instructions for testing the movement.

We broadcast a message that the food pellet will listen for.

Touching the Edge

We want the game to end if the snake touches the edge of the screen, so we will place this test under the other movement tests:

Moving the Food

The food pellet should move into a random position:

When we receive a new message:

Harder Levels

Try creating a harder level that our snake must move around:

We want our snake to die if it touches the walls, we can add a check to see if it touches the wall colour.

Now we can change the level when we get to a certain score: