Last active
October 23, 2019 18:10
-
-
Save JoelQ/65e41dfddeec29ad1fc5 to your computer and use it in GitHub Desktop.
Gravity and Ball
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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"
}