import Playground exposing (..)
import List exposing (map)
-- MAIN
main =
game view update init
init =
{ y = 0
, vy = 0
, score = 0.0
, countdown = 5.0
, blocks = []
, gameOver = False
}
-- VIEW
fromBool p =
case p of
True -> "True"
False -> "False"
view computer mario =
let
w = computer.screen.width
h = computer.screen.height
b = computer.screen.bottom
in
[ rectangle (rgb 0 0 0) w 10
|> moveY (b + h/2)
, image 70 70 (toGif mario)
|> move (-w/3) (b + h/2 + mario.y + 30)
, words (rgb 0 0 0) ("Score: " ++ String.fromInt (round mario.score))
|> scale 1.3
|> moveY (b + h/2 - 70) -- (-w/3)
]
++ map
(\x -> (rectangle (rgb 0 0 0) 10 50) |> move (w*x/100 - w/2) (b+h/2 + 30))
mario.blocks
++ if mario.gameOver then gameOverScreen h b else []
gameOverScreen h b =
[ rectangle (rgb 0 0 0) 200 100
|> moveY (b + h/2)
, words (rgb 255 255 255) "Game Over!"
|> scale 2
|> moveY (b + h/2)
, words (rgb 255 255 255) "Press Space to try again"
|> scale 0.8
|> moveY (b + h/2 - 30)
]
toGif mario =
if mario.y > 0 then
"https://elm-lang.org/images/mario/jump/right.gif"
else if not mario.gameOver then
"https://elm-lang.org/images/mario/walk/right.gif"
else
"https://elm-lang.org/images/mario/stand/right.gif"
-- UPDATE
checkBounds w blocks =
case blocks of
[] -> []
x::xs -> if x <= 0 then checkBounds w xs else x::(checkBounds w xs)
checkCollision : Float -> Float -> Float -> List Float -> Bool
checkCollision w mx my blocks =
case blocks of
[] -> False
x::xs -> (abs (x - 100/6) <= 1 && my < 50) || (checkCollision w mx my xs)
update computer mario =
if mario.gameOver then if computer.keyboard.space then init else mario else
let
w = computer.screen.width
dt = 1.666
vy =
if mario.y == 0 then
if computer.keyboard.space then 5 else 0
else
mario.vy - dt / 8
y = mario.y + dt * vy
c = if mario.countdown <= 0 then 5.0 else mario.countdown - dt/50
blocks =
if mario.countdown <= 0
then mario.blocks ++ [100]
else map (\x -> x - dt/5) (checkBounds w mario.blocks)
in
{ y = max 0 y
, vy = vy
, score = mario.score + dt/50
, countdown = c
, blocks = blocks
, gameOver = mario.gameOver || checkCollision w (-w/3) mario.y blocks
}