Added game restart option with reload

master
Abhinav Sarkar 2013-10-12 20:35:38 +05:30
parent b1730f4816
commit f18652da6f
1 changed files with 12 additions and 12 deletions

View File

@ -63,11 +63,6 @@
ball-radius 8 ball-radius 8
ball-speed 0.33 ball-speed 0.33
init-pos [(/ width 2) (/ height 2)] init-pos [(/ width 2) (/ height 2)]
init-vel (let [sgn #(if (< % 0.5) -1 1)
deg (+ 35 (* 20 (rand)))
rad (deg->rad deg)]
(map #(* ball-speed %)
[(* (sgn (rand)) (sin rad)) (* (sgn (rand)) (cos rad))]))
paddle-step 20 paddle-step 20
max-paddle-y (- height paddle-size) max-paddle-y (- height paddle-size)
ef-paddle-width (+ paddle-width padding) ef-paddle-width (+ paddle-width padding)
@ -94,12 +89,17 @@
(defn start-game (defn start-game
"Sets up the game by creating the signals and setting up the components and starts the game." "Sets up the game by creating the signals and setting up the components and starts the game."
[] []
(let [frames (frame-chan) ;; frames signal (let [frames (frame-chan) ;; frames signal
pos (chan 1) ;; ball position signal pos (chan 1) ;; ball position signal
vel (chan 1) ;; ball velocity signal vel (chan 1) ;; ball velocity signal
pl-pos (chan 1) ;; paddle left position signal pl-pos (chan 1) ;; paddle left position signal
pr-pos (chan 1) ;; paddle right position signal pr-pos (chan 1) ;; paddle right position signal
game-state (chan 1)] ;; game state signal, the state of the game and the current score game-state (chan 1) ;; game state signal, the state of the game and the current score
init-vel (let [sgn #(if (< % 0.5) -1 1)
deg (+ 35 (* 20 (rand)))
rad (deg->rad deg)]
(map #(* ball-speed %)
[(* (sgn (rand)) (sin rad)) (* (sgn (rand)) (cos rad))]))]
(setup-components frames game-state pos vel pl-pos pr-pos) (setup-components frames game-state pos vel pl-pos pr-pos)
;; start the game by setting the initial values of the signals ;; start the game by setting the initial values of the signals
@ -272,7 +272,7 @@
(dom/set-text! score-el score)) (dom/set-text! score-el score))
(when (= state :gameover) (when (= state :gameover)
(do (dom/set-text! state-el "press <space> to restart") (do (dom/set-text! state-el "press <space> to restart")
(ev/listen-once! :keypress #(.reload (.-location js/window))))) (ev/listen-once! :keypress start-game)))
(doto ball-el (doto ball-el
(dom/set-attr! "cx" x) (dom/set-attr! "cx" x)
(dom/set-attr! "cy" y)) (dom/set-attr! "cy" y))