How to playWrite a Python function move(head, food, body, walls, size) that returns "up", "down", "left" or "right". Press Run: your function is called once per move and the snake follows it. Eat the target number of apples without hitting a wall, the edge or your own tail to clear the level.
How to play
- Read the level goal. Each level says how many apples to eat and what's on the board.
- Edit move(). Your function gets
head(x, y),food(x, y), thebodylist, thewallsset and the boardsize, and returns a direction. - Run the bot. The game calls move() for every step and animates the result. Crashes show exactly where and why.
- Beat all five levels. Use the helpers
step(pos, direction)andis_free(pos)to avoid walls and your own tail.
What move() receives
| Parameter | Example | Meaning |
|---|---|---|
head | (5, 3) | Column and row of the snake's head. x grows to the right, y grows downwards. |
food | (8, 3) | Where the apple is. |
body | [(5, 3), (4, 3), (3, 3)] | Every cell of the snake, head first. |
walls | {(6, 2), (6, 3)} | A set of blocked cells. |
size | 12 | The board is size × size; valid x and y are 0 … size−1. |
Two helpers are ready to use: step(pos, "left") returns the cell you'd move into, and is_free(pos) is True if that cell is on the board and not a wall or part of the snake.
From the starter to a winning bot
The starter heads straight for the apple, which clears the first two levels — then it crashes into the wall on level 3. The next idea is to only consider directions that are free and pick the one that gets closest to the food:
Run ▸def move(head, food, body, walls, size):
best = None
for d in ("up", "down", "left", "right"):
nxt = step(head, d)
if is_free(nxt):
dist = abs(nxt[0] - food[0]) + abs(nxt[1] - food[1])
if best is None or dist < best[0]:
best = (dist, d)
return best[1] if best else "up"
That greedy bot gets much further, but on the last level a long snake can still trap itself in a dead end. The winning idea is to look ahead: before taking a move, count how many cells you could still reach from there (a flood fill) and avoid moves that leave less room than your body needs. That's a real algorithm used in pathfinding and game AI — and it's only about fifteen lines of Python.
What you're practising
- Functions that return a value
- Tuples and coordinates
- if/elif decisions and loops over options
- Sets for fast "is this blocked?" checks
- Breaking a big problem into helpers
New to these? Warm up with the function exercises and dictionary exercises first.
Frequently asked questions
Is Snake Bot a real Python game?
Yes. Your code runs in a real Python 3 interpreter in the browser; the game calls your move() function on every step.
Why does my snake crash into itself?
Moving towards the food can lead into your own tail. Check each option with is_free(step(head, d)) before choosing it.
My bot goes round in circles — what happened?
If it never reaches the food it runs out of moves. Usually two directions keep looking equally good; add a tie-breaker or prefer moves that keep the most free space.
Can I make a snake game in Python myself?
Yes — with pygame or turtle on your computer. This game lets you focus on the AI logic without the graphics code.
Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.