Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 7d54940

Browse files
committed
Add Elm content from Game Data chapter
1 parent 39e6f0f commit 7d54940

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

‎assets/elm/elm.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
"elm/http": "2.0.0",
1313
"elm/json": "1.1.2",
1414
"elm/random": "1.0.0",
15-
"elm/svg": "1.0.1"
15+
"elm/svg": "1.0.1",
16+
"elm/time": "1.0.0"
1617
},
1718
"indirect": {
1819
"elm/bytes": "1.0.7",
1920
"elm/file": "1.0.1",
20-
"elm/time": "1.0.0",
2121
"elm/url": "1.0.0",
2222
"elm/virtual-dom": "1.0.2"
2323
}

‎assets/elm/src/Games/Platformer.elm‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Json.Decode as Decode
77
import Random
88
import Svg exposing (..)
99
import Svg.Attributes exposing (..)
10+
import Time
1011

1112

1213

@@ -52,7 +53,7 @@ initialModel =
5253
, itemPositionY = 300
5354
, itemsCollected = 0
5455
, playerScore = 0
55-
, timeRemaining = 0
56+
, timeRemaining = 10
5657
}
5758

5859

@@ -66,7 +67,8 @@ init _ =
6667

6768

6869
type Msg
69-
= GameLoop Float
70+
= CountdownTimer Time.Posix
71+
| GameLoop Float
7072
| KeyDown String
7173
| NoOp
7274
| SetNewItemPositionX Int
@@ -75,9 +77,21 @@ type Msg
7577
update : Msg -> Model -> ( Model, Cmd Msg )
7678
update msg model =
7779
case msg of
80+
CountdownTimer time ->
81+
if model.timeRemaining > 0 then
82+
( { model | timeRemaining = model.timeRemaining - 1 }, Cmd.none )
83+
84+
else
85+
( model, Cmd.none )
86+
7887
GameLoop time ->
7988
if characterFoundItem model then
80-
( model, Random.generate SetNewItemPositionX (Random.int 50 500) )
89+
( { model
90+
| itemsCollected = model.itemsCollected + 1
91+
, playerScore = model.playerScore + 100
92+
}
93+
, Random.generate SetNewItemPositionX (Random.int 50 500)
94+
)
8195

8296
else
8397
( model, Cmd.none )
@@ -134,6 +148,7 @@ subscriptions model =
134148
Sub.batch
135149
[ Browser.Events.onKeyDown (Decode.map KeyDown keyDecoder)
136150
, Browser.Events.onAnimationFrameDelta GameLoop
151+
, Time.every 1000 CountdownTimer
137152
]
138153

139154

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /