   :on-render
   (fn [screen entities]
     (clear!)
-    (render! screen (moving entities))) 
+    (render! screen 
+             (->
+               (moving entities)
+               (collision)
+               ))) 
        
   :on-timer
   (fn [screen entities]

  (:require [play-clj.core :refer :all]
            [play-clj.g2d :refer :all]))


(defn move [screen entity]
  (cond
    (= (:key screen) (key-code :dpad-up)) (if (<
                                                (+ (:y entity) 30) 
                                                (- 600 (:height entity)))
                                            (assoc entity :y (+ (:y entity) 30))
                                            (assoc entity :y  (- 600 (:height entity))))              
    (= (:key screen) (key-code :dpad-down)) (if (>
                                                (- (:y entity) 30) 
                                                0)
                                            (assoc entity :y (- (:y entity) 30))
                                            (assoc entity :y 0))         
    (= (:key screen) (key-code :dpad-right)) (if (<
                                                (+ (:x entity) 30) 
                                                (- 800 (:width entity)))
                                            (assoc entity :x (+ (:x entity) 30))
                                            (assoc entity :x  (- 800 (:width entity))))                 
    (= (:key screen) (key-code :dpad-left)) (if (>
                                                (- (:x entity) 30) 
                                                0)
                                            (assoc entity :x (- (:x entity) 30))
                                            (assoc entity :x 0))))



(defn move-boxes [boxes]
  (vec (map move-box boxes)))

(defn has-col [player box]
  (let [left-player (- (:x player) (/ (:width player) 2))
        right-player (+ (:x player) (/ (:width player) 2))        
        left-box (- (:x box) (/ (:width box) 2))
        right-box (+ (:x box) (/ (:width box) 2))        
        up-player (+ (:y player) (/ (:height player) 2))
        down-player (- (:y player) (/ (:height player) 2))        
        up-box (+ (:y box) (/ (:height box) 2))
        down-box (- (:y box) (/ (:height box) 2))]     
    
   
  (or  
  (and (> right-player  left-box) (< down-player up-box)
       (> up-player up-box) (< left-player left-box))
  (and (> right-player left-box) (> up-player down-box)
       (< down-player down-box) (< left-player left-box))  
   (and (< left-player right-box) (> up-player down-box)
        (< down-player down-box) (> right-player right-box))
   
  (and (< left-player right-box) (< down-player up-box)
       (> up-player up-box) (> right-player right-box)))))

(defn has-collide [player boxes]
  (let [collision false
        collision (loop [x (dec (count boxes))]
                    (when (>= x 0)
                      (if (has-col player (get boxes x))
                        true
                        (recur (dec x)))))]
    collision
    ))

(defn moving [entities]    
  (let [backgr (first entities)
        player (last entities)
