Skip to content

Instantly share code, notes, and snippets.

@nuclearsandwich
Created February 23, 2013 05:03
Show Gist options
  • Save nuclearsandwich/5018546 to your computer and use it in GitHub Desktop.
Save nuclearsandwich/5018546 to your computer and use it in GitHub Desktop.
-- Written by notthemessiah in #elm on Freenode.
-- MODEL
ship = { x=100, y=100, vx=0, vy=0, a=0.0}
-- UPDATE -- ('m' is for model, formerly mario)
move t m = { m | x <- m.x + t*m.vx , y <- m.y + t*m.vy }
drag t m = { m | vx <- m.vx - 0.01 * t*m.vx, vy <- m.vy - 0.01 * t*m.vy}
thrust {y} m = { m | vx <- m.vx + cos(m.a*2*pi)*y , vy <- m.vy + sin(m.a*2*pi)*y }
turn {x} m = { m | a <- m.a + x/32}
warp m = {m | x <- mod m.x 500, y <- mod m.y 500}
step (t, dir) = move t . drag t . thrust dir . turn dir . warp
-- DISPLAY
render (w,h) ship =
let half n = n `div` 2
in collage w h
[ filled (rgb 17 24 24) $ rect 500 500 (250,250) --w h (half w, half h)
, rotate ship.a $ outlined white $ ngon 3 20 (ship.x, ship.y)
, rotate ship.a $ outlined white $ ngon 2 10 (ship.x, ship.y) ]
-- ASTEROIDS
input = let delta = lift (\t -> t/20) (fps 25)
in sampleOn delta (lift2 (,) delta Keyboard.arrows)
main = lift2 render Window.dimensions (foldp step ship input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment