Skip to content

Instantly share code, notes, and snippets.

@davidhq
Forked from JoelQ/Ball.elm
Last active March 31, 2016 21:27
Show Gist options
  • Save davidhq/eae55136adc1973b524f7b41746b78ac to your computer and use it in GitHub Desktop.
Save davidhq/eae55136adc1973b524f7b41746b78ac to your computer and use it in GitHub Desktop.
Gravity and Ball
-- https://gist.github.com/davidhq/eae55136adc1973b524f7b41746b78ac
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Color exposing (..)
import Keyboard
import Time
import Debug
-- Model
type alias Model = { x : Float, y : Float, radius: Float }
width : Int
width = 700
height : Int
height = 500
bottom : Int
bottom = (-height) // 2
-- Update
type Action = Up | Right | Left | Noop
update : Action -> Model -> Model
update action model =
playerMovement action model
|> gravity
|> Debug.watch "model"
playerMovement : Action -> Model -> Model
playerMovement action model =
case action of
Noop -> model
Up -> { model | y = model.y + 30 }
Right -> { model | x = model.x + 5 }
Left -> { model | x = model.x - 5 }
gravity : Model -> Model
gravity model =
if model.y > (toFloat bottom) + model.radius
then { model | y = model.y - 5 }
else model
-- View
view : Model -> Element
view model =
let
ball =
circle model.radius
|> filled blue
|> move (model.x, model.y)
|> Debug.trace "ball"
background =
rect (toFloat width) (toFloat height)
|> filled green
in
collage width height [ ball ]
-- Signals
input : Signal { x : Int, y : Int }
input =
Signal.sampleOn (Time.fps 30) Keyboard.arrows
action : Signal Action
action =
Signal.map arrowToAction input
arrowToAction : { x : Int, y : Int} -> Action
arrowToAction arrow =
if arrow.x < 0 then Left else
if arrow.x > 0 then Right else
if arrow.y > 0 then Up else Noop
model : Signal Model
model =
Signal.foldp update {x = 0, y = 0, radius = 30 } action
-- Signal.foldp
-- : (a -> state -> state)
-- -> state
-- -> Signal a
-- -> Signal state
main : Signal Element
main =
Signal.map view model
@davidhq
Copy link
Author

davidhq commented Mar 31, 2016

Updated for Elm 0.16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment