Skip to content

Instantly share code, notes, and snippets.

@fredguth
Last active February 7, 2016 21:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredguth/aa87dc1cba583a78738f to your computer and use it in GitHub Desktop.
Save fredguth/aa87dc1cba583a78738f to your computer and use it in GitHub Desktop.
Part 2: Following Elm tutorial
import Graphics.Element exposing (..)
import Graphics.Collage exposing (..)
import Color exposing (..)
import Mouse
import Window
import Time exposing (..)
import Signal exposing (foldp)
--Model
type alias Vec =
(Float, Float)
type alias Pill =
{ pos : Vec
, vel : Vec
, rad : Float
, col : Color
}
vecAdd : Vec -> Vec -> Vec
vecAdd (ax, ay) (bx, by) =
(ax + bx, ay + by)
vecMulS : Vec -> Time -> Vec
vecMulS (x, y) t =
(x*t, y*t)
defaultPill =
{ pos = (0,0)
, vel = (0,-30)
, rad = 15
, col = lightRed
}
-- Update
stepPill : Time -> Pill -> Pill
stepPill dt p =
{p|pos = vecAdd p.pos (vecMulS p.vel dt)}
-- View
render : (Int, Int) -> Pill -> Element
render (w,h) p =
let formPill {rad, col, pos} = circle rad
|> filled col
|> move pos
forms = [formPill p]
in
color lightGray <| container w h middle
<| color white
<| collage 400 400 forms
sig = Signal.map inSeconds (fps 30)
main = Signal.map2 render Window.dimensions (foldp stepPill defaultPill sig )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment