Skip to content

Instantly share code, notes, and snippets.

@JoelQ JoelQ/Ball.elm
Last active Dec 21, 2016

Embed
What would you like to do?
Gravity and Ball
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 -> Left
| arrow.x > 0 -> Right
| arrow.y > 0 -> Up
| otherwise -> Noop
model : Signal Model
model =
Signal.foldp update {x = 0, y = 0, radius = 30 } action
main : Signal Element
main =
Signal.map view model
@sscholle

This comment has been minimized.

Copy link

commented Dec 21, 2016

i'm new to elm, I tried running this gist with elm reactor but got this error:

`I cannot find module 'Graphics.Collage'.

Module 'Main' is trying to import it.

Potential problems could be:

  • Misspelled the module name
  • Need to add a source directory or new dependency to elm-package.json`

My elm-package looks like this:

{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"evancz/elm-graphics": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.