Repository navigation
Expand file tree
/
Copy pathSnakeGame.elm
More file actions
72 lines (57 loc) · 1.6 KB
/
Copy pathSnakeGame.elm
File metadata and controls
72 lines (57 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module SnakeGame exposing (main)
import AnimationFrame
import Collage exposing (collage)
import Element exposing (toHtml)
import Html exposing (Html)
import Snake exposing (Snake, Model, Msg, view, init, step, update, subscriptions)
import Apple exposing (draw, Apple)
import Time exposing (Time)
main : Program Never Model Msg
main =
Html.program
{ init = ( init, Cmd.none )
, view = view
, update = update
, subscriptions =
\_ ->
Sub.batch
[ AnimationFrame.times CurrentTick
, Sub.map SnakeMsg Snake.subscriptions
]
}
type alias Model =
{ snake : Snake.Model, apple : Apple, lastUpdate : Time }
init : Model
init =
{ apple = ( 0, -1 )
, lastUpdate = 0
, snake = Snake.init
}
type Msg
= CurrentTick Time
| SnakeMsg Snake.Msg
update : Msg -> Model -> ( Model, Cmd a )
update msg model =
case msg of
CurrentTick time ->
if time - model.lastUpdate > 100 then
{ model | lastUpdate = time, snake = Snake.step model.snake } ! []
else
model ! []
SnakeMsg snakeMsg ->
let
( snakeModel, _ ) =
Snake.update snakeMsg model.snake
in
{ model | snake = snakeModel } ! []
view : Model -> Html a
view { snake, apple } =
let
blockSize =
25
blocks =
[ Apple.draw blockSize apple, Snake.view blockSize snake ]
in
blocks
|> collage 500 500
|> toHtml