Skip to content

Commit e8002d1

Browse files
committed
PONG Home-Màquina
1 parent bb3c00f commit e8002d1

File tree

1 file changed

+93
-48
lines changed

1 file changed

+93
-48
lines changed

pong.js

Lines changed: 93 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const FONT_SIZE = '45px';
1212

1313
const PADDLE_RIGHT_COLOR = 'RED';
1414
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;
1717

1818
const BALL_COLOR = 'WHITE';
19-
const BALL_RADIUS = '10';
19+
const BALL_RADIUS = 10;
2020
const BALL_DELTA_VELOCITY = 0.5;
2121
const BALL_VELOCITY = 5;
2222

@@ -70,22 +70,6 @@ const playerB = {
7070
let localPlayer = playerA;
7171
let computer = playerB;
7272

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-
8973
//HELPER CANVAS
9074
function drawRect(x,y,w,h, color){
9175
ctx.fillStyle = color;
@@ -106,36 +90,60 @@ function drawText(text, x, y, color=FONT_COLOR, fontSize=FONT_SIZE, fontFamily=F
10690
ctx.fillText(text, x, y);
10791
}
10892

109-
110-
111-
112-
113-
11493
//HELPERS PONG
11594
function clearCanvas(){
116-
drawRect(0,0,cvs.width,cvs.height,'BLACK');
95+
drawRect(0,0,cvs.width,cvs.height, BG_COLOR);
11796
}
97+
11898
function drawNet(){
11999
for(let i=0; i<=cvs.height; i+=net.padding){
120100
drawRect(net.x, net.y+i, net.width, net.height, net.color);
121101
}
122102
}
123103

124104
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');
127107
}
128108

129109
function drawPaddle(paddle){
130110
drawRect(paddle.x, paddle.y, paddle.width, paddle.height, paddle.color);
131111
}
132112

133113
function drawBall(){
134-
drawCircle(ball.x,ball.y,ball.radius, ball.color);
114+
drawCircle(ball.x, ball.y, ball.radius, ball.color);
135115
}
136116

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);
139147
}
140148

141149
function collision(b, p){ //bola i pala
@@ -152,6 +160,14 @@ function collision(b, p){ //bola i pala
152160
return b.right > p.left && b.bottom > p.top && b.left < p.right && b.top < p.bottom;
153161
}
154162

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+
155171
function update(){
156172
//actualizamos la posición de la bola
157173
ball.x += ball.velocityX;
@@ -165,21 +181,32 @@ function update(){
165181
ball.velocityY = -ball.velocityY;
166182
}
167183

184+
//comprobar si la pelota choca contra una pala
168185
let whatPlayer = (ball.x < cvs.width/2) ? playerA : playerB;
169-
186+
170187
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);
173192
//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
175195
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);
180199
//cada vez le damos a la pala incrementamos la velocidad de la bola
181200
ball.speed += BALL_DELTA_VELOCITY;
182201
}
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+
}
183210
}
184211

185212
function render() { //codi refactoritzat
@@ -190,35 +217,53 @@ function render() { //codi refactoritzat
190217

191218
drawPaddle(localPlayer);
192219
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+
}
196226
}
197227

198-
function drawBoard(){
199-
clearCanvas();
200-
drawNet();
228+
function endGame(){
229+
console.log('Game Over');
201230

202-
drawScore();
231+
//mostrem el final del joc
232+
drawText('GAME OVER', cvs.width/3, cvs.height/2, 'BLUE');
203233

204-
drawPaddle(localPlayer);
205-
drawPaddle(computer);
234+
// detenemos el bucle de juego
235+
stopGameLoop();
206236
}
207237

208-
var gameLoopId;
238+
let gameLoopId;
209239

210240
function gameLoop(){
211241
update();
212242
render();
213243
}
214244

245+
function stopGameLoop(){
246+
clearInterval(gameLoopId);
247+
}
248+
215249
function initGameLoop(){
216250
gameLoopId = setInterval(gameLoop, 1000/FRAME_PER_SECOND);
217251
}
218252

219253
function play() {
220254
drawBoard();
255+
initPaddleMovement();
221256
initGameLoop();
222257
}
223258

259+
function drawBoard(){
260+
clearCanvas();
261+
drawNet();
262+
263+
drawScore();
264+
265+
drawPaddle(localPlayer);
266+
drawPaddle(computer);
267+
}
268+
224269
play();

0 commit comments

Comments
 (0)