@@ -284,24 +284,38 @@ gameState = foldp stepGame defaultGame input
284------ Displaying the Game ------
284------ Displaying the Game ------
285------------------------------------------------------------------------
285------------------------------------------------------------------------
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
288-- can see and understand. It is totally independent of how the game
288-- can see and understand. It is totally independent of how the game
289-- updates, it only needs to know the current game state. This allows us
289-- updates, it only needs to know the current game state. This allows us
290-- to change how the game looks without changing any of the logic of the
290-- to change how the game looks without changing any of the logic of the
294+-- This function displays the current score and directions.
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 "↑" "↓" ]
305+ in if inPlay then score else score `above` msg
308+-- This function displays the entire GameState.
293display (w,h) (GameState state (Score p1 p2) (Ball pos _) (Paddle y1) (Paddle y2)) =
310display (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
311+ layers
297− [ if state == Play then score else
312+ [ scoreBoard w (state == Play) p1 p2
298− score `above` (width w . centeredText $ toText "Press SPACE to begin.")
299 , let pongGreen = rgb 60 100 60 in
313 , let pongGreen = rgb 60 100 60 in
300 size w h . box 5 $ collage gameWidth gameHeight
314 size w h . box 5 $ collage gameWidth gameHeight
301 [ filled pongGreen (rect gameWidth gameHeight (halfWidth,halfHeight))
315 [ filled pongGreen (rect gameWidth gameHeight (halfWidth,halfHeight))
302− , filled white (oval 15 15 pos)
316+ , filled white (oval 15 15 pos) -- ball
303− , filled white (rect 10 40 ( 20, y1))
317+ , filled white (rect 10 40 ( 20, y1)) -- first paddle
304− , filled white (rect 10 40 (gameWidth - 20, y2))
318+ , filled white (rect 10 40 (gameWidth - 20, y2)) -- second paddle