       (swap! state update :score + 10)
       (swap! state assoc-in [:obstacles 0 :scored] true))))
 
-(defn game-loop [state end]
+(defn game-loop [state end max-score]
   (let [{:keys [cur-time]} @state
         next-time (js/performance.now)
         delta (- next-time cur-time)]

      (swap! state update :speed-y + (* gravity delta))
      (swap! state update-obstacles)
      (update-score state)
      (when (detect-collision @state) (swap! state assoc :game-over true)))
    (swap! state assoc :cur-time next-time))
  (when-not @end
    (js/requestAnimationFrame #(game-loop state end))))

(defn game-view [state]
  (r/with-let [end (atom false)
               keys [(events/listen js/window "keydown" #(handle-key state %))
                     (events/listen js/window "keyup" #(handle-key-up state %))]
               loop (js/requestAnimationFrame #(game-loop state end))]

    [:div.game
     (when (and (:pause @state) (not (:game-over @state))) [:div.pause "PAUSED"])
     (when (:game-over @state) [:div.game-over "GAME OVER"
                                [:button.restart {:on-click #(reset! state initial-state)} "restart"]])
     [:div.score (:score @state)]
     [:div.sky {:style {:background-position-x (/ (- (:pos-x @state )) 5)}}]
     [:div.road {:style {:background-position-x (- (:pos-x @state ))}}]
     [:div.runner {:class (when (:down @state) "down")

            [mrunner.routes :as routes]))


;; Localstorage methods
(defn set-item!
  "Set `key' in browser's localStorage to `val`."
  [key val]
  (.setItem (.-localStorage js/window) key val))

(defn get-item
  "Returns value of `key' from browser's localStorage."
  [key]
  (.getItem (.-localStorage js/window) key))


;;
;;  Application state
;;  =================
