Removed nested defns and moved top level let bindings to defs
parent
a03a829a5e
commit
f2ed5e513d
|
@ -29,8 +29,6 @@
|
|||
;; | renderer |
|
||||
;; +----------+
|
||||
|
||||
(defn rand [] (.random js/Math))
|
||||
|
||||
(defn abs [x] (.abs js/Math x))
|
||||
|
||||
(def PI 3.141592653589793)
|
||||
|
@ -53,20 +51,18 @@
|
|||
(defn next-pos [[x y] [vel-x vel-y] tick]
|
||||
[(+ x (* vel-x tick)) (+ y (* vel-y tick))])
|
||||
|
||||
(defn ^:export frpong []
|
||||
(let [body-el (.-body js/document)
|
||||
width (- (.-scrollWidth body-el) 20)
|
||||
height (- (.-scrollHeight body-el) 125)
|
||||
padding 5
|
||||
paddle-size 100
|
||||
paddle-width 10
|
||||
ball-radius 8
|
||||
ball-speed 0.33
|
||||
init-pos [(/ width 2) (/ height 2)]
|
||||
paddle-step 20
|
||||
max-paddle-y (- height paddle-size)
|
||||
ef-paddle-width (+ paddle-width padding)
|
||||
init-paddle-pos (/ (- height paddle-size) 2)]
|
||||
(def width (- (.-scrollWidth (.-body js/document)) 20))
|
||||
(def height (- (.-scrollHeight (.-body js/document)) 125))
|
||||
(def padding 5)
|
||||
(def paddle-size 100)
|
||||
(def paddle-width 10)
|
||||
(def ball-radius 8)
|
||||
(def ball-speed 0.33)
|
||||
(def init-pos [(/ width 2) (/ height 2)])
|
||||
(def paddle-step 20)
|
||||
(def max-paddle-y (- height paddle-size))
|
||||
(def ef-paddle-width (+ paddle-width padding))
|
||||
(def init-paddle-pos (/ (- height paddle-size) 2))
|
||||
|
||||
(defn layout-game
|
||||
"Lays out the game screen."
|
||||
|
@ -177,13 +173,6 @@
|
|||
:up (max (- pos paddle-step) 0)
|
||||
:down (min (+ pos paddle-step) max-paddle-y)))))))
|
||||
|
||||
(defn collision-detector [ticks pos vel-in pl-pos pr-pos game-state-in game-state vel-out]
|
||||
"Collision Detector component.
|
||||
Detects the collision of the ball with the walls and the paddles and accordingly calculates
|
||||
and outputs the next ball velocity and next game state to the `vel-out` and `game-state`
|
||||
signals respectively.
|
||||
Reads the current tick, ball position, ball velocity and left and right paddle positions from
|
||||
the `ticks`, `pos`, `vel-in`, `pl-pos` and `pr-pos` signals respectively."
|
||||
(defn in-y-range? [y paddle-y]
|
||||
(and (> y (- paddle-y padding)) (< y (+ paddle-y paddle-size padding))))
|
||||
|
||||
|
@ -213,6 +202,14 @@
|
|||
|
||||
(defn perturb [v] (* v (+ 1 (/ (- (rand) 0.5) 25))))
|
||||
|
||||
(defn collision-detector [ticks pos vel-in pl-pos pr-pos game-state-in game-state vel-out]
|
||||
"Collision Detector component.
|
||||
Detects the collision of the ball with the walls and the paddles and accordingly calculates
|
||||
and outputs the next ball velocity and next game state to the `vel-out` and `game-state`
|
||||
signals respectively.
|
||||
Reads the current tick, ball position, ball velocity, left and right paddle positions and game state
|
||||
from the `ticks`, `pos`, `vel-in`, `pl-pos`, `pr-pos` and `game-state` signals respectively."
|
||||
|
||||
(go-loop
|
||||
(let [;; get all current values
|
||||
tick (<! ticks)
|
||||
|
@ -272,7 +269,7 @@
|
|||
(dom/set-text! score-el score))
|
||||
(when (= state :gameover)
|
||||
(do (dom/set-text! state-el "press <space> to restart")
|
||||
(ev/listen-once! :keypress start-game)))
|
||||
(ev/listen-once! :keypress #(when (= (:keyCode %) 32) (start-game)))))
|
||||
(doto ball-el
|
||||
(dom/set-attr! "cx" x)
|
||||
(dom/set-attr! "cy" y))
|
||||
|
@ -282,6 +279,7 @@
|
|||
(go-loop
|
||||
(dom/set-attr! rpaddle-el "y" (<! pr-pos)))))
|
||||
|
||||
;; Everything is ready now. Layout the game and start it on any keypress!
|
||||
;; Everything is ready now. Layout the game and start it on pressing <space>!
|
||||
(defn ^:export frpong []
|
||||
(layout-game)
|
||||
(ev/listen-once! :keypress start-game)))
|
||||
(ev/listen-once! :keypress #(when (= (:keyCode %) 32) (start-game))))
|
||||
|
|
Loading…
Reference in New Issue