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 2f9b1ba

Browse files
committed
Finish adding game content from Game State chapter
1 parent 7d54940 commit 2f9b1ba

File tree

1 file changed

+124
-23
lines changed

1 file changed

+124
-23
lines changed

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

Lines changed: 124 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ type Direction
3232
| Right
3333

3434

35+
type GameState
36+
= StartScreen
37+
| Playing
38+
| Success
39+
| GameOver
40+
41+
3542
type alias Model =
3643
{ characterDirection : Direction
3744
, characterPositionX : Int
3845
, characterPositionY : Int
46+
, gameState : GameState
3947
, itemPositionX : Int
4048
, itemPositionY : Int
4149
, itemsCollected : Int
@@ -49,6 +57,7 @@ initialModel =
4957
{ characterDirection = Right
5058
, characterPositionX = 50
5159
, characterPositionY = 300
60+
, gameState = StartScreen
5261
, itemPositionX = 500
5362
, itemPositionY = 300
5463
, itemsCollected = 0
@@ -78,7 +87,7 @@ update : Msg -> Model -> ( Model, Cmd Msg )
7887
update msg model =
7988
case msg of
8089
CountdownTimer time ->
81-
if model.timeRemaining > 0 then
90+
if model.gameState ==Playing&& model.timeRemaining > 0 then
8291
( { model | timeRemaining = model.timeRemaining - 1 }, Cmd.none )
8392

8493
else
@@ -93,26 +102,56 @@ update msg model =
93102
, Random.generate SetNewItemPositionX (Random.int 50 500)
94103
)
95104

105+
else if model.itemsCollected >= 10 then
106+
( { model | gameState = Success }, Cmd.none )
107+
108+
else if model.itemsCollected < 10 && model.timeRemaining == 0 then
109+
( { model | gameState = GameOver }, Cmd.none )
110+
96111
else
97112
( model, Cmd.none )
98113

99114
KeyDown key ->
100115
case key of
101116
"ArrowLeft" ->
102-
( { model
103-
| characterDirection = Left
104-
, characterPositionX = model.characterPositionX - 15
105-
}
106-
, Cmd.none
107-
)
117+
if model.gameState == Playing then
118+
( { model
119+
| characterDirection = Left
120+
, characterPositionX = model.characterPositionX - 15
121+
}
122+
, Cmd.none
123+
)
124+
125+
else
126+
( model, Cmd.none )
108127

109128
"ArrowRight" ->
110-
( { model
111-
| characterDirection = Right
112-
, characterPositionX = model.characterPositionX + 15
113-
}
114-
, Cmd.none
115-
)
129+
if model.gameState == Playing then
130+
( { model
131+
| characterDirection = Right
132+
, characterPositionX = model.characterPositionX + 15
133+
}
134+
, Cmd.none
135+
)
136+
137+
else
138+
( model, Cmd.none )
139+
140+
" " ->
141+
if model.gameState /= Playing then
142+
( { model
143+
| characterDirection = Right
144+
, characterPositionX = 50
145+
, itemsCollected = 0
146+
, gameState = Playing
147+
, playerScore = 0
148+
, timeRemaining = 10
149+
}
150+
, Cmd.none
151+
)
152+
153+
else
154+
( model, Cmd.none )
116155

117156
_ ->
118157
( model, Cmd.none )
@@ -174,15 +213,53 @@ view model =
174213
viewGame : Model -> Svg Msg
175214
viewGame model =
176215
svg [ version "1.1", width "600", height "400" ]
177-
[ viewGameWindow
178-
, viewGameSky
179-
, viewGameGround
180-
, viewCharacter model
181-
, viewItem model
182-
, viewGameScore model
183-
, viewItemsCollected model
184-
, viewGameTime model
185-
]
216+
(viewGameState model)
217+
218+
219+
220+
-- GAME STATES
221+
222+
223+
viewGameState : Model -> List (Svg Msg)
224+
viewGameState model =
225+
case model.gameState of
226+
StartScreen ->
227+
[ viewGameWindow
228+
, viewGameSky
229+
, viewGameGround
230+
, viewCharacter model
231+
, viewItem model
232+
, viewStartScreenText
233+
]
234+
235+
Playing ->
236+
[ viewGameWindow
237+
, viewGameSky
238+
, viewGameGround
239+
, viewCharacter model
240+
, viewItem model
241+
, viewGameScore model
242+
, viewItemsCollected model
243+
, viewGameTime model
244+
]
245+
246+
Success ->
247+
[ viewGameWindow
248+
, viewGameSky
249+
, viewGameGround
250+
, viewCharacter model
251+
, viewItem model
252+
, viewSuccessScreenText
253+
]
254+
255+
GameOver ->
256+
[ viewGameWindow
257+
, viewGameSky
258+
, viewGameGround
259+
, viewCharacter model
260+
, viewItem model
261+
, viewGameOverScreenText
262+
]
186263

187264

188265

@@ -225,7 +302,7 @@ viewGameGround =
225302

226303

227304

228-
-- DISPLAY GAME DATA
305+
-- DISPLAY GAME TEXT DATA
229306

230307

231308
viewGameText : Int -> Int -> String -> Svg Msg
@@ -240,6 +317,30 @@ viewGameText positionX positionY str =
240317
[ Svg.text str ]
241318

242319

320+
viewStartScreenText : Svg Msg
321+
viewStartScreenText =
322+
Svg.svg []
323+
[ viewGameText 140 160 "Collect ten coins in ten seconds!"
324+
, viewGameText 140 180 "Press the SPACE BAR key to start."
325+
]
326+
327+
328+
viewSuccessScreenText : Svg Msg
329+
viewSuccessScreenText =
330+
Svg.svg []
331+
[ viewGameText 260 160 "Success!"
332+
, viewGameText 140 180 "Press the SPACE BAR key to restart."
333+
]
334+
335+
336+
viewGameOverScreenText : Svg Msg
337+
viewGameOverScreenText =
338+
Svg.svg []
339+
[ viewGameText 260 160 "Game Over"
340+
, viewGameText 140 180 "Press the SPACE BAR key to restart."
341+
]
342+
343+
243344
viewGameScore : Model -> Svg Msg
244345
viewGameScore model =
245346
let

0 commit comments

Comments
(0)

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