One way to approach this without mutation is as follows:

Decide on a data representation that represents the entire state of
the game world at a given instant.

Write a function which can draw this state.
Write a function which takes the state and player keypresses as input,
and returns a new updated state.
Write a function which takes the state and returns the new state just
from time elapsing.

Note that none of the above functions mutate anything.  It's all about
returning fresh states, which fits well with Clojure's standard
handling of data structures.

Then write a loop that consumes an initial state, and creates a game
experience by repeatedly applying the above functions.  No globals or
refs are required, just keep passing the new states back into the loop
for further processing.

This is an outline of the strategy employed by the "world" teachpack
that accompanies the "How to Design Programs" curriculum that uses PLT
Scheme.  Students routinely develop the snake program as a homework
assignment, using this approach.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to