From d37a301ed39bac823e0f2223b8d229b417e128c7 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Wed, 8 Oct 2014 22:13:41 +0200 Subject: Adding a power to change the player color that reverse points to catch and points to avoid --- README.md | 10 ++++++---- index.html | 14 +++++++++++--- src/Cloud.elm | 12 +++++++----- src/CloudStep.elm | 29 ++++++++++++++++++----------- src/Config.elm | 9 +++++++++ src/Display.elm | 32 ++++++++++++++++++-------------- src/Game.elm | 5 +++++ src/Input.elm | 17 ++++++++++------- src/Player.elm | 4 +++- src/Point.elm | 2 +- src/RandomValues.elm | 4 ++-- src/Step.elm | 44 ++++++++++++++++++++++++++++---------------- style.css | 18 ++++++++++++++---- 13 files changed, 132 insertions(+), 68 deletions(-) create mode 100644 src/Config.elm diff --git a/README.md b/README.md index a845588..fbf3ade 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ -Avoid -===== +cAtchVoid +========= -Avoid red points and catch the most green points as possible. +Catch the points of your color, avoid the other points. -You can play at [http://guyonvarch.github.io/avoid](http://guyonvarch.github.io/avoid). +Use the arrow keys to move and 'e' to change your color. + +You can play at [http://guyonvarch.github.io/catchvoid](http://guyonvarch.github.io/catchvoid). diff --git a/index.html b/index.html index b2352f0..5a380b5 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ - Avoid + cAtchVoid @@ -12,17 +12,25 @@ -

Avoid

+

cAtchVoid

+

+ Catch the points of your color, avoid the other points. +

+ +

+ Use the arrow keys to move and 'e' to change your color. +

+ - + Fork me on GitHub [Point] , spawn : Float , lastSpawn : Float } initCloud : Cloud initCloud = - let spawn = 200 - in { greenPoints = [] - , redPoints = [] + let spawn = 100 + in { points config = + case config of + White -> [] + Black -> [] , spawn = spawn , lastSpawn = -spawn } diff --git a/src/CloudStep.elm b/src/CloudStep.elm index e33d573..65609cb 100644 --- a/src/CloudStep.elm +++ b/src/CloudStep.elm @@ -8,26 +8,33 @@ import Point (..) import RandomValues (..) import Physics (getMove) import Cloud (..) +import Config (..) cloudStep : Float -> RandomValues -> Player -> Cloud -> (Cloud, Int) -cloudStep time {greenPoint, redPoint} player {greenPoints, redPoints, spawn, lastSpawn} = - let insideGreenPoints = presentPoints time greenPoints - insideNotCaughtGreenPoints = filter (not . (playerPointCollision time player)) insideGreenPoints - addScore = (length insideGreenPoints) - (length insideNotCaughtGreenPoints) - presentRedPoints = presentPoints time redPoints +cloudStep time randomValues player {points, spawn, lastSpawn} = + let pointsToCatch = presentPoints time (points player.config) + presentAndNotCaughtPoints = filter (not . (playerPointCollision time player)) pointsToCatch + addScore = (length pointsToCatch) - (length presentAndNotCaughtPoints) + presentOtherPoints = presentPoints time (points (otherConfig player.config)) newCloud = if time > lastSpawn + spawn then - let newGreenPoint = newPoint time greenPoint - newRedPoint = newPoint time redPoint + let newPoint1 = newPoint time randomValues.point1 + newPoint2 = newPoint time randomValues.point2 in - { greenPoints = newGreenPoint :: insideNotCaughtGreenPoints - , redPoints = newRedPoint :: presentRedPoints + { points config = + if(config == player.config) then + newPoint1 :: presentAndNotCaughtPoints + else + newPoint2 :: presentOtherPoints , spawn = spawn - sqrt(spawn) / 50 , lastSpawn = time } else - { greenPoints = insideNotCaughtGreenPoints - , redPoints = presentRedPoints + { points config = + if(config == player.config) then + presentAndNotCaughtPoints + else + presentOtherPoints , spawn = spawn , lastSpawn = lastSpawn } diff --git a/src/Config.elm b/src/Config.elm new file mode 100644 index 0000000..60f4cc3 --- /dev/null +++ b/src/Config.elm @@ -0,0 +1,9 @@ +module Config where + +data Config = White | Black + +otherConfig : Config -> Config +otherConfig config = + case config of + White -> Black + Black -> White diff --git a/src/Display.elm b/src/Display.elm index aa4ed51..c52b9e3 100644 --- a/src/Display.elm +++ b/src/Display.elm @@ -5,15 +5,16 @@ import Player (..) import Game (Game) import Point (..) import Board (boardSize) +import Config (..) display : Game -> Element display {time, score, player, cloud, bestScore} = - let greenPointForms = map (pointForm time greenPointColor) cloud.greenPoints - redPointForms = map (pointForm time redPointColor) cloud.redPoints + let whitePointForms = map (pointForm time (configColor White)) (cloud.points White) + blackPointForms = map (pointForm time (configColor Black)) (cloud.points Black) forms = boardForms ++ playerForms player - ++ greenPointForms - ++ redPointForms + ++ whitePointForms + ++ blackPointForms ++ scoreForms score ++ bestScoreForms bestScore in collage (truncate boardSize.x) (truncate boardSize.y) forms @@ -22,10 +23,12 @@ boardForms : [Form] boardForms = [filled boardColor (rect boardSize.x boardSize.y)] boardColor : Color -boardColor = rgb 17 17 17 +boardColor = rgb 103 123 244 playerForms : Player -> [Form] -playerForms player = [circleForm player.pos playerSize playerColor] +playerForms player = + let playerColor = configColor player.config + in [circleForm player.pos playerSize playerColor] playerColor : Color playerColor = rgb 224 224 224 @@ -35,11 +38,11 @@ pointForm time color point = let pos = pointMove point time in circleForm pos pointSize color -greenPointColor : Color -greenPointColor = rgb 34 85 34 - -redPointColor : Color -redPointColor = rgb 170 0 0 +configColor : Config -> Color +configColor config = + case config of + White -> rgb 240 240 240 + Black -> rgb 14 17 33 circleForm : Vec2 -> Float -> Color -> Form circleForm pos size color = @@ -64,15 +67,16 @@ bestScoreForms bestScore = if(bestScore > 0) then let text = "Record: " ++ (show bestScore) pos = - { x = boardSize.x / 2 - 65 + { x = -boardSize.x / 2 + 100 , y = -boardSize.y / 2 + 30 } - in [textForm text pos rightAligned] + in [textForm text pos leftAligned] else [] textForm : String -> Vec2 -> (Text -> Element) -> Form textForm content pos alignment = let textElement = toText content + |> Text.height 30 |> typeface ["calibri", "arial"] |> Text.color textColor |> bold @@ -82,4 +86,4 @@ textForm content pos alignment = |> move (pos.x, pos.y) textColor : Color -textColor = rgb 224 224 224 +textColor = rgb 14 17 33 diff --git a/src/Game.elm b/src/Game.elm index 83d8baa..0a12db8 100644 --- a/src/Game.elm +++ b/src/Game.elm @@ -3,9 +3,12 @@ module Game where import Player (..) import Cloud (..) import Vec2 (Vec2) +import Config (..) +import Keyboard (KeyCode) type Game = { time : Float + , keysDown : [KeyCode] , score : Int , player : Player , cloud : Cloud @@ -17,9 +20,11 @@ initialGame playerPos bestScore = let initPlayer = { pos = playerPos , speed = { x = 0, y = 0 } + , config = White } in { time = 0 + , keysDown = [] , score = 0 , player = initPlayer , cloud = initCloud diff --git a/src/Input.elm b/src/Input.elm index 1015302..8ba43ec 100644 --- a/src/Input.elm +++ b/src/Input.elm @@ -1,13 +1,15 @@ module Input where -import Keyboard -import Random +import Char (toCode) import RandomValues (RandomValues) +import Keyboard (KeyCode, keysDown, arrows) +import Random import Vec2 (Vec2) type Input = { dir : Vec2 + , inputKeysDown : [KeyCode] , delta : Time , randomValues : RandomValues } @@ -15,10 +17,11 @@ type Input = getInput : Signal Input getInput = let dtSignal = delta - dirSignal = lift recordIntToVec2 Keyboard.arrows + dirSignal = lift recordIntToVec2 arrows randomFloatsSignal = Random.floatList (lift (\_ -> 6) dtSignal) randomValuesSignal = lift floatsToRandomValues randomFloatsSignal in sampleOn dtSignal <| Input <~ dirSignal + ~ keysDown ~ dtSignal ~ randomValuesSignal @@ -33,16 +36,16 @@ recordIntToVec2 {x, y} = floatsToRandomValues : [Float] -> RandomValues floatsToRandomValues [angle1, x1, y1, angle2, x2, y2] = - let greenPoint = + let point1 = { angle = angle1 , x = x1 , y = y1 } - redPoint = + point2 = { angle = angle2 , x = x2 , y = y2 } - in { greenPoint = greenPoint - , redPoint = redPoint + in { point1 = point1 + , point2 = point2 } diff --git a/src/Player.elm b/src/Player.elm index b64655e..56c4e97 100644 --- a/src/Player.elm +++ b/src/Player.elm @@ -1,14 +1,16 @@ module Player where import Vec2 (..) +import Config (Config) type Player = { pos : Vec2 , speed : Vec2 + , config : Config } playerSize : Float playerSize = 10 playerSpeed : Float -> Float -playerSpeed dt = dt / 500 +playerSpeed dt = dt / 400 diff --git a/src/Point.elm b/src/Point.elm index 90c61c3..086f8a4 100644 --- a/src/Point.elm +++ b/src/Point.elm @@ -18,7 +18,7 @@ pointSize : Float pointSize = 5 pointSpeed : Float -> Float -pointSpeed dt = dt / 25 +pointSpeed dt = dt / 20 pointSpawnDist : Float pointSpawnDist = boardDiagonal * 3 / 5 diff --git a/src/RandomValues.elm b/src/RandomValues.elm index 5d45b15..5e40fc5 100644 --- a/src/RandomValues.elm +++ b/src/RandomValues.elm @@ -1,8 +1,8 @@ module RandomValues where type RandomValues = - { greenPoint : PointRandomValues - , redPoint : PointRandomValues + { point1 : PointRandomValues + , point2 : PointRandomValues } type PointRandomValues = diff --git a/src/Step.elm b/src/Step.elm index 72d3ae1..08a5f49 100644 --- a/src/Step.elm +++ b/src/Step.elm @@ -11,26 +11,38 @@ import Input (Input) import Physics (getNewPosAndSpeed) import RandomValues (..) import CloudStep (cloudStep) +import Config (otherConfig) +import Keyboard (KeyCode) +import Char (fromCode, toCode) step : Input -> Game -> Game -step {dir, delta, randomValues} {time, score, player, cloud, bestScore} = - if(playerPointsCollision time player cloud.redPoints) then - let newBestScore = if(score > bestScore) then score else bestScore - in initialGame player.pos newBestScore - else - let newTime = time + delta - newPlayer = playerStep delta dir player - (newCloud, addScore) = cloudStep time randomValues player cloud - in { time = newTime - , score = score + addScore - , player = newPlayer - , cloud = newCloud - , bestScore = bestScore - } +step {dir, inputKeysDown, delta, randomValues} {time, keysDown, score, player, cloud, bestScore} = + let hostilePoints = cloud.points (otherConfig player.config) + in if(playerPointsCollision time player hostilePoints) then + let newBestScore = if(score > bestScore) then score else bestScore + in initialGame player.pos newBestScore + else + let newTime = time + delta + newPlayer = playerStep delta dir (newKeyCode keysDown inputKeysDown) player + (newCloud, addScore) = cloudStep time randomValues newPlayer cloud + in { time = newTime + , keysDown = inputKeysDown + , score = score + addScore + , player = newPlayer + , cloud = newCloud + , bestScore = bestScore + } -playerStep : Float -> Vec2 -> Player -> Player -playerStep dt dir player = +playerStep : Float -> Vec2 -> (KeyCode -> Bool) -> Player -> Player +playerStep dt dir newKey player = let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed) + newConfig = if (newKey (toCode 'e')) then otherConfig player.config else player.config in { pos = inBoard playerSize pos , speed = speed + , config = newConfig } + +newKeyCode : [KeyCode] -> [KeyCode] -> KeyCode -> Bool +newKeyCode lastKeyCodes newKeyCodes keyCode = + let contains = (\l -> l > 0) . length . filter (\kc -> kc == keyCode) + in not (contains lastKeyCodes) && (contains newKeyCodes) diff --git a/style.css b/style.css index 71a4afc..9fad024 100644 --- a/style.css +++ b/style.css @@ -1,25 +1,35 @@ body { margin: 0; - background-color: #111111; + background-color: #05060c; font-family: calibri; } h1 { font-weight: bold; - background-color: #225522; + background-color: #1b203f; min-width: 500px; color: white; margin: 0; - padding-top: 20px; - padding-bottom: 20px; + font-size: 36px; + padding-top: 30px; + padding-bottom: 30px; text-align: center; } #game { margin-left: auto; margin-right: auto; + margin-bottom: 40px; border-top: 10px dashed #222222; border-bottom: 10px dashed #222222; width: 500px; height: 500px; } + +p { + text-align: center; + color: #eeeeee; + font-style: italic; + font-size: 17px; +} + -- cgit v1.2.3