-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
222 lines (186 loc) · 5.61 KB
/
script.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 400;
const chuckImgStill = new Image();
const chuckImgKick = new Image();
const dogeImgStill = new Image();
const dogeImgKick = new Image();
chuckImgStill.src = 'chuck still.gif';
chuckImgKick.src = 'chuck kick.gif';
dogeImgStill.src = 'doge still.png';
dogeImgKick.src = 'doge kick.png';
chuckImgStill.onload = chuckImgKick.onload = dogeImgStill.onload = dogeImgKick.onload = function() {
console.log('Images loaded');
};
const paddleWidth = 10;
const paddleHeight = 80;
const dogePaddle = {
x: 10,
y: canvas.height / 2 - paddleHeight / 2,
width: 80, // Increased size
height: 80,
dy: 0,
img: dogeImgStill
};
const chuckPaddle = {
x: canvas.width - 90,
y: canvas.height / 2 - paddleHeight / 2,
width: 90, // Increased size
height: 90,
dy: 0,
img: chuckImgStill,
speed: 4
};
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
size: 15,
dx: 4,
dy: 4,
speedIncrement: 0.5,
hitCount: 0
};
let dogeScore = 0;
let chuckScore = 0;
let isGameStarted = false;
let isSinglePlayer = true;
function drawPaddle(paddle, isDoge = false) {
ctx.drawImage(paddle.img, paddle.x, paddle.y, paddle.width, paddle.height);
}
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.size / 2, 0, Math.PI * 2);
ctx.fillStyle = '#fff';
ctx.fill();
ctx.closePath();
}
function drawScore() {
ctx.font = '32px Arial';
ctx.fillStyle = '#fff';
ctx.fillText(dogeScore, canvas.width / 4, 50);
ctx.fillText(chuckScore, (canvas.width / 4) * 3, 50);
}
function update() {
dogePaddle.y += dogePaddle.dy;
if (isSinglePlayer) {
if (ball.y < chuckPaddle.y + chuckPaddle.height / 2) {
chuckPaddle.dy = -chuckPaddle.speed;
} else if (ball.y > chuckPaddle.y + chuckPaddle.height / 2) {
chuckPaddle.dy = chuckPaddle.speed;
} else {
chuckPaddle.dy = 0;
}
chuckPaddle.y += chuckPaddle.dy;
} else {
chuckPaddle.y += chuckPaddle.dy;
}
if (dogePaddle.y < 0) dogePaddle.y = 0;
if (dogePaddle.y + dogePaddle.height > canvas.height) dogePaddle.y = canvas.height - dogePaddle.height;
if (chuckPaddle.y < 0) chuckPaddle.y = 0;
if (chuckPaddle.y + chuckPaddle.height > canvas.height) chuckPaddle.y = canvas.height - chuckPaddle.height;
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.y < 0 || ball.y + ball.size > canvas.height) {
ball.dy *= -1;
}
if (ball.x < dogePaddle.x + dogePaddle.width &&
ball.y > dogePaddle.y &&
ball.y < dogePaddle.y + dogePaddle.height) {
ball.dx *= -1;
ball.hitCount++;
dogePaddle.img = dogeImgKick;
setTimeout(() => dogePaddle.img = dogeImgStill, 100);
}
if (ball.x + ball.size > chuckPaddle.x &&
ball.y > chuckPaddle.y &&
ball.y < chuckPaddle.y + chuckPaddle.height) {
ball.dx *= -1;
ball.hitCount++;
chuckPaddle.img = chuckImgKick;
setTimeout(() => chuckPaddle.img = chuckImgStill, 500); // Increase delay to allow GIF to play
}
if (ball.hitCount >= 2) {
ball.dx += ball.speedIncrement * Math.sign(ball.dx);
ball.dy += ball.speedIncrement * Math.sign(ball.dy);
ball.hitCount = 0;
}
if (ball.x < 0) {
chuckScore++;
resetBall();
} else if (ball.x + ball.size > canvas.width) {
dogeScore++;
resetBall();
}
if (dogeScore === 6 || chuckScore === 6) {
alert('Game Over!');
dogeScore = 0;
chuckScore = 0;
isGameStarted = false;
showStartScreen();
}
}
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.dx = 4 * Math.sign(ball.dx);
ball.dy = 4 * Math.sign(ball.dy);
ball.hitCount = 0;
}
function draw() {
ctx.fillStyle = '#000'; // Black court
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawPaddle(dogePaddle, true);
drawPaddle(chuckPaddle);
drawBall();
drawScore();
}
function gameLoop() {
if (isGameStarted) {
update();
draw();
}
requestAnimationFrame(gameLoop);
}
function showStartScreen() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = '48px Arial';
ctx.fillStyle = '#fff';
ctx.textAlign = 'center';
ctx.fillText('Pong Game', canvas.width / 2, canvas.height / 3);
ctx.font = '32px Arial';
ctx.fillText('Press 1 for Single Player', canvas.width / 2, canvas.height / 2);
ctx.fillText('Press 2 for Multiplayer', canvas.width / 2, canvas.height / 2 + 50);
}
function startGame(singlePlayerMode) {
isSinglePlayer = singlePlayerMode;
isGameStarted = true;
}
document.addEventListener('keydown', function(e) {
if (!isGameStarted) {
if (e.key === '1') {
startGame(true);
} else if (e.key === '2') {
startGame(false);
}
} else {
if (e.key === 'w') dogePaddle.dy = -6;
if (e.key === 's') dogePaddle.dy = 6;
if (!isSinglePlayer) {
if (e.key === 'ArrowUp') chuckPaddle.dy = -6;
if (e.key === 'ArrowDown') chuckPaddle.dy = 6;
}
}
});
document.addEventListener('keyup', function(e) {
if (e.key === 'w' || e.key === 's') dogePaddle.dy = 0;
if (!isSinglePlayer) {
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') chuckPaddle.dy = 0;
}
});
// Show start screen initially
showStartScreen();
// Start the game loop
gameLoop();