|
| 1 | +^{:kindly/hide-code true |
| 2 | + :clay {:title "Beginning to build a browser game" |
| 3 | + :quarto {:author [:mattkleinsmith] |
| 4 | + :category :clojure |
| 5 | + :tags [:game :browser]}}} |
| 6 | +(ns games.beginning-to-build-a-browser-game) |
| 7 | + |
| 8 | +; Today we are pairing. We are messing around with Clojure. |
| 9 | + |
| 10 | +; Instead of HTML tags, we're using Clojure data structures to say what we mean. |
| 11 | +; With tags, we need to close them. It's nice to not need to close them. |
| 12 | +(def thing [:svg {:style {:border "1px solid red"}} |
| 13 | + [:rect {:width 50 :height 50 :stroke :blue}]]) |
| 14 | + |
| 15 | +; This tells Clay (our renderer) to render the HTML. |
| 16 | +^:kind/hiccup |
| 17 | +thing |
| 18 | + |
| 19 | +^:kind/hiccup |
| 20 | +[:div [:button "Click me"] thing] |
| 21 | + |
| 22 | +; Let's create a function that parametrizes thing |
| 23 | +(defn create-thing [x y] |
| 24 | + ^:kind/hiccup |
| 25 | + [:svg {:style {:border "1px solid red"}} |
| 26 | + [:rect {:width 50 :height 50 :stroke :blue :x x :y y}]]) |
| 27 | +(create-thing 50 50) |
| 28 | + |
| 29 | +; Let's create the world |
| 30 | +(def world (atom {})) |
| 31 | + |
| 32 | +; Let's add something to the world |
| 33 | +(swap! world assoc :player {:x 75 :y 75}) |
| 34 | + |
| 35 | +; Wait, did `assoc` work? Yes. |
| 36 | +@world |
| 37 | + |
| 38 | +(defn render-the-world [world] |
| 39 | + ^:kind/hiccup |
| 40 | + [:svg {:style {:border "1px solid red"}} |
| 41 | + (for [[_entity-id {:keys [x y]}] world] |
| 42 | + [:rect {:width 50 :height 50 :stroke :blue :x x :y y}])]) |
| 43 | + |
| 44 | +(render-the-world @world) |
| 45 | + |
| 46 | +; An enemy |
| 47 | +(swap! world assoc :enemy {:x 130 :y 75}) |
| 48 | + |
| 49 | +; Show the enemy |
| 50 | +(render-the-world @world) |
| 51 | + |
| 52 | +; Let's look different from our enemy |
| 53 | +(defn render-the-world [world] |
| 54 | + ^:kind/hiccup |
| 55 | + [:svg {:style {:border "1px solid black"}} |
| 56 | + (for [[entity-id {:keys [x y]}] world] |
| 57 | + (if (= entity-id :player) |
| 58 | + [:rect {:width 50 :height 50 :fill "#62b133" :x x :y y}] |
| 59 | + [:circle {:r 25 :fill "#5880d9" :cx x :cy y}]))]) |
| 60 | +(render-the-world @world) |
| 61 | + |
| 62 | +; It was nice to work with Clojure syntax as opposed to JavaScript. |
| 63 | +; The only boilerplate I/we felt was the Clojure colons: ":" |
| 64 | +; The code is nice to read. I'm just looking at what I meant to say. |
| 65 | +; The things I care about are listed. There they are. |
| 66 | + |
0 commit comments