type alias Model = { ... }
type Msg = ...
view :: Model -> Html
update :: Msg -> Model -> Model
Elm supports a number of different main apps, but we will only focus on gameApp
main =
gameApp Tick
{ model = init
, update = update
, view = view
}
type Msg = Tick Float GetKeyState
type alias Model = { time :: Float
...
}
update msg model =
case msg of
Tick t getKey -> { model | time = t }
view model = collage 192 128 (myShapes model)
myShapes model = [ circle 5
|> filled red
|> scale (10 * sin model.time)
]
type alias GetKeyState = ( Keys -> KeyState, (Float,Float), (Float,Float))
type Keys
= Key String
| Backspace
| Tab
| Enter
| Shift
| Ctrl
| Alt
| Caps
| LeftArrow
| UpArrow
| RightArrow
| DownArrow
| Delete
| Space
type KeyState
= JustDown
| Down
| JustUp
| Up
We can use the GetKeyState function to update the model
type alias Model = { time : Float, size : Float }
update msg model =
case msg of
Tick t (getKey,_,_) ->
case (getKey (Key "a"),getKey (Key "b")) of
(Down,_) -> { model | size = model.size + 1 }
(_,Down) -> { model | size = model.size - 1 }
_ -> model
type Msg = Tick Float GetKeyState
| Tap
myShapes model =
[circle 5 |> filled red
|> scale model.size
|> notifyTap Tap ]
update msg model = case msg of
Tap -> {model | size = 20 }
Tick t (getKey,_,_) -> ...