Fox Studios logo Fox StudiosGame projects, devlogs and Scratch builds
Projects

How We Built a Snake Game in Scratch Using Clones

Most Snake tutorials for Scratch stop working the moment the snake gets longer than about ten segments: the tail drifts, the head hits itself when it should not, and the game speeds up in jumps.

A laptop on a classroom table showing a green block snake game on a grid while two kids watch and point at the screen

Most Snake tutorials for Scratch stop working the moment the snake gets longer than about ten segments: the tail drifts, the head hits itself when it should not, and the game speeds up in jumps. We built a snake game in Scratch over two evenings for our Saturday club, rebuilt it once after it broke, and this is the version that held up when 14 kids played it for an hour straight.

Below is how the project is structured, the two bugs that cost us the most time, and the one piece of popular advice we decided to ignore.

The plan for our snake game in Scratch

We gave ourselves a short brief before opening the editor, the same habit we describe in why your first game should be tiny:

  • Grid based movement, not smooth gliding, so the game feels like the classic
  • Arrow key controls with no reversing into yourself
  • One food item at a time, placed on a free cell
  • Score in the top corner and a speed that ramps gently
  • Game over on wall or self collision, with a restart on the space bar

Anything else, like power ups or two player mode, went on a "later" list that we never touched. That list is why the project finished.

Setting up the grid

The Scratch stage is 480 by 360 pixels. We chose a 20 pixel cell, which gives a grid of 24 columns by 18 rows. Every sprite in the game is a 20 by 20 square costume, centered on its costume center, so positions line up with the grid without rounding.

Scratch coordinates run from -240 to 240 horizontally and -180 to 180 vertically. To place something on a cell, we pick a column from 0 to 23 and a row from 0 to 17 and convert:

  • x = (column times 20) minus 230
  • y = (row times 20) minus 170

The minus 230 and minus 170 offsets put each square in the middle of its cell instead of on the gridline. Getting this wrong by 10 pixels is the most common reason food looks half off the board.

Using clones as body segments

The head is a normal sprite. The body is made of clones of a second sprite called Segment, and each clone has one "for this sprite only" variable called life.

Each game tick works like this:

  1. The head broadcasts "tick" before it moves.
  2. On "tick", the Segment parent creates a clone at the head's current position, with life set to the current snake length.
  3. Every existing clone subtracts 1 from its life. Any clone with life at 0 deletes itself.
  4. The head moves one cell in its current direction.

That is the whole trick. The tail never needs to "follow" anything. Old segments simply expire in order, and when the snake eats, we add 1 to the length variable so new clones live one tick longer. The snake grows from the tail end automatically.

The game loop itself is a forever block with a wait at the end. We start at 0.15 seconds per tick and subtract 0.005 every time the snake eats, with a floor of 0.07. Below that, the arrow keys could not keep up on the school laptops.

The two bugs that cost us an evening

Reversing into yourself

Our first version set the direction the instant a key was pressed. Pressing up then left quickly within one tick let the snake turn a full 180 degrees into its own neck. The fix was a second variable. Key presses write to "next direction", and the head only copies it into "direction" at the start of each tick, after checking it is not the exact opposite of the current one.

Food spawning inside the snake

Random placement put food under the body about one time in eight once the snake got long. We added a loop: pick a random cell, move the food there, and if it is touching the Segment sprite, try again. With 432 cells on the board, this almost always succeeds on the first or second try.

Why we skipped "touching color" for collisions

A lot of Scratch advice says to detect self collision with the "touching color" block, because it is simple and fast. We tried it and dropped it.

The problem is anti aliasing. When sprites sit at exact grid positions, edges blend slightly with neighbors, and the head would sometimes report touching the body color while sliding past a segment it was only next to. Changing the body shade, or adding a costume outline, changed the false positive rate in ways we could not predict.

Instead, the head checks "touching Segment" right after it moves, but only once the snake length is 4 or more, since a shorter snake cannot reach itself. Because the newest clone always sits on the cell the head just left, the head is never touching it after moving. Any clone it does touch is a real collision. This check worked every time across a full hour of play testing.

The other common worry is the Scratch clone limit of 300. On a 24 by 18 grid the board fills at 432 cells, so in theory a perfect player could hit the cap. In practice our best player reached a length of 61. If you build a bigger board, track positions in a list instead.

What we would change next time

AreaWhat we didWhat we would try
InputArrow keys onlyAdd WASD for kids sharing a keyboard
SpeedLinear ramp per foodStep up every 5 foods so players feel each level
FeedbackScore number onlyA short sound on eat and a flash on game over
RestartSpace barShort delay so players do not restart by accident

The clone and life pattern carries well into other projects. We used the same timing idea for trail effects in our endless runner built in Scratch over one weekend, and it maps cleanly to a node pool when you move the game to a bigger engine.

If you remix this, start with the grid math and the tick broadcast. Get a single square moving cell to cell before you add a single clone.

Frequently asked questions

How do you make the snake grow in Scratch?

Give each body clone a local life counter set to the current snake length. Every tick, each clone counts down and deletes itself at zero. Adding 1 to the length when the snake eats makes new clones last one tick longer, which grows the tail.

How do you stop the snake from reversing in Scratch?

Store key presses in a separate next direction variable. At the start of each tick, copy it into the real direction only if it is not the opposite of the current direction. This blocks fast double key presses from turning the snake into itself.

Is Snake a good first project in Scratch?

Yes, if you keep the scope small. It teaches grids, game loops, clones and collision in one project that is finished in a few sessions. Skip extras like power ups until the basic loop works perfectly.

DF
Diego Fenn

Diego covers finished builds and the community around them, and runs the feedback threads that shape what the studio makes next.

More posts by Diego

More in Projects

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto