-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
131 lines (114 loc) · 3.48 KB
/
game.js
File metadata and controls
131 lines (114 loc) · 3.48 KB
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
const canvas = document.getElementById('pongCanvas');
const ctx = canvas.getContext('2d');
// Game settings
const PADDLE_WIDTH = 12;
const PADDLE_HEIGHT = 100;
const PADDLE_MARGIN = 20;
const BALL_SIZE = 16;
const BALL_SPEED = 6;
const AI_SPEED = 4;
// Game state
let playerY = canvas.height / 2 - PADDLE_HEIGHT / 2;
let aiY = canvas.height / 2 - PADDLE_HEIGHT / 2;
let ballX = canvas.width / 2 - BALL_SIZE / 2;
let ballY = canvas.height / 2 - BALL_SIZE / 2;
let ballSpeedX = BALL_SPEED * (Math.random() > 0.5 ? 1 : -1);
let ballSpeedY = BALL_SPEED * (Math.random() * 2 - 1);
// Draw everything
function draw() {
// Clear
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Net
ctx.strokeStyle = "#fff";
ctx.setLineDash([10, 15]);
ctx.beginPath();
ctx.moveTo(canvas.width/2, 0);
ctx.lineTo(canvas.width/2, canvas.height);
ctx.stroke();
ctx.setLineDash([]);
// Left paddle (player)
ctx.fillStyle = "#fff";
ctx.fillRect(PADDLE_MARGIN, playerY, PADDLE_WIDTH, PADDLE_HEIGHT);
// Right paddle (AI)
ctx.fillStyle = "#fff";
ctx.fillRect(canvas.width - PADDLE_MARGIN - PADDLE_WIDTH, aiY, PADDLE_WIDTH, PADDLE_HEIGHT);
// Ball
ctx.fillStyle = "#fff";
ctx.fillRect(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
// Handle mouse movement
canvas.addEventListener('mousemove', function(e) {
const rect = canvas.getBoundingClientRect();
let mouseY = e.clientY - rect.top;
playerY = mouseY - PADDLE_HEIGHT / 2;
// Clamp within canvas
if (playerY < 0) playerY = 0;
if (playerY > canvas.height - PADDLE_HEIGHT) playerY = canvas.height - PADDLE_HEIGHT;
});
// Update ball and paddles
function update() {
// Ball movement
ballX += ballSpeedX;
ballY += ballSpeedY;
// Ball collision with top/bottom
if (ballY < 0) {
ballY = 0;
ballSpeedY *= -1;
}
if (ballY + BALL_SIZE > canvas.height) {
ballY = canvas.height - BALL_SIZE;
ballSpeedY *= -1;
}
// Ball collision with player paddle (left)
if (
ballX < PADDLE_MARGIN + PADDLE_WIDTH &&
ballY + BALL_SIZE > playerY &&
ballY < playerY + PADDLE_HEIGHT
) {
ballX = PADDLE_MARGIN + PADDLE_WIDTH; // Prevent sticking
ballSpeedX *= -1;
// Add some "spin"
let collidePoint = (ballY + BALL_SIZE/2) - (playerY + PADDLE_HEIGHT/2);
ballSpeedY = collidePoint * 0.2;
}
// Ball collision with AI paddle (right)
if (
ballX + BALL_SIZE > canvas.width - PADDLE_MARGIN - PADDLE_WIDTH &&
ballY + BALL_SIZE > aiY &&
ballY < aiY + PADDLE_HEIGHT
) {
ballX = canvas.width - PADDLE_MARGIN - PADDLE_WIDTH - BALL_SIZE;
ballSpeedX *= -1;
// Add some "spin"
let collidePoint = (ballY + BALL_SIZE/2) - (aiY + PADDLE_HEIGHT/2);
ballSpeedY = collidePoint * 0.2;
}
// Score (reset if ball out of bounds)
if (ballX < 0 || ballX + BALL_SIZE > canvas.width) {
resetBall();
}
// AI movement (simple tracking)
let aiCenter = aiY + PADDLE_HEIGHT / 2;
if (aiCenter < ballY + BALL_SIZE / 2 - 10) {
aiY += AI_SPEED;
} else if (aiCenter > ballY + BALL_SIZE / 2 + 10) {
aiY -= AI_SPEED;
}
// Clamp AI paddle within canvas
if (aiY < 0) aiY = 0;
if (aiY > canvas.height - PADDLE_HEIGHT) aiY = canvas.height - PADDLE_HEIGHT;
}
function resetBall() {
ballX = canvas.width / 2 - BALL_SIZE / 2;
ballY = canvas.height / 2 - BALL_SIZE / 2;
ballSpeedX = BALL_SPEED * (Math.random() > 0.5 ? 1 : -1);
ballSpeedY = BALL_SPEED * (Math.random() * 2 - 1);
}
// Main loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Start the game
gameLoop();