Snake looks simple: a line that moves, eats food and grows. Behind it are the ideas from every beginner topic: lists, tuples, loops, if and functions. Here is the core logic, without any graphics, so you can see how it works.
1. THE SNAKE IS A LIST OF POSITIONS
Each body part is a position (x, y) on a grid. The first item is the head.
snake = [(2, 0), (1, 0), (0, 0)]
print("Head:", snake[0])
print("Length:", len(snake))Head: (2, 0)
Length: 3
2. DIRECTIONS ARE SMALL STEPS
DIRS = {"right": (1, 0), "left": (-1, 0), "up": (0, -1), "down": (0, 1)}
dx, dy = DIRS["down"]
x, y = (2, 0)
print((x + dx, y + dy))(2, 1)
3. MOVING: ADD A NEW HEAD, DROP THE TAIL
The trick: the snake does not move every part. It adds a new head in front and removes the last part.
def move(snake, direction):
dx, dy = {"right": (1, 0), "left": (-1, 0), "up": (0, -1), "down": (0, 1)}[direction]
hx, hy = snake[0]
return [(hx + dx, hy + dy)] + snake[:-1]
snake = [(2, 0), (1, 0), (0, 0)]
snake = move(snake, "right")
print(snake)[(3, 0), (2, 0), (1, 0)]
4. EATING: DON'T DROP THE TAIL
When the new head lands on food, keep the tail. That is how the snake grows by one.
def step(snake, direction, food):
dx, dy = {"right": (1, 0), "left": (-1, 0), "up": (0, -1), "down": (0, 1)}[direction]
hx, hy = snake[0]
head = (hx + dx, hy + dy)
if head == food:
return [head] + snake, True
return [head] + snake[:-1], False
snake = [(1, 0), (0, 0)]
snake, ate = step(snake, "right", food=(2, 0))
print(snake, ate)[(2, 0), (1, 0), (0, 0)] True
5. GAME OVER: WALLS AND YOUR OWN TAIL
def crashed(snake, width, height):
x, y = snake[0]
hit_wall = not (0 <= x < width and 0 <= y < height)
hit_self = snake[0] in snake[1:]
return hit_wall or hit_self
print(crashed([(5, 2), (4, 2)], width=5, height=5))
print(crashed([(1, 1), (2, 1), (2, 2), (1, 2), (1, 1)], width=5, height=5))
print(crashed([(2, 2), (1, 2)], width=5, height=5))True
True
False
The first snake left the grid (x = 5 on a 5-wide grid means positions 0 to 4). The second one's head is also in its body.
snake = [(3, 3), (2, 3), (1, 3)]
food = (9, 9)
head = (4, 3)
if head == food:
snake = [head] + snake
else:
snake = [head] + snake[:-1]
print(len(snake))6. PUTTING IT TOGETHER
A real game repeats these steps many times a second: read the arrow key, call step(), check crashed(), place new food, and draw the grid. Libraries like pygame handle the drawing and keys, but the rules are exactly the functions above.
(2, 0) never change once made, so a tuple fits. It also lets you compare them with == and check in easily.PRACTICE IT, DON'T JUST READ IT
Snek Finale is the last world in Snekcade: you build this logic yourself, then play your own Snake game.
PLAY SNEK FINALE ▸FAQ
How does the snake move in a Snake game?
Add a new head one step in the current direction and remove the last tail part. Only two positions change each step.
How does the snake grow?
When the new head lands on the food, the tail is not removed, so the snake becomes one part longer.
How do you detect game over in Snake?
The head is outside the grid (hit a wall) or the head position is already in the body (hit itself).