How to make a snake game in Python, Scratch or JavaScript
Snake is the classic first coding project because the whole game is a grid and a list. The head moves one cell per tick, the tail follows, food makes it longer, and hitting a wall or yourself ends the run. Learn that logic once and it works in any language.
Checked . Roam is our app, and the Roam team wrote this page. See also: Scratch games to make, Create a game without coding.
How Snake works
The idea goes back to Blockade, a 1976 arcade game from Gremlin Industries, and Snake became famous as a game built into Nokia phones in the late 1990s. Every version uses the same parts.
- The grid. The board is a grid of cells, for example 20 by 20. Everything moves one cell per tick.
- The body is a list. Each tick, add a new head in the direction of travel and remove the last tail piece. If the snake just ate, skip the removal and it grows.
- Food. Place it on a random empty cell, never under the snake.
- Collision. Game over if the head hits a wall or any part of the body. Speed up the tick a little every few apples.
Code it in Python
- 1. Set up. Install Python and the pygame library with pip install pygame. Python's built-in turtle module also works for a simpler version.
- 2. Game loop. Each frame: read keys, move the snake, check collisions, draw, then wait with a clock tick.
- 3. Direction. Arrow keys change direction, but ignore a key that would reverse the snake into itself.
- 4. Draw and score. Draw each body cell and the food as squares, and show the score at the top.
- 5. Game over. Show the score and restart on a key press. Fast restarts keep people playing.
The same game in Scratch or HTML
- Scratch. Make a head sprite that moves a fixed number of steps on a timer and turns with the arrow keys. Each step it leaves a clone as a tail piece that deletes itself after a wait tied to the score, so a higher score means a longer tail. Touching a clone or the edge stops the game.
- JavaScript and HTML. Draw squares on a canvas element, keep the body as an array of x and y cells, listen for keydown events and move the snake on a timer with setInterval.
- Engines. Android Studio, GameMaker, Unity and Godot tutorials use the same list, tick and collision steps.
Snake without code
If you want the game more than the coding exercise, describe it to Roam, an iPhone app: arena, theme, what the snake eats. It comes back playable, and you can play it with friends. The no-code snake game maker page has twists to ask for.
Play it on Roam
Create your snake game with Roam
Pick the arena and the twist. Play it on your iPhone, then say what to change.
Try: "neon snake arena, eat glowing orbs to grow, boost costs length, my friends are the other snakes"Roam ("Roam - Make Games" on the App Store) is an iPhone app where you play games and make your own by describing them or from a photo. It is free to download and play, and making games works without a subscription; optional Spark and Reality plans and credit packs add more creation. Android: pre-register on Google Play.
Questions people ask
How do you create a snake game using Python?
Install pygame, make a grid, keep the snake as a list of cells and run a loop that reads keys, moves the head, checks for walls and the tail, and redraws. A beginner following a tutorial can finish a basic version in an afternoon.
What is the code for the snake game?
There is no single code. Every version has a list of body cells, a direction, a food cell and a loop that moves, checks collisions and redraws. The language only changes how you write those parts.
How do you make a snake game in HTML?
Use a canvas element and JavaScript. Store the snake as an array, change direction on keydown, move it with setInterval and redraw the canvas each tick.
What is the best language to code a snake game?
Python with pygame is the most common choice for beginners. JavaScript with an HTML canvas is a close second, and Scratch works with blocks instead of text.
Sources, checked November 5, 2026
- The snake genre began with Blockade (Gremlin Industries, 1976); Snake came built into Nokia phones in the late 1990s: en.wikipedia.org
- pygame is a free Python library for making games: pygame.org
Product names belong to their owners. Roam is not affiliated with them.
Keep exploring
- Make a snake game
- How to make a small game
- How to make a 2D game
- How to make a survival game
- How to make a farming game
- How to make a match 3 game
More on Roam: All Roam games Games to play, by kind


