From 51f77536410b22e945380aa4914c9a00e63d44bc Mon Sep 17 00:00:00 2001 From: Evan Czaplicki Date: Wed, 10 Dec 2014 03:26:21 -0800 Subject: [PATCH] update for 0.14 --- .gitignore | 4 +-- Hello.elm | 48 ++++++++++++++++++------------ README.md | 8 ++--- mario/Mario.elm | 78 +++++++++++++++++++++++++++++-------------------- 4 files changed, 81 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index de24112..c36548d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -build -cache +elm-stuff .cabal-sandbox cabal.sandbox.config -todo/elm_dependencies diff --git a/Hello.elm b/Hello.elm index 4a2b64b..d25d551 100644 --- a/Hello.elm +++ b/Hello.elm @@ -1,29 +1,41 @@ --- Welcome to your first Elm program +{- Welcome to your first Elm program --- Read up on syntax at http://elm-lang.org/learn/Syntax.elm +Read up on syntax: + http://elm-lang.org/learn/Syntax.elm + +Learn about the Elm's core libraries: + http://package.elm-lang.org/packages/elm-lang/core/latest/ + +-} + +import Graphics.Element (..) +import List +import Text (..) + + +main : Element +main = + flow down + [ helloWorld + , welcomeGraphics + ] --- Learn about Elm's functions in the Catalog: --- http://library.elm-lang.org/catalog/elm-lang-Elm/0.13.0/ helloWorld : Element -helloWorld = asText "Hello, World!" +helloWorld = + asText "Hello, World!" + welcomeGraphics : Element welcomeGraphics = let dimensions = 90 imgSize = 30 - elmLogo = image imgSize imgSize "http://elm-lang.org/logo.png" + elmLogo = + image imgSize imgSize "http://elm-lang.org/logo.png" + elmsPerSide = dimensions // imgSize - row = flow right (repeat elmsPerSide elmLogo) + + row = + flow right (List.repeat elmsPerSide elmLogo) in - flow down (repeat elmsPerSide row) - - -main : Element -main = - flow down [ - helloWorld, - welcomeGraphics - ] - - + flow down (List.repeat elmsPerSide row) diff --git a/README.md b/README.md index 4f086a6..1e06e0f 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,11 @@ fun to play with in [Elm Reactor][reactor]. ## Get Set Up -First make sure you have [Elm Platform][platform] 0.13 or higher. The next -step is to clone this repo: - -[platform]: https://github.com/elm-lang/elm-platform/ +After [installing Elm Platform](http://elm-lang.org/Install.elm), clone this +repo: ```shell -git clone https://github.com/michaelbjames/elm-examples.git +git clone https://github.com/evancz/elm-examples.git cd elm-examples ``` diff --git a/mario/Mario.elm b/mario/Mario.elm index 3a2a2f7..4ea6f79 100644 --- a/mario/Mario.elm +++ b/mario/Mario.elm @@ -1,21 +1,27 @@ -import Keyboard -import Window +import Color (..) import Debug +import Graphics.Collage (..) +import Graphics.Element (..) +import Keyboard +import Signal +import Time (..) +import Window -- MODEL -type Model = - { x : Float - , y : Float - , vx : Float - , vy : Float +type alias Model = + { x : Float + , y : Float + , vx : Float + , vy : Float , dir : Direction } -data Direction = Left | Right +type Direction = Left | Right + +type alias Keys = { x:Int, y:Int } -type Keys = { x:Int, y:Int } mario : Model mario = @@ -29,8 +35,8 @@ mario = -- UPDATE -step : (Float, Keys) -> Model -> Model -step (dt, keys) mario = +update : (Float, Keys) -> Model -> Model +update (dt, keys) mario = mario |> gravity dt |> jump keys @@ -38,16 +44,21 @@ step (dt, keys) mario = |> physics dt |> Debug.watch "mario" + jump : Keys -> Model -> Model jump keys mario = - if keys.y > 0 && mario.vy == 0 then { mario | vy <- 6.0 } else mario + if keys.y > 0 && mario.vy == 0 + then { mario | vy <- 6.0 } + else mario + gravity : Float -> Model -> Model gravity dt mario = { mario | - vy <- if mario.y > 0 then mario.vy - dt/4 else 0 + vy <- if mario.y > 0 then mario.vy - dt/40 else 0 } + physics : Float -> Model -> Model physics dt mario = { mario | @@ -55,32 +66,35 @@ physics dt mario = y <- max 0 (mario.y + dt * mario.vy) } + walk : Keys -> Model -> Model walk keys mario = { mario | vx <- toFloat keys.x, - dir <- if | keys.x < 0 -> Left - | keys.x > 0 -> Right - | otherwise -> mario.dir + dir <- + if | keys.x < 0 -> Left + | keys.x > 0 -> Right + | otherwise -> mario.dir } --- DISPLAY +-- VIEW -display : (Int, Int) -> Model -> Element -display (w',h') mario = +view : (Int, Int) -> Model -> Element +view (w',h') mario = let (w,h) = (toFloat w', toFloat h') - verb = if | mario.y > 0 -> "jump" - | mario.vx /= 0 -> "walk" - | otherwise -> "stand" + verb = + if | mario.y > 0 -> "jump" + | mario.vx /= 0 -> "walk" + | otherwise -> "stand" - dir = case mario.dir of - Left -> "left" - Right -> "right" + dir = + case mario.dir of + Left -> "left" + Right -> "right" - src = "imgs/mario/"++ verb ++ "/" ++ dir ++ ".gif" - --src = "http://i188.photobucket.com/albums/z137/DreamsInMotion/Video%20Game%20Pictures/Metroid/samus-1.gif" + src = "imgs/mario/"++ verb ++ "/" ++ dir ++ ".gif" marioImage = image 35 35 src @@ -102,12 +116,14 @@ display (w',h') mario = -- SIGNALS main : Signal Element -main = lift2 display Window.dimensions (foldp step mario input) +main = + Signal.map2 view Window.dimensions (Signal.foldp update mario input) + input : Signal (Float, Keys) input = - let delta = lift (\t -> t/20) (fps 25) + let delta = Signal.map (\t -> t/20) (fps 25) deltaArrows = - lift2 (,) delta (Debug.watch "arrows" <~ Keyboard.arrows) + Signal.map2 (,) delta (Signal.map (Debug.watch "arrows") Keyboard.arrows) in - sampleOn delta deltaArrows + Signal.sampleOn delta deltaArrows