@@ -12,11 +12,11 @@ const FONT_SIZE = '45px';
12
12
13
13
const PADDLE_RIGHT_COLOR = 'RED' ;
14
14
const PADDLE_LEFT_COLOR = 'WHITE' ;
15
- const PADDLE_WIDTH = '20' ;
16
- const PADDLE_HEIGHT = ' 100' ;
15
+ const PADDLE_WIDTH = 20 ;
16
+ const PADDLE_HEIGHT = 100 ;
17
17
18
18
const BALL_COLOR = 'WHITE' ;
19
- const BALL_RADIUS = '10' ;
19
+ const BALL_RADIUS = 10 ;
20
20
const BALL_DELTA_VELOCITY = 0.5 ;
21
21
const BALL_VELOCITY = 5 ;
22
22
@@ -70,22 +70,6 @@ const playerB = {
70
70
let localPlayer = playerA ;
71
71
let computer = playerB ;
72
72
73
- /* //rectangle fons
74
- ctx.fillStyle = 'BLACK';
75
- ctx.fillRect(10, 20, 150, 200);
76
-
77
- //cercle
78
- ctx.fillStyle = 'WHITE';
79
- ctx.beginPath();
80
- ctx.arc(60,70,10,0,2*Math.PI);
81
- ctx.closePath();
82
- ctx.fill();
83
-
84
- //text
85
- ctx.fillStyle = 'BLUE';
86
- ctx.font = '45px impact';
87
- ctx.fillText("Saludos", 200, 200); */
88
-
89
73
//HELPER CANVAS
90
74
function drawRect ( x , y , w , h , color ) {
91
75
ctx . fillStyle = color ;
@@ -106,36 +90,60 @@ function drawText(text, x, y, color=FONT_COLOR, fontSize=FONT_SIZE, fontFamily=F
106
90
ctx . fillText ( text , x , y ) ;
107
91
}
108
92
109
-
110
-
111
-
112
-
113
-
114
93
//HELPERS PONG
115
94
function clearCanvas ( ) {
116
- drawRect ( 0 , 0 , cvs . width , cvs . height , 'BLACK' ) ;
95
+ drawRect ( 0 , 0 , cvs . width , cvs . height , BG_COLOR ) ;
117
96
}
97
+
118
98
function drawNet ( ) {
119
99
for ( let i = 0 ; i <= cvs . height ; i += net . padding ) {
120
100
drawRect ( net . x , net . y + i , net . width , net . height , net . color ) ;
121
101
}
122
102
}
123
103
124
104
function drawScore ( ) {
125
- drawText ( localPlayer . score , cvs . width / 4 , cvs . height / 5 , 'WHITE' ) ;
126
- drawText ( computer . score , 3 * cvs . width / 4 , cvs . height / 5 , 'WHITE' ) ;
105
+ drawText ( localPlayer . score , cvs . width / 4 , cvs . height / 6 , 'WHITE' ) ;
106
+ drawText ( computer . score , 3 * cvs . width / 4 , cvs . height / 6 , 'WHITE' ) ;
127
107
}
128
108
129
109
function drawPaddle ( paddle ) {
130
110
drawRect ( paddle . x , paddle . y , paddle . width , paddle . height , paddle . color ) ;
131
111
}
132
112
133
113
function drawBall ( ) {
134
- drawCircle ( ball . x , ball . y , ball . radius , ball . color ) ;
114
+ drawCircle ( ball . x , ball . y , ball . radius , ball . color ) ;
135
115
}
136
116
137
- function updateComputer ( ) {
138
- computer . y += ball . y - ( computer . y + computer . height / 2 ) * COMPUTER_LEVEL ;
117
+ //Play HELPERS
118
+ function initPaddleMovement ( ) {
119
+ cvs . addEventListener ( 'mousemove' , updateLocalPlayerPos ) ;
120
+ }
121
+
122
+ function updateLocalPlayerPos ( event ) {
123
+ const rect = cvs . getBoundingClientRect ( ) ;
124
+
125
+ localPlayer . y = event . clientY - localPlayer . height / 2 - rect . top ;
126
+ }
127
+
128
+ function pause ( milliseconds ) {
129
+ stopGameLoop ( ) ;
130
+ setTimeout ( ( ) => {
131
+ initGameLoop ( ) ;
132
+ } , milliseconds ) ;
133
+ }
134
+
135
+ function newBall ( ) {
136
+ console . log ( 'Tanto!' ) ;
137
+
138
+ ball . x = cvs . width / 2 ;
139
+ ball . y = cvs . height / 2 ;
140
+
141
+ const direction = ball . velocityX > 0 ? - 1 : 1 ;
142
+ ball . velocityX = direction * BALL_VELOCITY ;
143
+ ball . velocityY = BALL_VELOCITY ;
144
+ ball . speed = BALL_VELOCITY ;
145
+
146
+ pause ( 500 ) ;
139
147
}
140
148
141
149
function collision ( b , p ) { //bola i pala
@@ -152,6 +160,14 @@ function collision(b, p){ //bola i pala
152
160
return b . right > p . left && b . bottom > p . top && b . left < p . right && b . top < p . bottom ;
153
161
}
154
162
163
+ function updateComputer ( ) {
164
+ computer . y += ( ball . y - ( computer . y + computer . height / 2 ) ) * COMPUTER_LEVEL ;
165
+ }
166
+
167
+ function isGameOver ( ) {
168
+ return localPlayer . score >= NUM_BALLS || computer . score >= NUM_BALLS ;
169
+ }
170
+
155
171
function update ( ) {
156
172
//actualizamos la posición de la bola
157
173
ball . x += ball . velocityX ;
@@ -165,21 +181,32 @@ function update(){
165
181
ball . velocityY = - ball . velocityY ;
166
182
}
167
183
184
+ //comprobar si la pelota choca contra una pala
168
185
let whatPlayer = ( ball . x < cvs . width / 2 ) ? playerA : playerB ;
169
-
186
+
170
187
if ( collision ( ball , whatPlayer ) ) {
171
- let collidePoint = ball . y - whatPlayer . y + whatPlayer . height / 2 ;
172
- collidePoint = collidePoint / whatPlayer . height / 2 ;
188
+ //calcular punt de colisió de la Y
189
+ let collidePoint = ball . y - ( whatPlayer . y + whatPlayer . height / 2 ) ;
190
+ //normalitzem el punt de colisio
191
+ collidePoint = collidePoint / ( whatPlayer . height / 2 ) ;
173
192
//calcule l'angle en rad
174
- const anglePad = collidePoint * Math . PI / 4 ;
193
+ const angleRad = collidePoint * Math . PI / 4 ;
194
+ //calculem la direccio de la pilota
175
195
const direction = ( ball . x < cvs . width / 2 ) ? 1 : - 1 ; //MEdiacapabilities o algo en lloc de cvs
176
-
177
- ball . velocityX = direction * ball . speed * Math . cos ( anglePad ) ;
178
- ball . velocityY = ball . speed * Math . sin ( anglePad ) ;
179
-
196
+ //modificar la velocitat de la pilota
197
+ ball . velocityX = direction * ball . speed * Math . cos ( angleRad ) ;
198
+ ball . velocityY = ball . speed * Math . sin ( angleRad ) ;
180
199
//cada vez le damos a la pala incrementamos la velocidad de la bola
181
200
ball . speed += BALL_DELTA_VELOCITY ;
182
201
}
202
+ //Actualitzem el marcador
203
+ if ( ball . x - ball . radius < 0 ) {
204
+ computer . score ++ ;
205
+ newBall ( ) ;
206
+ } else if ( ball . x + ball . radius > cvs . width ) {
207
+ localPlayer . score ++ ;
208
+ newBall ( ) ;
209
+ }
183
210
}
184
211
185
212
function render ( ) { //codi refactoritzat
@@ -190,35 +217,53 @@ function render() { //codi refactoritzat
190
217
191
218
drawPaddle ( localPlayer ) ;
192
219
drawPaddle ( computer ) ;
193
-
194
- drawBall ( ) ;
195
- //drawText('Saludos',200,100); //no li passem color i ixiran per defecte
220
+ //si hemos termiando la partida
221
+ if ( isGameOver ( ) ) {
222
+ endGame ( ) ;
223
+ } else {
224
+ drawBall ( ) ;
225
+ }
196
226
}
197
227
198
- function drawBoard ( ) {
199
- clearCanvas ( ) ;
200
- drawNet ( ) ;
228
+ function endGame ( ) {
229
+ console . log ( 'Game Over' ) ;
201
230
202
- drawScore ( ) ;
231
+ //mostrem el final del joc
232
+ drawText ( 'GAME OVER' , cvs . width / 3 , cvs . height / 2 , 'BLUE' ) ;
203
233
204
- drawPaddle ( localPlayer ) ;
205
- drawPaddle ( computer ) ;
234
+ // detenemos el bucle de juego
235
+ stopGameLoop ( ) ;
206
236
}
207
237
208
- var gameLoopId ;
238
+ let gameLoopId ;
209
239
210
240
function gameLoop ( ) {
211
241
update ( ) ;
212
242
render ( ) ;
213
243
}
214
244
245
+ function stopGameLoop ( ) {
246
+ clearInterval ( gameLoopId ) ;
247
+ }
248
+
215
249
function initGameLoop ( ) {
216
250
gameLoopId = setInterval ( gameLoop , 1000 / FRAME_PER_SECOND ) ;
217
251
}
218
252
219
253
function play ( ) {
220
254
drawBoard ( ) ;
255
+ initPaddleMovement ( ) ;
221
256
initGameLoop ( ) ;
222
257
}
223
258
259
+ function drawBoard ( ) {
260
+ clearCanvas ( ) ;
261
+ drawNet ( ) ;
262
+
263
+ drawScore ( ) ;
264
+
265
+ drawPaddle ( localPlayer ) ;
266
+ drawPaddle ( computer ) ;
267
+ }
268
+
224
269
play ( ) ;
0 commit comments