Last active
August 29, 2015 13:56
-
-
Save chribben/9113935 to your computer and use it in GitHub Desktop.
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 Mouse | |
import Window | |
-- Mouse.isDown is true whenever the left mouse button | |
-- is pressed down and false otherwise. | |
--MODEL | |
type Points = [(Int,Int)] | |
--UPDATE | |
--INPUT | |
main = lift2 scene (keepWhen Mouse.isDown (100,100) Mouse.position) Window.dimensions | |
scene (x,y) (w,h) = | |
collage w h | |
[ move (100, toFloat(h)/2 - (toFloat y)) <| filled blue (circle 10.0), | |
move (200, toFloat(h)/2 - (toFloat y)) <| filled red (circle 10.0) | |
] | |
-- Try clicking. The boolean value will update automatically. |
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 Mouse | |
import Window | |
-- Mouse.isDown is true whenever the left mouse button | |
-- is pressed down and false otherwise. | |
--MODEL | |
type Points = [(Int,Int)] | |
type Ball = {x:Int, y: Int, v: Float, xdir: Int, ydir:Int} | |
--UPDATE | |
location t b = { b | x <- b.x + t*((b.xdir-b.x)/abs(b.ydir-b.y)*b.v), y <- b.y + t*((b.ydir-b.y)/abs(b.xdir-b.x)*b.v)} | |
velocity (x,y) b = { b | v <- toFloat 2, xdir <- 2.1, ydir <- 20} | |
updateBall (t, xd, yd) = location t . velocity (xd,yd) | |
--INPUT | |
input = let delta = lift (\t -> t/20) (fps 25) | |
in sampleOn delta (lift3 (,,) delta (keepWhen Mouse.isDown (100,100) Mouse.position)) | |
main = lift2 scene (keepWhen Mouse.isDown (100,100) Mouse.position) Window.dimensions | |
scene (x,y) (w,h) = | |
collage w h | |
[ move ((toFloat x) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) <| filled blue (circle 10.0), | |
move ((toFloat x + 40) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) <| filled red (circle 10.0) | |
] | |
-- Try clicking. The boolean value will update automatically. |
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 Mouse | |
import Window | |
-- Mouse.isDown is true whenever the left mouse button | |
-- is pressed down and false otherwise. | |
--MODEL | |
type Points = [(Int,Int)] | |
type Ball = {x:Int, y: Int, v: Float, xdir: Int, ydir:Int} | |
defaultBall = {x = 200, y = 200, v = 0.0, xdir = 200, ydir = 200} | |
--UPDATE | |
location t b = { b | x <- b.x + t*((b.xdir-b.x)/(b.ydir-b.y)*b.v), y <- b.y + t*((b.ydir-b.y)/(b.xdir-b.x)*b.v)} | |
velocity (x,y) b = { b | v <- toFloat 2, xdir <- toFloat x, ydir <- toFloat y} | |
updateBall (t, xy) = location t . velocity xy | |
--INPUT | |
input = let delta = lift (\t -> t/20) (fps 25) | |
in sampleOn delta (lift2 (,) delta (keepWhen Mouse.isDown (100,100) Mouse.position)) | |
display b (w, h) = collage w h | |
[ | |
move (b.x, b.y) <| filled blue (circle 10.0) | |
] | |
main = lift2 display (foldp updateBall defaultBall input) Window.dimensions | |
--main = lift2 scene (keepWhen Mouse.isDown (100,100) Mouse.position) Window.dimensions | |
scene (x,y) (w,h) = | |
collage w h | |
[ move ((toFloat x) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) <| filled blue (circle 10.0), | |
move ((toFloat x + 40) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) <| filled red (circle 10.0) | |
] | |
-- Try clicking. The boolean value will update automatically. |
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 Mouse | |
import Window | |
-- Mouse.isDown is true whenever the left mouse button | |
-- is pressed down and false otherwise. | |
--MODEL | |
type Points = [(Int,Int)] | |
type Ball = {x:Int, y: Int, v: Float, angle: Float} | |
defaultBall = {x = 200, y = 200, v = toFloat 0, angle = toFloat 0} | |
--UPDATE | |
--location t b = { b | x <- b.x + t*sqrt(b.v^2-(b.ydir-b.y)^2), y <- b.y + t*sqrt(b.v^2-(b.xdir-b.x)^2)} | |
location t b = { b | x <- b.x + t * b.v * cos b.angle, | |
y <- b.y + b.t * b.v * sin b.angle } | |
velocity (x,y) b = { b | v <- 0.2, angle <- if x-b.x == 0 then (if y >= b.y then -pi else pi) else atan ((y-b.y)/(x-b.x))} | |
updateBall (t, xy) = location t . velocity xy | |
--INPUT | |
floatify (x,y) = (toFloat x, toFloat y) | |
input = let delta = lift (\t -> t/20) (fps 25) | |
in sampleOn delta (lift2 (,) delta (floatify <~ (keepWhen Mouse.isDown (200,200) Mouse.position))) | |
--main = lift asText input | |
display b (w, h) = collage w h | |
[ | |
move (b.x, b.y) <| filled blue (circle 10.0) | |
] | |
--main = lift asText <| foldp updateBall defaultBall input | |
main = lift2 display (foldp updateBall defaultBall input) Window.dimensions | |
--main = lift2 scene (keepWhen Mouse.isDown (100,100) Mouse.position) Window.dimensions | |
scene (x,y) (w,h) = | |
collage w h | |
[ move ((toFloat x) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) <| filled blue (circle 10.0), | |
move ((toFloat x + 40) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) <| filled red (circle 10.0) | |
] | |
-- Try clicking. The boolean value will update automatically. |
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 Mouse | |
import Window | |
--MODEL | |
type Points = [(Int,Int)] | |
type Ball = {x:Int, y: Int, v: Float, angle: Float} | |
defaultBall = {x = 100, y = 100, v = toFloat 0, angle = toFloat 0} | |
--UPDATE | |
location t b = { b | x <- b.x - t * b.v * cos b.angle, | |
y <- b.y - t * b.v * sin b.angle } | |
velocity (x,y) b = { b | v <- sqrt((b.y-y)^2 + (b.x-x)^2)/10, angle <- atan2 (b.y-y) (b.x-x)} | |
updateBall (t, xy) = location t . velocity xy | |
--INPUT | |
floatify (x,y) = (toFloat x, toFloat y) | |
translate (x,y) (w,h)= ((toFloat x) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) | |
input = let delta = lift (\t -> t/50) (fps 25) | |
in sampleOn delta (lift2 (,) delta ((keepWhen Mouse.isDown (100,100) (translate <~ Mouse.position ~ Window.dimensions)))) | |
display b (w, h) (x,y)= | |
collage w h | |
[ | |
move (b.x, b.y) <| filled blue (circle 10.0) | |
] | |
main = lift3 display (foldp updateBall defaultBall input) Window.dimensions (floatify <~ Mouse.position) |
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 List | |
import Mouse | |
import Window | |
--MODEL | |
type Points = [(Int,Int)] | |
type Ball = {x:Int, y: Int, v: Float, angle: Float} | |
defaultBall = {x = 100, y = 100, v = toFloat 0, angle = toFloat 0} | |
--UPDATE | |
location t b = { b | x <- b.x - t * b.v * cos b.angle, | |
y <- b.y - t * b.v * sin b.angle } | |
velocity (x,y) b = { b | v <- sqrt((b.y-y)^2 + (b.x-x)^2)/10, angle <- atan2 (b.y-y) (b.x-x)} | |
updateBall (t, xy) = location t . velocity xy | |
--INPUT | |
input = let delta = lift (\t -> t/20) (fps 40) | |
in sampleOn delta (lift2 (,) delta ((keepWhen Mouse.isDown (100,100) (translate <~ Mouse.position ~ Window.dimensions)))) | |
--DISPLAY | |
display balls (w, h) (x,y)= | |
flow down | |
[ | |
[markdown|Press and drag using mouse left button|], | |
collage w h | |
<| List.map (\b -> move (b.x, b.y) <| filled red (circle 10.0) ) balls | |
] | |
ball = foldp updateBall defaultBall input | |
balls = combine <| List.map (\d -> delay (d*100) ball) [0..4] | |
main = lift3 display balls Window.dimensions (floatify <~ Mouse.position) | |
--HELPERS | |
floatify (x,y) = (toFloat x, toFloat y) | |
translate (x,y) (w,h)= ((toFloat x) - toFloat(w)/2, toFloat(h)/2 - (toFloat y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment