Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake

An implementation of Snake in Haskell using the Gloss graphics library, originally written in 2022.

Snake game

Status

Archived project originally written in 2022 while learning Haskell.

Implementation

The game is built around Gloss's play interface, with separate modules for rendering, input handling and updating the game state.

Movement

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.

Game state

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.

Rendering and input

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.

Controls

  • Arrow keys — change direction
  • Esc — quit

Running

With GHC and Cabal installed:

$ cabal run

About

An implementation of Snake in Haskell using Gloss

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages