These instructions will give you a very basic version of the classic snake game and a few ideas as to how you might improve upon it. The tough part of this game is making it so that all of the pieces of the snake move in the correct way.
The snake starts with a red head and a blue body part. Click on the image to view a larger version. The sprites are all 8x8 pixels to give plenty of space for playing the game.
The next function increases the number of body parts by 1. Click on the image to view a larger version.
This code gets the game started.
The snake moves a set amount on each game update. If you change how often the game updates, you can make the snake move more quickly or more slowly. Click on the image to view a larger version.
In some snake games, the screen is a hard border and you die if you touch it. In this version, we can make the snake appear on the opposite side of the screen when it hits an edge.
The dx and dy variables are the number of pixels that the snake should move each game update. By changing these on button presses, we can give the player some control over the way that the snake moves.
Click on the image to view a larger version.
In the following function, othersprite is not a normal variable. Drag it from the function definition to place it where you need it.
This code causes the food to respawn when it has been eaten.
The snake dies if its head touches its body.
Challenges & Extensions
- The snake sprites are very basic. Keeping them at 8x8, redesign them so that they are a little more interesting.
- Make 4 different images for the snake's head, one facing in each of the four directions in which the snake can move. When the player presses a key, change the snake's head sprite to the one pointing in the new direction of movement.
- Add a tail piece for the snake. When adding a new body part, first change the last body part in the list to a normal snake body, then add the new body part with a tail image.
- Remove the code that does screen wrapping and use a tile map (with 8x8 tiles) to design a border for the game. When the snake hits one of the outer walls, have it die.
- When the food spawns, it does so in a random position. The snake moves a set amount each time. This effectively means that it is travelling on a grid. The food does not always line up nicely with the 'grid'. Work out how to generate a random position on the grid on which the snake travels.
- Have the game speed vary. If you set a variable when the game starts, you could place this in the game update block where it currently says 500ms. You could remove 5 from the number each time a piece of food is eaten. Alternatively, add a second extra variable to keep score. When the score reaches a number divisible by 10, change the update frequency (use remainders to work this out).
- Throw a little poison into the game for the snake to avoid.
- Make this a game of many levels. Use tile maps to make interesting shapes that the snake must travel around. You could have some openings on the outside for the snake to wrap across the screen and some barriers in the middle for it to avoid. You could have a tunnel of some sort in a higher level.
- One way to play the game would be to have the player grow the snake to a certain length before having it shrink back down and the game become more difficult.
- Add portals to the game. Have the snake enter one portal and exit another.