Skip to content

Commit bfd7f8c

Browse files
Update script.js
1 parent bc5556a commit bfd7f8c

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

ping-pong/script.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
const canvas = document.getElementById('pong');
22
const ctx = canvas.getContext('2d');
33

4-
function resizeCanvas() {
5-
const width = Math.min(window.innerWidth * 0.9, 600);
6-
const height = Math.min(window.innerWidth * 0.5, 400);
7-
canvas.width = width;
8-
canvas.height = height;
9-
// Recenter paddles and ball after resize
10-
leftY = canvas.height / 2 - PADDLE_HEIGHT / 2;
11-
rightY = canvas.height / 2 - PADDLE_HEIGHT / 2;
12-
ballX = canvas.width / 2;
13-
ballY = canvas.height / 2;
14-
}
154
const PADDLE_WIDTH = 10;
165
const PADDLE_HEIGHT = 70;
176
const BALL_RADIUS = 8;
187

19-
let leftY, rightY, ballX, ballY;
8+
let leftY = 0, rightY = 0, ballX = 0, ballY = 0;
209
let ballSpeedX = 4;
2110
let ballSpeedY = 3;
2211
let leftScore = 0;
@@ -26,8 +15,21 @@ let upPressed = false, downPressed = false, wPressed = false, sPressed = false;
2615
// Simple AI difficulty (pixels per frame)
2716
const AI_SPEED = 5;
2817

18+
function resizeCanvas() {
19+
const width = Math.min(window.innerWidth * 0.9, 600);
20+
const height = Math.min(window.innerWidth * 0.5, 400);
21+
canvas.width = width;
22+
canvas.height = height;
23+
// Recenter paddles and ball after resize
24+
leftY = canvas.height / 2 - PADDLE_HEIGHT / 2;
25+
rightY = canvas.height / 2 - PADDLE_HEIGHT / 2;
26+
ballX = canvas.width / 2;
27+
ballY = canvas.height / 2;
28+
}
2929
resizeCanvas();
30-
window.addEventListener('resize', resizeCanvas);
30+
window.addEventListener('resize', () => {
31+
resizeCanvas();
32+
});
3133

3234
function draw() {
3335
ctx.clearRect(0, 0, canvas.width, canvas.height);
@@ -51,6 +53,7 @@ function draw() {
5153

5254
ctx.fillStyle = "#fff";
5355
ctx.fillRect(18, leftY, PADDLE_WIDTH, PADDLE_HEIGHT);
56+
ctx.fillStyle = "#f00"; // RIGHT PADDLE COLOR TO DEBUG
5457
ctx.fillRect(canvas.width - 18 - PADDLE_WIDTH, rightY, PADDLE_WIDTH, PADDLE_HEIGHT);
5558

5659
ctx.beginPath();

0 commit comments

Comments
 (0)