To program this game, we will use the Scratch platform which has the advantage of using visual code: no need to learn a programming language to build your own games and videos !
Preparation time for facilitator
Time needed to complete activity (for learner)
Name of author
Support material needed for training
Resource originally created in
We will program a game in which a snake is controlled and the goal is to eat apples. Every time that a fruit is eaten, the snake grows. If the head of the snake touches the body or the edge of the board, the game ends.
Choosing a backdrop
- On the homepage, click on ‘Start Creating’
- Click on the button in the bottom right corner to create a backdrop.
- Choose ‘Neon Tunnel’
Making the Snake
- Click on the ‘create a new sprite’ icon, then ‘Paint’. Name the sprite ‘Snake’. Delete the cat.
- Start with the body. Draw a black circle and fill it with yellow. Have it so that the size of the body doesn’t pass 30 pixels. For this, look at the costume icon on the top left of the screen: if you shrink or enlarge the drawing, the pixel values change. In this example, the body is 25×25. Name the costume ‘body’.
- Place the sprite in the centre of the stage
- Continue by making the head. Duplicate the ‘body’ costume and name the new one ‘head’. Draw a nose and eyes. Make sure the nose stays within the boundaries of the circle.
Moving the Snake
- Create a variable ‘size’ corresponding to the size of the body and the head – in our example. 25 pixels.
- The snake should be constantly moving. Reproduce the script below. Here’s what the code does: at the beginning of the game, the snake will be placed at the centre and will move towards the right. It will always be placed on the front layer and start with the head costume. We set the initial size variable to 25 as the size of the head is 25×25. It will constantly move in intervals of its own size.
When we touch a directional arrow, the snake should change direction. Copy the 4 scripts below. Explanation: when a directional arrow is pressed (up, down, left, right), the head moves in the corresponding direction.
- Test the game. Problem: the snake moves too fast. To slow it down, create a variable ‘delay’ and change the movement script by adding two corresponding blocks as shown below. Here, he set ‘delay’ to 0.2 and between each movement of the snake we want this amount of time in terms of seconds. The larger the amount of time, the slower the snake.
- Create a variable ‘length’ that will represent the length of the body
- How do we control the movement of the snake? Hint: every time the head moves forward, a clone of the body has to be created and placed in the space preceding the head. The clone remains there the time equivalent to the length multiplied by the movement delay. Thus, the longer the body, the longer the clone remains. This is how the illusion of the body is created. Add the following blocks to the movement script. Here, we are setting the initial length of the snake as 1. Just before it moves, a clone is created.
- Add the clone script (body). Explanation: we switch to the ‘body’ costume. The clone stays for a duration equivalent to the length o the snake multiplied by the movement delay in seconds and is then deleted.
- Create a new sprite, choosing ‘Apple’ from the library. Name it ‘apple’.
- Apples should appear randomly. When the snake swallows it, it disappears and a new one appears. Every time an apple is swallowed, the length of the snake increases by 1. Copy the below script. Explanation: first the apple is hidden. The size of the original is reduced by 30%. The following sequence is repeated indefinitely: the apple is placed randomly on the screen; it appears until touched by the snake head; a sound effect; 1 is added to the length of the snake; the apple disappears.
- Problem: the apple can appear on the snake (or behind it). To avoid an apple appearing randomly on the snake, add a condition test after the apple has been placed. Below is the complete script for the apple. Explanation: the apples positioning keeps moving as long as the colour yellow (the colour of the snake) is touched – or until the colour yellow is no longer touched, which amounts to the same.
Ending the game
The game ends when the head of the snake touches the edge of the stage or when it touches its own body. Change the snake’s script as follows:
Explanation of code: after the snake moves, the game tests whether the edge or the yellow colour is touched. If either of these is the case, the crash cymbal sound effect is played and all scripts are stopped. Note that here we use the colour ‘black’ is this is the colour we chose for the outline of the snake. Note also that you need to match the colour coordinates exactly with the ones you used for the costumes.
Possible improvements to the game:
- Add scoring
- Increase the snake’s speed
- Place more apples
- Add enemies/poison apples