Created
March 9, 2012 21:17
-
-
Save qsorix/2008747 to your computer and use it in GitHub Desktop.
Bouncing ball - Functional Reactive Programming in Haskell using Fal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Text.Printf | |
{- Module Fal is from the book "The Haskell School of Expression" by Paul Hudak. | |
- You need to read it, or google for the code -} | |
import Fal | |
count :: Event a -> Behavior Int | |
count ev = 0 `stepAccum` ev ->> (+1) | |
app = | |
let yacc = -5 | |
falling = integral yacc | |
yvel = falling `switch` | |
( bounce `snapshot_` yvel =>> doBounce | |
.|. key ->> constB 4 + falling | |
) | |
doBounce v | v > -0.5 = constB 0 | |
| otherwise = constB (0.8 * negate v) + falling | |
ypos = integral yvel | |
xpos = 0 | |
bounce = when (ypos <=* -1.7 &&* yvel <* 0) | |
bounces = count bounce | |
keys = count key | |
in paint red (translate (0, -2.15) (rec 2 0.3)) | |
`over` paint yellow (translate (xpos, ypos) (ell 0.3 0.3)) | |
`over` text (lift1 (printf "Ypos: %+.4f") ypos) 20 20 | |
`over` text (lift1 (printf "Yvel: %+.4f") yvel) 20 40 | |
`over` text (lift1 (printf "bounces: %i") bounces) 20 60 | |
`over` text (lift1 (printf "clicks: %i") keys) 20 80 | |
{- test is implemented in Fal -} | |
main = test app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment