How to Make a Snake Game in Scratch

iD Tech in action

Snake is a classic game where the player will control a small snake using arrow key controls. When the snake eats a collectable item on the map, it'll grow in length.

Fun, right? Let's take a look at how to easily create Snake in Scratch. 

Sprite Creation

For a Snake game, there are two Scratch sprites that need to be created. One is the snake sprite and the other is the item the snake will be chasing. In this case, that collectable item will be an apple. Scratch Paint can be used to make the sprites. 

Design a Snake

The snake will actually be divided into two sprites: its head and body. The body will follow the head sprite around the map and grow as the snake collects items.

1. Go to Scratch > Create to start a new project titled "Snake Game".

2. Hover over the Choose a Sprite button and select Paint

3. Draw a circle for the snake's head, using a black outline on the shape.

4. Draw a pink tongue for the snake, and click Back to place it under the snake's head.

snake head sprite scratch

Draw the Body

The snake's body will be drawn separately, so it can grow as the snake collects more items without affecting the snake's head.

1. Hover over the Choose a Sprite button and select Paint to create a new sprite

2. Draw a circle with no outlines for the snake's body.

Line Up the Snake

Now, with two parts of the snake - the head and the body - you can line them up in the game preview window (you might need to resize them). 

1. Adjust the snake sprites so that they're small and centered on the screen.

2. Move the body slightly to the left of the head, so they look connected.

As a tip, the snake's body should be about 1/3rd the size of its head. It should also slightly overlap the head, so it looks like a neck.

The sprites should be small on the screen so the player has plenty of space to move around and collect items.

Snake Movement

Just to be clear, in this game, the snake will constantly be moving forward. It's up to the player to then control the snake's direction using arrow key commands.

Movement

To create movement:

1. Select the Snake Head sprite and add when flag clicked to the code area. 

2. Attach point in direction 90 and go to x, y  0, 0 to the block.

3. Attach a forever block with move 5 steps inside.

Control

To control the snake:

1. Add when space key pressed and set it to up arrow.

2. Attach point in direction 0 to the block.

3. Right-click and duplicate the block, and set it to down arrow with a direction of 180.

4. Duplicate the block two more times. 

5. Set one to right arrow with a direction of 90, and left arrow at -90.

gif showing progress with code blocks

As can be seen in the image above, the snake's body doesn't follow the head. This is because there isn't any code attached to it yet, so the body doesn't know what to do.

To make the body look connected, it will need to be coded to follow the head sprite.

Follow the Leader

Select the Snake Body sprite and add when flag clicked to the code area.

Attach point in direction 90 and a go to x, y block.

Attach a forever ↺ block with point towards and move 5 steps inside.

Choose Snake Head from the dropdown for the point towards block.

Changing Length

As a reminder, for the snake to grow, it needs to collect items (like the apple). In this game, any time the snake eats an apple, its tail will grow in length.

To track the snake's length, a variable needs to be created. This variable will track how many apples the snake eats, which will then be equal to how long the snake is.

Variables

This game will use two variables: a score and a length variable, which will determine how long the snake becomes.

In the Code tab, select Variables and click Make a Variable.

Create a variable named score and a variable named length

Set the Length

The snake's body will grow every time it eats an apple. To do this, it'll create clones of itself equal to the amount it's eaten.

1. Select the Snake Body sprite.

2. Attach the set length to 0 block to the top of its code.

3. Drag a create clone of myself block into the forever ↺ block (at the bottom).

4. Add when I start as a clone to the code area.

5. Attach wait and delete this clone to the block.

6. Drag the length variable into the wait block.

The length is set, but currently, the length will always be 0, because the variable isn't being changed. To change the variable, create and code the collectable item.

Collecting Items

For the snake to grow, you'll need to design an item sprite that the snake can eat.

Create a Collectable

This example uses an apple, but you can design any collectable item you want!

1. Hover over the Choose a Sprite button and select Paint to create a new sprite.

2. Draw a collectable item.

Set the Position

The apple will appear at random points on the map, making it impossible to predict where it'll show up next!

1. Select the Apple sprite and attach when flag clicked to the code area.

2. Attach a set score to 0 block. Eating apples will earn points for the player.

3. Attach go to random positionif on edge, bounce, and point in direction 90 to the code.

Collecting Apples

When the snake touches an apple, both the score and length variables will increase. The apple will also disappear, and then reappear in a new location.

1. Add when flag clicked with a forever ↺ block to the code area.

2. Attach an if then <> block with <touching Snake Head?> in the bracket.

3. Inside the if statement, add hide with change length  by 0.1 and change score by 10.

4. Add a broadcast block in the statement with the message "Reset Apple".

Reset the Apple

1. Add when I receive Reset Apple to the code area.

2. Duplicate the go to random position code blocks onto the receive block.3

3. Attach a show block to the code.

And that's it! you should now have a Snake game to play in scratch. Thinking about what the next steps could be? Kids can consider things like adding a "game over" if the snake touches its own body or the map border. 

If they are stuck and need help, consider our many online coding classes for kids or Scratch summer camps

A photo of Ryan

Ryan manages blog content at iD Tech, starting with the company in 2008. He earned his MBA from Santa Clara University after obtaining his Bachelor’s degree from Arizona State. Connect on LinkedIn!

Featured Posts

Categories

Authors

About iD Tech

iD Tech is the #1 tech camp on the planet, and world leader in youth STEM education, with programs held online and at 150+ global locations offering 50+ innovative tech courses: 

Coding camps
Video game camps
Robotics classes & camps
Creative arts classes & camps
All STEM camps

We've bet our reputation on recruiting the top instructors in the country. Our small classes ensure customized learning, leading to "a-ha moments" and awesome outcomes. Programs include:

On-Campus Programs

Online Tutoring

All Coding Courses