-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.elm
172 lines (135 loc) · 3.95 KB
/
Pong.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
-- See this document for more information on making Pong:
-- http://elm-lang.org/blog/Pong.elm
-- Compile this code on http://elm-lang.org/try
import Color exposing (..)
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Keyboard
import Mouse
import Text
import Time exposing (..)
import Window
-- SIGNALS
main =
-- Signal.map show input
Signal.map2 view Window.dimensions gameState
gameState : Signal Game
gameState =
Signal.foldp update defaultGame input
--}
delta =
Signal.map inSeconds (fps 35)
--}
input : Signal Input
input =
Signal.sampleOn delta <|
Signal.map4 Input
Keyboard.space
Mouse.y
Window.height
delta
-- MODEL
(gameWidth,gameHeight) = (600,400)
(halfWidth,halfHeight) = (300,200)
type State = Play | Pause
type alias Ball =
{ x:Float, y:Float, vx:Float, vy:Float }
type alias Player =
{ x:Float, y:Float, score:Int }
type alias Game =
{ state:State, ball:Ball, player1:Player, player2:Player }
player : Float -> Player
player x =
{ x = x, y = 0, score = 0 }
defaultGame : Game
defaultGame =
{ state = Pause
, ball = { x=0, y=0, vx=200, vy=200 }
, player1 = player (20-halfWidth)
, player2 = player (halfWidth-20)
}
type alias Input =
{ space : Bool
, mouseY : Int
, windowH : Int
, delta : Time
}
-- UPDATE
update : Input -> Game -> Game
update {space,mouseY,windowH,delta} ({state,ball,player1,player2} as game) =
let score1 = if ball.x > halfWidth then 1 else 0
score2 = if ball.x < -halfWidth then 1 else 0
newState =
if | space -> Play
| score1 /= score2 -> Pause
| otherwise -> state
newBall =
if state == Pause
then ball
else updateBall delta ball player1 player2
in
{ game |
state <- newState,
ball <- newBall,
player1 <- updatePlayer mouseY windowH score1 player1,
player2 <- updatePlayer mouseY windowH score2 player2
}
updateBall : Time -> Ball -> Player -> Player -> Ball
updateBall t ({x,y,vx,vy} as ball) p1 p2 =
if not (ball.x |> near 0 halfWidth)
then { ball | x <- 0, y <- 0 }
else physicsUpdate t
{ ball |
vx <- stepV vx (ball `within` p1) (ball `within` p2),
vy <- stepV vy (y < 7-halfHeight) (y > halfHeight-7)
}
updatePlayer : Int -> Int -> Int -> Player -> Player
updatePlayer mouseY windowH points player =
let player1 = { player | y <- toFloat windowH / 2 - toFloat mouseY }
in
{ player1 |
y <- clamp (22-halfHeight) (halfHeight-22) player1.y,
score <- player.score + points
}
physicsUpdate t ({x,y,vx,vy} as obj) =
{ obj |
x <- x + vx * t,
y <- y + vy * t
}
near k c n =
n >= k-c && n <= k+c
within ball paddle =
near paddle.x 8 ball.x && near paddle.y 20 ball.y
stepV v lowerCollision upperCollision =
if | lowerCollision -> abs v
| upperCollision -> 0 - abs v
| otherwise -> v
-- VIEW
view : (Int,Int) -> Game -> Element
view (w,h) {state,ball,player1,player2} =
let scores : Element
scores = txt (Text.height 50) (toString player1.score ++ " " ++ toString player2.score)
in
container w h middle <|
collage gameWidth gameHeight
[ rect gameWidth gameHeight
|> filled pongGreen
, oval 15 15
|> make ball
, rect 10 40
|> make player1
, rect 10 40
|> make player2
, toForm scores
|> move (0, gameHeight/2 - 40)
, toForm (if state == Play then spacer 1 1 else txt identity msg)
|> move (0, 40 - gameHeight/2)
]
pongGreen = rgb 60 100 60
textGreen = rgb 160 200 160
txt f = Text.fromString >> Text.color textGreen >> Text.monospace >> f >> leftAligned
msg = "SPACE to start, mouse to move"
make obj shape =
shape
|> filled white
|> move (obj.x,obj.y)