Skip to content

Commit 8998980

Browse files
Merge pull request #32 from MattKleinsmith/let-us-move
Add movement to beginning-to-build-a-browser-game
2 parents 1378b80 + 028a5ed commit 8998980

1 file changed

Lines changed: 34 additions & 38 deletions

File tree

src/games/beginning_to_build_a_browser_game.clj

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,58 @@
77
:tags [:game :browser]}}}
88
(ns games.beginning-to-build-a-browser-game)
99

10-
; Today we are pairing. We are messing around with Clojure.
10+
; I'm new to Clojure. I asked my new friend Timothy how to make a square appear on the screen:
1111

12-
; Instead of HTML tags, we're using Clojure data structures to say what we mean.
13-
; With tags, we need to close them. It's nice to not need to close them.
14-
(def thing [:svg {:style {:border "1px solid red"}}
15-
[:rect {:width 50 :height 50 :stroke :blue}]])
12+
^{:kindly/hide-code true}
13+
^:kind/code
14+
(str "^:kind/hiccup
15+
[:svg [:rect {:width 50 :height 50}]]")
1616

17-
; This tells Clay (our renderer) to render the HTML.
17+
^{:kindly/hide-code true}
1818
^:kind/hiccup
19-
thing
19+
[:svg {:style {:width 50 :height 50}}
20+
[:rect {:width 50 :height 50}]]
2021

21-
^:kind/hiccup
22-
[:div [:button "Click me"] thing]
22+
; Compared to HTML:
2323

24-
; Let's create a function that parametrizes thing
25-
(defn create-thing [x y]
26-
^:kind/hiccup
27-
[:svg {:style {:border "1px solid red"}}
28-
[:rect {:width 50 :height 50 :stroke :blue :x x :y y}]])
29-
(create-thing 50 50)
24+
^{:kindly/hide-code true}
25+
^:kind/code
26+
(str "<svg><rect width=" 50 " height=" 50 " /></svg>")
27+
28+
29+
; It's nice to not have to close tags, but I'm not sure about the colons, nor the dependencies.
3030

31-
; Let's create the world
32-
(def world (atom {}))
31+
; Dependencies aside, we are this square. And we want to be able to move. Timothy, how can I move?
3332

34-
; Let's add something to the world
35-
(swap! world assoc :player {:x 75 :y 75})
33+
; We'll separate the position data from the rendering:
3634

37-
; Wait, did `assoc` work? Yes.
38-
@world
35+
(def world (atom {:player {:x 75 :y 75}}))
3936

40-
(defn render-the-world [world]
37+
(defn render [world]
4138
^:kind/hiccup
42-
[:svg {:style {:border "1px solid red"}}
39+
[:svg {:style {:border "1px solid black"}}
4340
(for [[_entity-id {:keys [x y]}] world]
44-
[:rect {:width 50 :height 50 :stroke :blue :x x :y y}])])
41+
[:rect {:width 50 :height 50 :x x :y y}])])
4542

46-
(render-the-world @world)
43+
(render @world)
4744

48-
; An enemy
45+
; We'll add an enemy to see how to work with `atom`:
4946
(swap! world assoc :enemy {:x 130 :y 75})
47+
(render @world)
5048

51-
; Show the enemy
52-
(render-the-world @world)
53-
54-
; Let's look different from our enemy
55-
(defn render-the-world [world]
49+
; Let us be different shapes:
50+
(defn render [world]
5651
^:kind/hiccup
5752
[:svg {:style {:border "1px solid black"}}
5853
(for [[entity-id {:keys [x y]}] world]
5954
(if (= entity-id :player)
60-
[:rect {:width 50 :height 50 :fill "#62b133" :x x :y y}]
61-
[:circle {:r 25 :fill "#5880d9" :cx x :cy y}]))])
62-
(render-the-world @world)
55+
[:rect {:width 50 :height 50 :fill "#62b133" :x x :y y :id "player"}]
56+
[:circle {:r 25 :fill "#5880d9" :cx x :cy y :id "enemy"}]))])
57+
(render @world)
6358

64-
; It was nice to work with Clojure syntax as opposed to JavaScript.
65-
; The only boilerplate I/we felt was the Clojure colons: ":"
66-
; The code is nice to read. I'm just looking at what I meant to say.
67-
; The things I care about are listed. There they are.
59+
; Now would be a perfect time to use [Reagent](https://reagent-project.github.io/) to connect the user's keyboard to our world atom. However, I couldn't get it working with [Clay](https://scicloj.github.io/clay) (what we're using to render this page). The dependencies have won the first battle.
6860

61+
; Moving on, we'll resort to a `script` element. Clay lets us use JavaScript, but I couldn't get it to work with curly braces. Suffering a one-liner, we can now move, via WASD on our keyboard.
62+
63+
^:kind/hiccup
64+
[:script #"document.addEventListener('keydown', e => (['w', 'W'].includes(e.key) && player.setAttribute('y', parseInt(player.getAttribute('y')) - 10) || ['a', 'A'].includes(e.key) && player.setAttribute('x', parseInt(player.getAttribute('x')) - 10) || ['s', 'S'].includes(e.key) && player.setAttribute('y', parseInt(player.getAttribute('y')) + 10) || ['d', 'D'].includes(e.key) && player.setAttribute('x', parseInt(player.getAttribute('x')) + 10)))"]

0 commit comments

Comments
 (0)