← All projects

04 / Nand2Tetris

Snake in Jack

I wrote this Snake game in Jack during Nand2Tetris. It handles keyboard input, movement, food, collisions, and scoring using the course’s platform APIs.

Jack · Hack platformCourse project
View repository

The problem

A small game still needs a coherent update cycle: read input, update position, detect collisions, change the score, and draw the next state. Jack makes these steps unusually easy to inspect.

How it is structured

The Game class coordinates keyboard input, movement, collision checks, food placement, scoring, and timing. The Snake class manages direction and body coordinates, then draws through the platform’s Screen API.

Input

Read a key and update direction.

State

Move, check collisions, update score.

Output

Draw using the platform’s Screen API.

Changing direction

Direction changes depend on both the pressed key and the current direction. Moving the snake shifts coordinate arrays and advances the head, keeping the body representation explicit.

The game loop

The game loop polls Keyboard.keyPressed, updates the screen and snake position, checks collisions, and waits before continuing. Rectangle-overlap checks handle food and self-collision.

Current capabilities

  • Keyboard-controlled direction and coordinate-based snake movement.
  • Food collision, snake growth, and score updates.
  • Drawing and timing through the Nand2Tetris operating-system APIs.

Scope & limitations

This older project uses the Jack/Hack toolchain; it doesn’t run in the browser. It hasn’t been rerun for this write-up. Food placement uses a value from the game loop rather than a general random-number generator.

A detail worth looking at

The movement code uses coordinate arrays for the snake’s body. Following how those arrays change from one frame to the next is a useful way to read the project.

Explore the source