Hi,

That was written several months ago, against a very different version
of Clojure.

Below is a working port, against a current Clojure (rv 1185), on
OpenJDK (1.6.0_0-b11) on GNU/Linux.  The original web page will also
be updated.

(ns snake (:import (java.awt.event KeyEvent ActionListener KeyListener)
                   (javax.swing JPanel JFrame Timer)))

(defn gen-apple [] {:x (int (* 790 (Math/random))) :y (int (* 590
(Math/random)))})

(def snake (ref {:body (list {:x 10 :y 10}) :dir {:x 10 :y 0}}))
(def apple (ref (gen-apple)))
(def dirs {KeyEvent/VK_LEFT {:x -10 :y 0} KeyEvent/VK_RIGHT {:x 10 :y 0}
           KeyEvent/VK_UP   {:x 0 :y -10} KeyEvent/VK_DOWN  {:x 0 :y 10}})

(defn move [{body :body dir :dir :as snake} & opts]
  (merge snake {:body (cons {:x (+ (:x dir) (:x (first body)))
                             :y (+ (:y dir) (:y (first body)))}
                            (if (:grow (apply hash-map opts)) body
(butlast body)))}))

(defn turn [snake newdir] (if newdir {:body (:body snake) :dir newdir} snake))

(defn collision? [{[b & bs] :body} a]
  (and (>= (+ 10 (:x b)) (:x a)) (<= (:x b) (+ 10 (:x a)))
       (>= (+ 10 (:y b)) (:y a)) (<= (:y b) (+ 10 (:y a)))))

(defn run-snake []
  (let [panel (proxy [JPanel ActionListener KeyListener] []
                (paintComponent [g] (proxy-super paintComponent g)
                                    (.fillRect g (:x @apple) (:y @apple) 10 10)
                                    (doseq [p (:body @snake)]
                                      (.fillRect g (:x p) (:y p) 10 10))
                                    (if (collision? @snake @apple)
                                      (dosync (ref-set apple (gen-apple))
                                              (alter snake move :grow true))))
                (actionPerformed [e] (dosync (alter snake move))
                                     (. this (repaint)))
                (keyPressed [e] (dosync (alter snake turn (get dirs
(.getKeyCode e))))))]
    (doto panel (.setFocusable true) (.addKeyListener panel))
    (doto (new JFrame "Snake") (.add panel) (.setSize 800 600)
(.setVisible true))
    (.start (new Timer 75 panel))))


Cheers,
Abhishek

On 12/24/08, wubbie <sunj...@gmail.com> wrote:
>
> Hi,
> I ran it with the changes and got the message:
> java.lang.IllegalArgumentException: Don't know how to create ISeq
> from: Symbol (NO_SOURCE_FILE:22)
>
> What can be the problem?
> thanks
> sun
>
>
>
> On Dec 23, 6:08 pm, "Stephen C. Gilardi" <squee...@mac.com> wrote:
>> On Dec 23, 2008, at 5:19 PM, MattyDub wrote:
>>
>> > On the Java Interop page (http://clojure.org/java_interop), the proxy
>> > macro is described as creating "...a instance of a proxy class that
>> > implements the named class/interface(s)...".  Is this done using Java
>> > Dynamic Proxy Classes (http://java.sun.com/j2se/1.5.0/docs/guide/
>> > reflection/proxy.html)?
>>
>> That seems likely but I don't know for sure. The implementation is in
>> the Clojure distribution in src/clj/clojure/core_proxy.clj and
>> supporting classes in src/jvm/clojure/lang.
>>
>> >    Background to this question: I came across a blog post (http://
>> >www.plt1.com/1070/even-smaller-snake/) which claims to implement the
>> > game "Snake" in 35 lines of clojure.  However, it doesn't run; for one
>> > thing, in line 26, the syntax for doseq is wrong.  Another problem has
>> > to do with calling methods from the JPanel class on the proxied JPanel
>> > object created in the run-snake function.  It looks like you shouldn't
>> > call methods on proxied classes that aren't the ones you supplied when
>> > creating the proxy - is that true?
>>
>> It's not true. In addition to the update to doseq's syntax, the code
>> there also needs an update to the new "doto" syntax. Now, when calling
>> methods on a Java object instance inside a "doto" form, the method
>> names need to be prefixed with ".":
>>
>>      (doto panel (.setFocusable true) (.addKeyListener panel))
>>      (doto (new JFrame "Snake") (.add panel) (.setSize 800 600)
>> (.setVisible true))
>>
>> The code in the blog post worked for me with today's Clojure after I
>> made those two updates.
>>
>> --Steve
>>
>>  smime.p7s
>> 3KViewDownload
> >
>


-- 
Abhishek Reddy
http://abhishek.geek.nz

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