A Clojure Snake

The Snake game features a player-controlled snake that moves around a game grid hunting for an apple. When your snake eats an apple, it grows longer by a segment, and a new apple appears. If your snake reaches a certain length, you win. But if your snake crosses over its own body, you lose.

Before you start building your own snake, take a minute to try the completed version. From the book’s REPL, enter the following:

 ​(use ​'examples.snake​)​
 ​​
 ​(game)​
 ​-> [#<Ref clojure.lang.Ref@65694ee6>​
 ​#<Ref clojure.lang.Ref@261ae209>​
 ​#<Timer javax.swing.Timer@7f0df737>]​

Select the Snake window, and use the arrow keys to control your snake.

Our design for the snake is going to take advantage of Clojure’s functional nature and its ...

Get Programming Clojure, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.