Hi Mark,

I don't think this approach works in Clojure / Swing, but I may be
mistaken,  I often am.

The issue is the event thread.  In Abhishek's original and it's derivatives
the Swing event thread is used and the timer pushes events into it so key
press events and the game timer run in the same thread.  This allowed Stuart
to pass the game into the function containing the JPanel proxy and for
everything to live in the Swing event thread (which is hidden from the
application).  In fact the code should be safe without concurrency
constructs.

Using loop - recur means there are now two threads in the code (The app loop
and Swing event loop) and these must be coordinated.  This is, I think,  why
Mark needed that last global and Stuart didn't.

I don't think there is a way to do this without some mutation because of the
implementation of Swing, but, as I said, I may be wrong.  There will need to
be at least one concurrency construct.

Cheers

Tom

2009/1/6 Mark Engelberg <mark.engelb...@gmail.com>

>
> 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