Skip to content

Instantly share code, notes, and snippets.

@chribben
chribben / ReactiveCircle.elm
Created August 11, 2013 21:16
Reactive circle
import Signal
import Mouse
cl = (clamp 100 200) <~ Mouse.x
reactiveCircle = (filled blue) <~ (circle <~ (toFloat <~ cl))
main = lift (collage 300 300) (Signal.combine [reactiveCircle])
time = lift (inSeconds . fst) (timestamp (fps 40))
a dy = let c = filled blue (circle 10)
in collage 300 300
[c |> move (0, 100 * cos dy)]
b = lift a time
main = b
@chribben
chribben / TetrisBrickGenerator.elm
Created August 13, 2013 21:18
Tetris Brick Generator
import Random
data Brick = Line | LeftSquiggle | RightSquiggle | LeftStick | RightStick | Square
rand a = Random.range 0 6 (constant a)
brick n = if | n == 0 -> Line
| n == 2 -> LeftSquiggle
| n == 3 -> RightSquiggle
| n == 4 -> RightStick
| n == 5 -> LeftStick
| otherwise -> Square
main = asText <~ (brick <~ (rand 1))
@chribben
chribben / BouncingRandomTetrisBrick.elm
Created August 14, 2013 20:45
BouncingRandomTetrisBrick
import Random
data Brick = Line | LeftSquiggle | RightSquiggle | LeftStick | RightStick | Box
rand = Random.range 0 6 (constant 1)
brick n = if | n == 0 -> Line
| n == 2 -> LeftSquiggle
| n == 3 -> RightSquiggle
| n == 4 -> RightStick
| n == 5 -> LeftStick
| otherwise -> Box
brickText = asText <~ (brick <~ rand)
@chribben
chribben / MoveableAndClickableBouncingFruit.elm
Created August 18, 2013 14:54
MoveableAndClickableBouncingFruit
import Random
import Mouse
import Keyboard
data Fruit = Apple | Orange | Banana | Melon | Guava
rand = Random.range 0 4 (Mouse.clicks)
fruit n = if | n == 0 -> Apple
| n == 1 -> Orange
| n == 2 -> Banana
| n == 3 -> Melon
| otherwise -> Guava
@chribben
chribben / SignalForPressingRightOrLeftArrow.elm
Created August 21, 2013 08:18
SignalForPressingRightOrLeftArrow
import Keyboard
keyToBool x = if x == 0 then False else True
sfx p = lift (\p -> p.x) p
signalX = sfx Keyboard.arrows
xArrowPulse = fpsWhen 10 (keyToBool <~ signalX)
main = lift asText xArrowPulse
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
import List
import Mouse
import Window
--MODEL
defaultBall = {x = 0, y = 0, 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 }
import String
import Graphics.Input (Input, input)
import Graphics.Input.Field as Field
xaxis = segment (-150,-100) (150,-100)
yaxis = segment (-150,-100) (-150,100)
content : Input Field.Content
content = input Field.noContent
field = Field.field Field.defaultStyle content.handle id "Type here!" <~ content.signal
import Keyboard
import Window
import Graphics.Input (Input, input, button, dropDown)
import Graphics.Input.Field (Content, noContent, field, defaultStyle, Forward, Backward)
-- MODEL
data Direction = North | East | South | West
type Robot = {x:Int, y:Int, dir:Direction}
type Board = [(Int,Int)]
type Game = {robot:Robot, board:Board}