Skip to content

Instantly share code, notes, and snippets.

@nebuta nebuta/bouncing.elm
Last active Aug 29, 2015

Embed
What would you like to do?
Bouncing ball
main = lift scene state
state = foldp step initial (fps 60)
initial = State 0 -70 100 8 0
data State = State Float Float Float Float Float
scene (State t x y vx vy) = collage 400 400 [box, ball x y]
box = filled (hsl (pi/2) 0.5 0.5)
<| path [ (-90,100), (-100,100), (-100,-50),(100,-50),
(100,100), (90,100), (90,-40), (-90,-40), (-90,50)]
ball x y = move (x,y)
<| filled (hsl 0 0.5 0.5)
<| circle 10
step _ (State t x y vx vy) =
let vy2 = if y < -27 then -vy*0.95 else vy-0.2
vx2 = (if x < -80 then -vx else
(if x >=80 then -vx else vx)) * (if y < -27 then 0.8 else 1)
in State t (x+vx2) (y+vy2) vx2 vy2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.