forked from Dimvy-Clothing-brand/nodoubtz-my-games
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.js
More file actions
139 lines (122 loc) · 3.5 KB
/
Copy pathgame.js
File metadata and controls
139 lines (122 loc) · 3.5 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
132
133
134
135
136
137
138
139
const canvas = document.getElementById('pong');
const ctx = canvas.getContext('2d');
// Game constants
const PADDLE_WIDTH = 12, PADDLE_HEIGHT = 100;
const BALL_SIZE = 14;
const PLAYER_X = 24, AI_X = canvas.width - 24 - PADDLE_WIDTH;
const PADDLE_SPEED = 7;
const BALL_SPEED = 6;
// Game state
let playerY = (canvas.height - PADDLE_HEIGHT) / 2;
let aiY = (canvas.height - PADDLE_HEIGHT) / 2;
let ballX = canvas.width / 2 - BALL_SIZE / 2;
let ballY = canvas.height / 2 - BALL_SIZE / 2;
let ballVelX = BALL_SPEED * (Math.random() > 0.5 ? 1 : -1);
let ballVelY = BALL_SPEED * (Math.random() * 2 - 1);
let playerScore = 0, aiScore = 0;
// Mouse control
canvas.addEventListener('mousemove', e => {
const rect = canvas.getBoundingClientRect();
const mouseY = e.clientY - rect.top;
playerY = mouseY - PADDLE_HEIGHT / 2;
if (playerY < 0) playerY = 0;
if (playerY > canvas.height - PADDLE_HEIGHT)
playerY = canvas.height - PADDLE_HEIGHT;
});
// Draw everything
function draw() {
// Background
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Net
ctx.strokeStyle = '#fff2';
ctx.beginPath();
for (let y = 0; y < canvas.height; y += 28) {
ctx.moveTo(canvas.width / 2, y);
ctx.lineTo(canvas.width / 2, y + 14);
}
ctx.stroke();
// Paddles
ctx.fillStyle = '#fff';
ctx.fillRect(PLAYER_X, playerY, PADDLE_WIDTH, PADDLE_HEIGHT);
ctx.fillRect(AI_X, aiY, PADDLE_WIDTH, PADDLE_HEIGHT);
// Ball
ctx.fillRect(ballX, ballY, BALL_SIZE, BALL_SIZE);
// Score
ctx.font = '36px Arial';
ctx.textAlign = 'center';
ctx.fillText(playerScore, canvas.width / 4, 50);
ctx.fillText(aiScore, canvas.width * 3 / 4, 50);
}
// Update game state
function update() {
// Ball movement
ballX += ballVelX;
ballY += ballVelY;
// Ball collision with top/bottom walls
if (ballY < 0) {
ballY = 0;
ballVelY = -ballVelY;
} else if (ballY + BALL_SIZE > canvas.height) {
ballY = canvas.height - BALL_SIZE;
ballVelY = -ballVelY;
}
// Ball collision with player paddle
if (
ballX < PLAYER_X + PADDLE_WIDTH &&
ballX > PLAYER_X &&
ballY + BALL_SIZE > playerY &&
ballY < playerY + PADDLE_HEIGHT
) {
ballX = PLAYER_X + PADDLE_WIDTH;
ballVelX = -ballVelX;
// Add some "english"
let deltaY = ballY + BALL_SIZE / 2 - (playerY + PADDLE_HEIGHT / 2);
ballVelY = deltaY * 0.25;
}
// Ball collision with AI paddle
if (
ballX + BALL_SIZE > AI_X &&
ballX + BALL_SIZE < AI_X + PADDLE_WIDTH &&
ballY + BALL_SIZE > aiY &&
ballY < aiY + PADDLE_HEIGHT
) {
ballX = AI_X - BALL_SIZE;
ballVelX = -ballVelX;
let deltaY = ballY + BALL_SIZE / 2 - (aiY + PADDLE_HEIGHT / 2);
ballVelY = deltaY * 0.25;
}
// Score check
if (ballX < 0) {
aiScore++;
resetBall();
} else if (ballX + BALL_SIZE > canvas.width) {
playerScore++;
resetBall();
}
// AI movement (simple follow)
let aiCenter = aiY + PADDLE_HEIGHT / 2;
if (aiCenter < ballY + BALL_SIZE / 2 - 12) {
aiY += PADDLE_SPEED;
} else if (aiCenter > ballY + BALL_SIZE / 2 + 12) {
aiY -= PADDLE_SPEED;
}
// Clamp AI paddle
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;
ballVelX = BALL_SPEED * (Math.random() > 0.5 ? 1 : -1);
ballVelY = BALL_SPEED * (Math.random() * 2 - 1);
}
// Main loop
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
// Start game
loop();