An implementation of Snake in Haskell using the Gloss graphics library, originally written in 2022.
Archived project originally written in 2022 while learning Haskell.
The game is built around Gloss's play interface, with separate modules for rendering, input handling and updating the game state.
Rather than representing the snake only by integer grid coordinates, each segment stores an exact Rational position together with its direction of travel:
data SnakeSegment = SnakeSegment
{ segPos :: RPoint
, segDir :: Direction
}The snake moves continuously between grid cells in quarter-cell increments. Direction changes are queued and applied when the head reaches the next cell, keeping turns aligned with the grid.
The world state keeps track of the snake, apples, score, queued turn, and whether the game is still running. Each update advances the snake and handles food collection and collisions.
Food positions are generated using a random-number generator stored as part of the world state.
Gloss is used to render the board, snake and food and to run the game loop. Keyboard events are handled separately from the game update logic.
- Arrow keys — change direction
Esc— quit
With GHC and Cabal installed:
$ cabal run