@@ -56,7 +56,6 @@
56module Pong where
56module Pong where
58import JavaScript
58import JavaScript
62-- Set the frames per second (FPS) to 60, calculate the deltas (the
61-- Set the frames per second (FPS) to 60, calculate the deltas (the
@@ -70,53 +69,19 @@ delta = lift inSeconds (fps 60)
70------ Modelling User Input ------
69------ Modelling User Input ------
71------------------------------------------------------------------------
70------------------------------------------------------------------------
73−-- Each paddle can be moving up, down, or not at all. We'll call this
72+-- During gameplay, all keyboard input is about the position of the
74−-- the `direction' of the paddle.
73+-- two paddles. So the keyboard input can be reduced to two directions,
74+-- each represented by an integer in {-1,0,1}. Furthermore, the SPACE
75+-- key is used to start the game between rounds, so we also need a
76+-- boolean value to represent whether it is pressed.
76−data Direction = Up | Neutral | Down
78+data KeyInput = KeyInput Bool Int Int
78−-- During gameplay, all keyboard input is about the position of the two
79−-- paddles. So the keyboard input can be reduced to two `Directions'.
80−-- Furthermore, the SPACE key is used to start the game between rounds,
81−-- so we also need a boolean value to represent whether it is pressed.
83−data KeyInput = KeyInput Bool Direction Direction
85−defaultKeyInput = KeyInput False Neutral Neutral
80+defaultKeyInput = KeyInput False 0 0
88−-- Now we determine how to update the direction of a paddle based on
89−-- keyboard input. The first two args of `updatePaddle` are the key
90−-- codes of the up and down keys. The next argument is the key that
91−-- has been pressed. The last argument is the previously calculated
92−-- direction of the paddle, which is getting updated.
94−updateDirection upKey downKey key direction =
96− Up -> if key == downKey then Neutral else Up
97− Down -> if key == upKey then Neutral else Down
98− Neutral -> if key == upKey then Up else
99− if key == downKey then Down else Neutral
102−updateDirection1 = updateDirection 87 83 -- 'w' for up and 's' for down
103−updateDirection2 = updateDirection 38 40 -- 'UP' for up and 'DOWN' for down
106−-- Update the keyboard input representation based on a particular key press.
108−updateInput key (KeyInput space dir1 dir2) =
109− KeyInput (space || key == 32)
110− (updateDirection1 key dir1)
111− (updateDirection2 key dir2)
114−-- `keysDown` has type (Signal [Int]) so we need to fold `updateInput`
115−-- over all of the keys that are currently pressed to get the current
118−keyInput = lift (foldl updateInput defaultKeyInput) keysDown
82+keyInput = lift3 KeyInput Keyboard.space
83+ (lift .y Keyboard.wasd)
84+ (lift .y Keyboard.arrows)
122------------------------------------------------------------------------
87------------------------------------------------------------------------
@@ -191,10 +156,7 @@ defaultGame = GameState BetweenRounds
191-- by keyboard input).
156-- by keyboard input).
193stepPaddle delta dir (Paddle y) =
158stepPaddle delta dir (Paddle y) =
195− Up -> Paddle $ clamp 20 (gameHeight-20) (y - 200 * delta)
196− Down -> Paddle $ clamp 20 (gameHeight-20) (y + 200 * delta)
159+ Paddle $ clamp 20 (gameHeight-20) (y + toFloat dir * 200 * delta)
200-- We must also step the ball forward. This is more complicated due to
162-- We must also step the ball forward. This is more complicated due to