In this Tutorial, we attempt to make the popular snake game on Scratch
About Snake Game:
The player controls a snake, whose main goal is to eat apple appearing at random position on the screen. The more Apple snake eats, the longer it gets, and the harder it becomes to keep the snake from crushing its own tail. If that happens, Game will be over.
For this game we need 2 sprites.
- Apple: The snake will try to eat this.
- Snake: This is what player control.
Sketchout the Snake Game:
Lets sketch out the snake game.
- Make the Snake Controlled by Arrow Key
- Make Apple appear at random position
- The snake eats the apple and grows
- Make the Snake Teleport
- Add score to the game
- Add Game over Screen
Snake Game Scratch Code
Make the Snake Controlled by Arrow Key
Make sure the snake moves continuously. Using Forever loop achieve constant movement. This is easy to code, when green flag clicked the snake will automatically move 20 steps.
The player uses the arrow keys to control the direction of the snake movement. As the snake is moving on its own, we can change the direction of the snake, by using ‘point in direction’ block from “Motion”.
Make Apple appear at random position
At a random spot, an apple is introduced into the game. This is easy to program. this need only 2 things.
Program your apple sprite so that, if snake touches it, it will hide and go to the random position and appear again.
The snake eats the apple and grows
Program your sprite so that, it should keep track of the points, and the snake must grow in size after eating.
This is perhaps the most complicated to understand.
You can build a clone or multiple clones in no time. The clone was continuously produced every 0.2 seconds. When it start as clone switch costume to body not the head.
Make sure switch the costume to the head at the beginning of the game to resolve the costume problem.
The snake eats an apple and raises its score by 1. yes. there ought to be a clone linked to the snake every time the player gets the point.
The waiting peiod before removing additional clones is same as Total clones multiplied by the speed. Or we can say Speed x score.
Make the Snake Teleport
Make the game more interesting, by letting the snake to teleport. i.e, Snake come from the opposite direction, when it hits the edge of the board. This will make the game more smoother.
Add score to the game
What happens if the snake eats apple?
Every time the snake eats a apple, the score increase by 1.
Add Game over Screen
Game will be over if it touches its own tail. When this happen we need to broadcast a game over message, so that other sprite can act accordingly.