Skip to content

Instantly share code, notes, and snippets.

@artemkonenko
Created December 22, 2014 19:07
Show Gist options
  • Save artemkonenko/90047fa83e2d623c8367 to your computer and use it in GitHub Desktop.
Save artemkonenko/90047fa83e2d623c8367 to your computer and use it in GitHub Desktop.
Anygame haskell project
import FRP.Helm
import qualified FRP.Helm.Keyboard as Keyboard
import qualified FRP.Helm.Window as Window
boardWidth = 7
boardHeight = 6
--type Board [[Int]]
{- 6x7 circles
0 - empty
1 - red
2 - yellow
-}
data State = State { currentColor :: Int,
aimPos :: (Int, Int),
board :: [[Int]] }
step :: (Int, Int) -> State -> State
step (dx, dy) (State { currentColor = currentColor, aimPos = (aimx, aimy), board = board }) =
State { currentColor = currentColor,
aimPos = (mod (aimx + dx) boardWidth, mod (aimy + dy) boardHeight),
board = board}
stateColor :: Int -> Color
stateColor 0 = white
stateColor 1 = red
stateColor 2 = yellow
coordToForm :: Int -> Int -> (Form -> Form)
coordToForm x y = move ((fromIntegral x) * 90 - 300, (fromIntegral y) * 90 - 250)
stateToForm :: Int -> Int -> Int -> Form
stateToForm x y color = coordToForm x y $ filled (stateColor color) $ circle 40
aimToForm :: (Int, Int) -> Int -> Form
aimToForm (x, y) color = coordToForm x y $ outlined (dashed (stateColor color)) $ square 64
stateToRenderlist :: (Int, Int) -> State -> [Form]
stateToRenderlist (w,h) (State { currentColor = currentColor, aimPos = aimPos, board = board }) =
concat (map (\(row, y) -> map (\(colour, x) -> stateToForm x y colour) row) enumBoard)
where
enumBoard = zip (map (\x -> zip x [0..]) board) [0..]
render :: (Int, Int) -> State -> Element
render (w, h) (State { currentColor = currentColor, aimPos = aimPos, board = board }) =
centeredCollage w h ((stateToRenderlist (w,h) (State { currentColor = currentColor, aimPos = aimPos, board = board }))
++[aimToForm aimPos currentColor])
main :: IO ()
main = run defaultConfig $ render <~ Window.dimensions ~~ stepper
where
state = State { currentColor = 1,
aimPos = (2, 2),
board = [[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,2,0,0,0],
[0,1,0,1,0,2,0]]}
stepper = foldp step state Keyboard.arrows--Keyboard.Number1Key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment