diff --git a/resources/public/index.html b/resources/public/index.html index c7da4a4..1148e16 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -13,9 +13,6 @@ body { #title { margin: 0px; - position: absolute; - width: 98%; - z-index: -1; } #ball { @@ -38,6 +35,7 @@ body { #canvas { border: 2px green solid; + margin-top: 3px; } #score { @@ -45,20 +43,25 @@ body { text-anchor: middle; dominant-baseline: central; } + +#msg { + position: absolute; + width: 98%; + top: 40px; + font-size: 1.2em; +} + +#msg .imp { + font-size: xx-large; + font-weight: bold; +}

Gravity Pong!

-
- Gravity - 0 10 - - - press <space> to start - -
+
0 @@ -70,7 +73,7 @@ body {
fps 0
- use W-S keys to move the left paddle and Up-Down arrow keys to move the right paddle + use W-S and Up-Down keys to move the paddles / use Left-Right keys to change gravity
Source
diff --git a/src/cljs/frpong/core.cljs b/src/cljs/frpong/core.cljs index bb50b77..339ae51 100644 --- a/src/cljs/frpong/core.cljs +++ b/src/cljs/frpong/core.cljs @@ -50,6 +50,11 @@ (close! c)))) c)) +(defn flash [el msgs millis] + (when (not-empty msgs) + (do (dom/set-text! el (first msgs)) + (.setTimeout js/window #(flash el (rest msgs) millis) millis)))) + ;; Global settings (def *width* (- (.-scrollWidth (.-body js/document)) 20)) (def *height* (- (.-scrollHeight (.-body js/document)) 130)) @@ -60,7 +65,7 @@ (def *ball-radius* 8) (def *ball-speed* 0.6) (def *init-vel-deg-lim* [35 55]) -(def *perturb-factor* 0.02) +(def *perturb-factor* 0.05) (def *init-mass-radius* 0) @@ -71,16 +76,19 @@ (def *init-paddle-pos* (/ (- *height* *paddle-size*) 2)) (def *gravity* (atom 0.005)) +(def *gravity-step* 0.005) (defn mass-radius [] (+ *init-mass-radius* (* (deref *gravity*) 1000))) -;; listen for changes in the gravity input slider and set the atom value accordingly -(ev/listen! (dom/by-id "gravity") :change - #(let [val (* (int (dom/value (dom/by-id "gravity"))) 0.01)] - (reset! *gravity* val) - (dom/set-attr! (dom/by-id "mass") "r" (mass-radius)) - (.blur (dom/by-id "gravity")))) +(defn setup-gravity-control [] + (let [keydowns (first (event-chan :keydown)) + actions { 37 #(- % *gravity-step*) 39 #(+ % *gravity-step*) }] + (go-loop + (let [k (:keyCode ( to restart") - (dom/set-text! title-el "GAME OVER") + (dom/set-html! msg-el "GAME OVER
press <space> to restart") (start-on-space))) (recur fps score)))))) ;; Everything is ready now. Layout the game and start it on pressing ! (defn ^:export frpong [] + (setup-gravity-control) (layout-game) + (dom/set-text! (dom/by-id "msg") "press to start") (start-on-space)) diff --git a/src/cljs/frpong/helpers.cljs b/src/cljs/frpong/helpers.cljs index 2e018e0..d9b3ef7 100644 --- a/src/cljs/frpong/helpers.cljs +++ b/src/cljs/frpong/helpers.cljs @@ -128,13 +128,13 @@ (defn event-chan ([event-type] - (let [c (chan)] - (ev/listen! ev/root-element event-type #(put! c %)) - [c #(do (ev/unlisten! ev/root-element event-type) (close! c))])) + (let [c (chan) + [lkey] (ev/listen! event-type #(put! c %))] + [c #(do (ev/unlisten-by-key! lkey) (close! c))])) ([node event-type] - (let [c (chan)] - (ev/listen! node event-type #(put! c %)) - [c #(do (ev/unlisten! node event-type) (close! c))]))) + (let [c (chan) + [lkey] (ev/listen! node event-type #(put! c %))] + [c #(do (ev/unlisten-by-key! lkey) (close! c))]))) (defn key-chan [keydowns keyups sampler keycodes] (let [c (chan)