Skip to content

Commit ff76dca

Browse files
committed
Episode 5: Moving with key strokes
1 parent d1b908b commit ff76dca

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

elm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"elm/core": "1.0.2",
1111
"elm/html": "1.0.0",
1212
"elm/http": "2.0.0",
13+
"elm/json": "1.1.3",
1314
"elm/svg": "1.0.1",
1415
"elm/time": "1.0.0"
1516
},
1617
"indirect": {
1718
"elm/bytes": "1.0.8",
1819
"elm/file": "1.0.5",
19-
"elm/json": "1.1.3",
2020
"elm/url": "1.0.0",
2121
"elm/virtual-dom": "1.0.2"
2222
}

src/Main.elm

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module Main exposing (..)
22

33
import Browser
4+
import Browser.Events exposing (onKeyDown)
45
import Html exposing (Html, button, div, text)
56
import Html.Events exposing (onClick)
7+
import Json.Decode as Decode
68
import List exposing (map, range)
79
import String exposing (fromInt)
810
import Svg
@@ -156,9 +158,14 @@ init _ =
156158

157159

158160
type Msg
159-
= Left
160-
| Right
161-
| GravityTick Time.Posix
161+
= GravityTick Time.Posix
162+
| KeyDown Key
163+
| Noop
164+
165+
166+
type Key
167+
= LeftArrow
168+
| RightArrow
162169

163170

164171
update : Msg -> Model -> ( Model, Cmd Msg )
@@ -169,25 +176,52 @@ update msg model =
169176
, Cmd.none
170177
)
171178

172-
Left ->
173-
( model
179+
KeyDown key ->
180+
( movePiece key model
174181
, Cmd.none
175182
)
176183

177-
Right ->
184+
Noop ->
178185
( model
179186
, Cmd.none
180187
)
181188

182189

190+
movePiece : Key -> Model -> Model
191+
movePiece key ({ currentPiece } as model) =
192+
let
193+
( x, y ) =
194+
currentPiece.position
195+
196+
newPosition =
197+
case key of
198+
LeftArrow ->
199+
( x - 1, y )
200+
201+
RightArrow ->
202+
( x + 1, y )
203+
204+
movedPiece =
205+
{ currentPiece | position = newPosition }
206+
in
207+
{ model | currentPiece = movedPiece }
208+
209+
183210
dropCurrentPiece : Model -> Model
184211
dropCurrentPiece ({ currentPiece } as model) =
185212
let
186213
( x, y ) =
187214
currentPiece.position
188215

216+
nextRow =
217+
if y == 0 then
218+
20
219+
220+
else
221+
y - 1
222+
189223
droppedPiece =
190-
{ currentPiece | position = ( x, y - 1 ) }
224+
{ currentPiece | position = ( x, nextRow ) }
191225
in
192226
{ model | currentPiece = droppedPiece }
193227

@@ -198,17 +232,40 @@ dropCurrentPiece ({ currentPiece } as model) =
198232

199233
subscriptions : Model -> Sub Msg
200234
subscriptions model =
201-
Time.every 1000 GravityTick
235+
Sub.batch
236+
[ Time.every 1000 GravityTick
237+
, onKeyDown keyDecoder
238+
]
239+
240+
241+
keyDecoder : Decode.Decoder Msg
242+
keyDecoder =
243+
Decode.map toKey (Decode.field "key" Decode.string)
244+
245+
246+
toKey : String -> Msg
247+
toKey string =
248+
case string of
249+
"ArrowLeft" ->
250+
KeyDown LeftArrow
251+
252+
"ArrowRight" ->
253+
KeyDown RightArrow
254+
255+
_ ->
256+
Noop
202257

203258

204259

260+
-- Decode.succeed KeyDown
205261
-- VIEW
206262

207263

208264
view : Model -> Html Msg
209265
view model =
210266
div []
211-
[ div [] [ text "LOOK MUM, NO SERVER" ]
267+
[ div [] [ text "Developing a Web Tetris in Elm" ]
268+
, div [] [ text "Continuing TONIGHT at 7PM (EST)" ]
212269
, boardView model.board model.currentPiece
213270
]
214271

0 commit comments

Comments
 (0)