@@ -284,24 +284,38 @@ gameState = foldp stepGame defaultGame input
284284------ Displaying the Game ------
285285------------------------------------------------------------------------
286286
287−-- This function takes a GameState and turns it into something a user
287+-- These functions take a GameState and turn it into something a user
288288-- can see and understand. It is totally independent of how the game
289289-- updates, it only needs to know the current game state. This allows us
290290-- to change how the game looks without changing any of the logic of the
291291-- game.
292292
293+
294+-- This function displays the current score and directions.
295+
296+scoreBoard w inPlay p1 p2 =
297+ let { code = text . monospace . toText
298+ ; stack top bottom = flow down [ code " ", code top, code bottom ]
299+ ; msg = width w . box 2 . text . monospace $ toText "Press SPACE to begin"
300+ ; score = width w . box 2 $ flow right
301+ [ stack "W" "S", rectangle 20 1
302+ , text . Text.height 4 $ show p1 ++ toText " " ++ show p2
303+ , rectangle 20 1, stack "↑" "↓" ]
304+ }
305+ in if inPlay then score else score `above` msg
306+
307+
308+-- This function displays the entire GameState.
309+
293310display (w,h) (GameState state (Score p1 p2) (Ball pos _) (Paddle y1) (Paddle y2)) =
294− let score = width w . centeredText . Text.height 4 $
295− show p1 ++ toText " " ++ show p2
296− in layers
297− [ if state == Play then score else
298− score `above` (width w . centeredText $ toText "Press SPACE to begin.")
311+ layers
312+ [ scoreBoard w (state == Play) p1 p2
299313 , let pongGreen = rgb 60 100 60 in
300314 size w h . box 5 $ collage gameWidth gameHeight
301315 [ filled pongGreen (rect gameWidth gameHeight (halfWidth,halfHeight))
302− , filled white (oval 15 15 pos)
303− , filled white (rect 10 40 ( 20, y1))
304− , filled white (rect 10 40 (gameWidth - 20, y2))
316+ , filled white (oval 15 15 pos) -- ball
317+ , filled white (rect 10 40 ( 20, y1)) -- first paddle
318+ , filled white (rect 10 40 (gameWidth - 20, y2)) -- second paddle
305319 ]
306320 ]
307321