on master help sign in
Update Pong source to match blog post.
commitcf011a0ba8c601c7a7baabdf04d56c5665d84950 authorevancz <info@elm-lang.org> dateFri 24 Aug 2012, 13:25 (14 years ago) parent5547fb1
1 file changed+8−6
Examples/elm-js/Pong/Pong.elm+8−6
Examples/elm-js/Pong/Pong.elm+8−6
@@ -236,13 +236,15 @@ makePositive n = if n > 0 then n else 0-n
236236makeNegative n = if n > 0 then 0-n else n
237237within epsilon n x = x > n - epsilon && x < n + epsilon
238238
239+stepVelocity velocity lowerCollision upperCollision =
240+ if lowerCollision then makePositive velocity else
241+ if upperCollision then makeNegative velocity else velocity
242+
239243stepBall delta (Ball (x,y) (vx,vy)) (Paddle y1) (Paddle y2) =
240− let { vx' = if within 20 y1 y && within 8 25 x
241− then makePositive vx else
242− if within 20 y2 y && within 8 (gameWidth - 25) x
243− then makeNegative vx else vx
244− ; vy' = if y < 7 then makePositive vy else
245− if y > gameHeight - 7 then makeNegative vy else vy
244+ let { hitPaddle1 = within 20 y1 y && within 8 25 x
245+ ; hitPaddle2 = within 20 y2 y && within 8 (gameWidth - 25) x
246+ ; vx' = stepVelocity vx hitPaddle1 hitPaddle2
247+ ; vy' = stepVelocity vy (y < 7) (y > gameHeight - 7)
246248 ; scored = x > gameWidth || x < 0
247249 ; x' = if scored then halfWidth else x + vx' * delta
248250 ; y' = if scored then halfHeight else y + vy' * delta