Game Development & Coding
How to Make Snake Game in Scratch & Python: Complete Tutorial
Game Dev Tutorial
9 min read
• Published November 13, 2026
• By SnakeGameX Editorial Team
What You Will Learn in This Coding Guide
- The Core Architecture: Every snake game consists of three components: a grid state, a coordinate queue (FIFO buffer) for body segments, and a discrete game loop timer.
- Scratch Method: Use clone lifespan matching the
Length variable—an elegant, block-based trick requiring zero complex list manipulation.
- Python Method: Build a clean, 60-line Python implementation using the standard
turtle library or pygame for complete keyboard event binding.
Building your own version of Snake is the universal rite of passage for aspiring game developers and software engineers. Because the rules are mathematically clean and visually intuitive, it is the perfect project to learn programming. In this complete guide, you will learn how to make snake game in scratch (using visual blocks) and how to make a snake game in python (using clean, readable code).
Part 1: How to Make a Snake Game in Scratch (Step-by-Step)
MIT Scratch is the world's most popular visual programming platform for beginners. If you want to know how to make the snake game in scratch, the secret lies in a brilliant technique called clone lifetime management.
Step 1: Set Up Your Sprites
- Open Scratch.mit.edu and click Create.
- Delete the default cat sprite.
- Click the paintbrush icon to create a new sprite named Head. Draw a square costume (e.g., 20 × 20 pixels) centered on the canvas.
- Paint a second sprite named Apple. Draw a bright red circular apple (20 × 20 pixels).
Step 2: Create Variables
Click the Variables category and create two variables for all sprites:
Score (tracks total apples eaten)
Speed (controls delay between steps)
Now create one variable for the Head sprite only (select "For this sprite only"):
Clone Duration (determines how many seconds body segments survive before disappearing)
Step 3: Head Movement & Cloning Code
Here is how to structure the Head sprite's blocks to create an endlessly growing snake body without complicated arrays:
WHEN GREEN FLAG CLICKED:
Set Score to 0
Set Clone Duration to 0.4
Go to x: 0 y: 0
Point in direction 90 (Right)
FOREVER:
Move 20 steps
Create clone of [myself]
Wait (0.1) seconds
If <touching [Apple]?> then:
Change Score by 1
Change Clone Duration by 0.1
Broadcast [spawn apple]
If <touching edge?> or <touching color [body color]?> then:
Broadcast [game over]
Stop [all]
WHEN I START AS A CLONE:
Wait (Clone Duration) seconds
Delete this clone
The Genius of Clone Duration
Why does this clone trick work so well in Scratch? Every time the head moves forward by 20 pixels, it leaves behind a clone that sits completely still. When the clone's timer expires, it deletes itself. As Clone Duration increases when you eat apples, clones live longer, creating the illusion of a longer snake slithering smoothly behind you!
Step 4: Steering Controls
Add simple key detection scripts so the player can steer the head with arrow keys:
WHEN [up arrow] KEY PRESSED:
If not <direction = 180> then point in direction 0
WHEN [down arrow] KEY PRESSED:
If not <direction = 0> then point in direction 180
WHEN [left arrow] KEY PRESSED:
If not <direction = 90> then point in direction -90
WHEN [right arrow] KEY PRESSED:
If not <direction = -90> then point in direction 90
Step 5: The Apple Randomizer Script
Attach this script to the Apple sprite:
WHEN GREEN FLAG CLICKED:
Show
Go to x: (pick random -10 to 10 * 20) y: (pick random -8 to 8 * 20)
WHEN I RECEIVE [spawn apple]:
Go to x: (pick random -10 to 10 * 20) y: (pick random -8 to 8 * 20)
Multiplying by 20 guarantees that the apple always snaps perfectly onto your 20-pixel grid, ensuring clean alignment with your snake's head!
Part 2: How to Make a Snake Game in Python
Python is one of the most widely used programming languages in the world. Building a snake game in Python teaches you event listeners, coordinate tuples, list slicing, and game tick loops.
Below is a complete, copy-paste-ready implementation using Python's built-in turtle library. It requires zero third-party installations (no pip install needed) and runs out-of-the-box on Windows, Mac, and Linux:
import turtle
import time
import random
# Game Window Configuration
delay = 0.1
score = 0
high_score = 0
wn = turtle.Screen()
wn.title("SnakeGameX Python Edition")
wn.bgcolor("#0f172a")
wn.setup(width=600, height=600)
wn.tracer(0) # Turns off automatic screen updates for smooth 60fps rendering
# Snake Head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("#10b981")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Snake Food (Apple)
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("#ef4444")
food.penup()
food.goto(0, 100)
segments = []
# Direction Functions with Reverse-Protection
def go_up():
if head.direction != "down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_left():
if head.direction != "right":
head.direction = "left"
def go_right():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
head.sety(head.ycor() + 20)
elif head.direction == "down":
head.sety(head.ycor() - 20)
elif head.direction == "left":
head.setx(head.xcor() - 20)
elif head.direction == "right":
head.setx(head.xcor() + 20)
# Keyboard Bindings
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
# Main Game Loop
while True:
wn.update()
# Border Collision Detection
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(0.5)
head.goto(0, 0)
head.direction = "stop"
for seg in segments:
seg.goto(1000, 1000)
segments.clear()
score = 0
# Apple Collision Detection
if head.distance(food) < 20:
food.goto(random.randint(-14, 14) * 20, random.randint(-14, 14) * 20)
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("#34d399")
new_segment.penup()
segments.append(new_segment)
score += 1
# Move body segments in reverse order
for index in range(len(segments) - 1, 0, -1):
x = segments[index - 1].xcor()
y = segments[index - 1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
segments[0].goto(head.xcor(), head.ycor())
move()
# Self-Collision Detection
for seg in segments:
if seg.distance(head) < 20:
time.sleep(0.5)
head.goto(0, 0)
head.direction = "stop"
for s in segments:
s.goto(1000, 1000)
segments.clear()
score = 0
time.sleep(delay)
Scratch vs Python: Which Should You Learn First?
| Factor |
Scratch Snake Game |
Python Snake Game |
| Prerequisites |
Zero coding knowledge needed |
Basic syntax & variables understanding |
| Development Time |
15 – 30 minutes |
45 – 60 minutes |
| Logic Depth |
Visual clones & broadcast events |
FIFO queues, coordinate arrays, event loops |
| Customizability |
Costumes, sound blocks, web sharing |
AI bots, neural network solvers, custom shaders |
Frequently Asked Questions: Making Snake Games
Why does the snake reverse and kill itself when I press keys too fast?
This is known as the "double-turn bug." If you press Down then Left between a single game tick, the game updates direction twice before executing a single movement step, causing the head to collide with its immediate neck. To solve this in code, you must implement an input buffer queue that processes only one turn per tick.
How can I make the snake move faster as it eats more apples?
In your game loop, reduce the delay or wait duration by a small percentage (e.g. delay = max(0.04, delay * 0.98)) every time an apple is collected.
How does the browser version on SnakeGameX work?
SnakeGameX is built using raw vanilla JavaScript and the HTML5 <canvas> 2D context. It uses requestAnimationFrame for butter-smooth 60 FPS animation, two-step input buffering, and Web Audio API synthesis for zero-asset retro sound effects.
Continue Your Journey
Strategy Guide
Learn the mathematical Hamiltonian algorithms that bots use to clear boards with 100% win rates.
Retro Lore
Discover how Gremlin Industries and Taneli Armanto engineered the original game code.
SX
SnakeGameX Editorial Team
Teaching game design, algorithm optimization, and software development through classic retro arcade classics.