ClojureScript and Pixi - An Experiment


Spent a little time derping with Pixi and ClojureScript today, following Goodboy's Getting Started tutorial. Yay spinning bunnies! (Please forgive the 4 frames-per-second gif.)

Spinning bunnies woo!

The relevant codes:

(ns pixicljs.core
  (:require [pixi.core :as p]))

(def game-state (atom {:entities {:bunny {:texture "resources/bunny.png"
                                          :position {:x 200 :y 150}
                                          :anchor {:x 0.5 :y 0.5}
                                          :rotation 0}
                                  :bunny-two {:texture "resources/bunny.png"
                                              :position {:x 150 :y 150}
                                              :anchor {:x 0.5 :y 0.5}
                                              :rotation 0.5}}}))

(defn update-state [state time]

  (letfn [(rot [by s] (+ by s))]
    (-> state
        (update-in [:entities :bunny :rotation] (partial rot 0.1))
        (update-in [:entities :bunny-two :rotation] (partial rot 0.2)))))

(defn main []
    (p/start! game-state update-state :width 500 :height 400))

Wrangling the (unfamiliar) stateful API into something vaguely functional didn't result in particularly pretty code (compounded by the fact that I'm still very much finding my feet with Clojure/ClojureScript). But that's okay! It's a learning process.

Source is available on GitHub.