Skip to content

Commit 3873b45

Browse files
Merge pull request #30 from MattKleinsmith/beginning-to-build-a-browser-game
beginning-to-build-a-browser-game
2 parents bbc6bc7 + cbb273f commit 3873b45

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

site/db.edn

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
:email "timothypratley@gmail.com"
4444
:affiliation [:hummi]
4545
:links [{:icon "github" :href "https://github.com/timothypratley"}]}
46+
{:id :mattkleinsmith
47+
:name "Matt Kleinsmith"
48+
:url "https://github.com/mattkleinsmith"
49+
:image "https://avatars.githubusercontent.com/u/8968171?v=4"
50+
:email "mwksmith@gmail.com"
51+
:affiliation [:clojurecamp]}
4652
{:id :harold
4753
:name "Harold"
4854
:url "https://techascent.com"
@@ -138,8 +144,7 @@
138144
:desc "Conferences and meetups"}]
139145

140146
:notebook
141-
[
142-
;; ===== CORE =====
147+
[;; ===== CORE =====
143148
{:id "clj-docs"
144149
:title "Official Clojure Documentation"
145150
:url "https://clojure.org/guides/getting_started"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)