-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPart3-3.elm
227 lines (132 loc) · 4.67 KB
/
Part3-3.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import Graphics.Element exposing (..)
import Time
import Window
import Color exposing (..)
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Keyboard
import Text exposing (monospace, fromString)
import Time exposing (..)
import Window
import Signal exposing ((<~), (~), foldp, sampleOn)
import Debug exposing (log)
type H = H
--
-- Animating the ball
--
-- First some helper functions
-- are n and m near each other?
-- specifically are they within c of each other?
near : Float -> Float -> Float -> Bool
near n c m = m >= n-c && m <= n+c
-- is the ball within a paddle?
-- |> is 'like' thread last in Clojure
within : Ball -> Player -> Bool
within ball player =
(ball.x |> near player.x 8) && (ball.y |> near player.y 20)
-- change the direction of a velocity based on collisions
stepV : Float -> Bool -> Bool -> Float
stepV v lowerCollision upperCollision =
if | lowerCollision -> abs v
| upperCollision -> 0 - abs v
| otherwise -> v
stepBall : Time -> Ball -> Player -> Player -> Ball
stepBall t ({x,y,vx,vy} as ball) player1 player2 =
if not (ball.x |> near 0 halfWidth)
then { ball | x <- 0, y <- 0 }
else
let vx' = stepV vx (ball `within` player1) (ball `within` player2)
vy' = stepV vy (y < 7-halfHeight) (y > halfHeight-7)
in
stepObj t { ball | vx <- vx', vy <- vy' }
-- Exercise 3.3
-- Update `stepGame` to take into account animating the ball using the supporting functions above
stepGame : Input -> Game -> Game
stepGame {paddle1,paddle2,delta}
({player1,player2,ball} as game) =
let paddle1' = dirToInt paddle1
paddle2' = dirToInt paddle2
player1' = stepPlyr delta paddle1 player1
player2' = stepPlyr delta paddle2 player2
in
{ game | player1 <- player1'
, player2 <- player2'
, ball <- H}
main = Signal.map2 display Window.dimensions gameState
-- Enjoy the game!!!
-- Want more?
-- If you would like to explore this example further, a good first step is to make it match the original Pong game
-- found on the Elm website. To reach parity, try and implement:
-- 1. The ability to pause the game by pressing SPACE
-- 2. Displaying of scores
-- more at: http://elm-lang.org/blog/Pong.elm
--
-- Supporting functions and data structures from previous exercises
--
type alias Object a = { a | x:Float, y:Float, vx:Float, vy:Float }
type alias Score = Int
type alias Player = Object {}
type alias Ball = Object {}
player : Float -> Player
player x = { x=x, y=0, vx=0, vy=0 }
defaultBall : Ball
defaultBall = { x=0, y=0, vx=200, vy=200 }
type alias Game = { player1:Player, player2:Player, ball: Ball }
defaultGame : Game
defaultGame =
{ player1 = player (20-halfWidth),
player2 = player (halfWidth-20),
ball = defaultBall
}
-- generic function that moves objects
stepObj : Time -> Object a -> Object a
stepObj t ({x,y,vx,vy} as obj) =
{ obj | x <- x + vx * t
, y <- y + vy * t }
-- step a player forward, making sure it does not fly off the court
stepPlyr : Time -> Direction -> Player -> Player
stepPlyr t dir player =
let dir' = dirToInt dir
player' = stepObj t { player | vy <- toFloat dir' * 200 }
y' = clamp (22-halfHeight) (halfHeight-22) player'.y -- explain clamp
in
{ player' | y <- y'}
gameState : Signal Game
gameState = foldp stepGame defaultGame input
displayObj : Object a -> Shape -> Form
displayObj obj shape =
move (obj.x,obj.y) (filled white shape) -- H
display : (Int,Int) -> Game -> Element
display (w,h) {player1,player2, ball} =
container w h middle <|
collage gameWidth gameHeight
[ filled pongGreen (rect gameWidth gameHeight),
displayObj player1 (rect 10 40),
displayObj player2 (rect 10 40),
displayObj ball (oval 15 15)
]
(gameWidth,gameHeight) = (600,400)
(halfWidth,halfHeight) = (300,200)
pongGreen = rgb 60 100 60
textGreen = rgb 160 200 160
type Direction = Up | Stopped | Down
type alias Input = {paddle1:Direction, paddle2:Direction, delta:Time }
-- <~ same as map
delta : Signal Time
delta = inSeconds <~ fps 35
dirToInt : Direction -> Int
dirToInt dir =
case dir of
Up -> 1
Stopped -> 0
Down -> -1
intToDir : Int -> Direction
intToDir x =
if | x == 1 -> Up
| x == (-1) -> Down
| otherwise -> Stopped
-- <~ signal ~ signal -- same as map2. Think of it as mapN
input : Signal Input
input = sampleOn delta <| Input <~ Signal.map (intToDir << .y) Keyboard.wasd
~ Signal.map (intToDir << .y) Keyboard.arrows
~ delta