Skip to content

Instantly share code, notes, and snippets.

@arsatiki
Created January 25, 2014 11:23
Show Gist options
  • Save arsatiki/8614978 to your computer and use it in GitHub Desktop.
Save arsatiki/8614978 to your computer and use it in GitHub Desktop.
Cats and mice
import Mouse
import Window
ball = {x = 0.0, y = 0.0 }
type Pair = (Float, Float)
unit (x, y) = let d = sqrt(x^2 + y^2) in (x/d, y/d)
(-->) {x, y} (tx, ty) = unit (tx - x, ty - y)
update: Pair -> {x: Float, y: Float} -> {x: Float, y: Float}
update (dx,dy) b = {b | x <- b.x + dx, y <- b.y + dy }
fixit (a,b) = (toFloat a, toFloat b)
fpos = fixit <~ Mouse.position
err: Signal Pair
err = lift2 (-->) (sampleOn (fps 25) balls) fpos
balls = foldp update ball err
main = lift2 scene balls Window.dimensions
scene {x, y} (w, h) =
let dx = x - toFloat w / 2
dy = toFloat h / 2 - y
in collage w h
[ circle 30 |> filled orange
|> move (dx, dy)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment