You want to think more abstractly. What are the common elements in your
code?

For instance, a naive first pass:

(defn move [subject delta]
  (update-in subject [:position] v+ delta))

(defn key-held [key]
  (@*keys-held* :left))

(defmacro on [subject event action]
  `(if ~event (-> ~subject ~action))

Then you could write:

  (-> g
    (on (key-held :left) (move [-10 0]))
    (on (key-held :right) (move [10 0]))
    (on (key-held :up) (move [0 -10]))
    (on (key-held :down) (move [0 10]))

But if you're designing a game, you usually want to separate the event loop
from the event handling. That's a big topic though, and I can't really go
into much detail in a single post.

- James



On 11 February 2013 20:10, JvJ <kfjwhee...@gmail.com> wrote:

> I'm writing a simple game engine in Clojure, and each game object supplies
> its own unique update function, which takes the original object (a map of
> properties) and returns an updated version.  However, writing the updates
> is somewhat cumbersome because each line of code has to return either the
> original or updated object.  I'd like to see if I can clean up this code,
> possibly by using monads (which I don't understand very well).  Does anyone
> have any advice?  Thanks (Code examples below)
>
> The pseudocode for what i want to do looks something like this:
>
> if left key is held
>    g.position += [-10 0]
> if right key is held
>    g.position += [10 0]
> if up key is held
>    g.position += [0 -10]
> if down key is held
>    g.position += [0 10]
> if q is pressed
>    fire event {:type :dialogue, :text "Hello"}
> if space is pressed
>    g.switchstate(:s2)
>
>
> But the code I ended up writing is this mess:
>
>
> (fn [g]
>                   (-> g
>                       (#(if (@*keys-held* :left)
>                           (update-in % [:position] v+ [-10 0])
>                           %))
>                       (#(if (@*keys-held* :right)
>                           (update-in % [:position] v+ [10 0]) %))
>                       (#(if (@*keys-held* :up)
>                           (update-in % [:position] v+ [0 -10]) %))
>                       (#(if (@*keys-held* :down)
>                           (update-in % [:position] v+ [0 10]) %))
>                       (#(if (@*keys-pressed* \q)
>                           (do (fire-event {:type :dialogue
>                                            :text "Hello!"})
>                               %)
>                           %))
>                       (#(if (@*keys-pressed* :space)
>                           (do (comment (println "spaced!"))
>                               (switch-state % :s2)) %))))
>
> --
> --
> 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
> Note that posts from new members are moderated - please be patient with
> your first post.
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to