-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
313 lines (243 loc) · 8.11 KB
/
script.js
File metadata and controls
313 lines (243 loc) · 8.11 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Source palette: https://twitter.com/AlexCristache/status/1738610343499157872
const colorPalette = {
Fire: "#AE2F24",
FireBall: "#BB7169",
EarthBall: "#737E44",
Earth: "#1E3414",
Air: "#EE7223",
AirBall: "#F39C65",
WaterBall: "#488ECB",
Water: "#244766",
};
// Idea for Pong wars: https://twitter.com/nicolasdnl/status/1749715070928433161
const canvas = document.getElementById("pongCanvas");
const ctx = canvas.getContext("2d");
const winner = document.getElementById("winner");
const keysNames = ["Earth", "Water", "Fire", "Air"]
const colors = Object.fromEntries(keysNames.map(k => [k, colorPalette[k]]))
const colorBalls = Object.fromEntries(keysNames.map(k => [k, colorPalette[`${k}Ball`]]))
const SQUARE_SIZE = 20;
const numSquaresX = Math.round(canvas.width / SQUARE_SIZE);
const numSquaresY = Math.round(canvas.height / SQUARE_SIZE);
const valueMin = 0
const valueMax = (numSquaresX * numSquaresY) / 4;
const ScoreMax = Object.fromEntries(keysNames.map(k => [k, valueMin]))
const ScoreMin = Object.fromEntries(keysNames.map(k => [k, valueMax]))
let squares = [];
let speedInput = document.querySelector('input[name="speed"]:checked').value;
const speed = document.getElementById('speed');
// Ball position, velocity and direction
let x1 = canvas.width / 4;
let y1 = canvas.height / 4;
let dx1 = randomNumber(5, 15);
let dy1 = randomNumber(5, 15);
let x2 = (canvas.width / 4) * 3;
let y2 = canvas.height / 4;
let dx2 = randomNumber(5, 15);
let dy2 = randomNumber(5, 15);
let x3 = canvas.width / 4;
let y3 = (canvas.height / 4) * 3;
let dx3 = randomNumber(5, 15);
let dy3 = randomNumber(5, 15);
let x4 = (canvas.width / 4) * 3;
let y4 = (canvas.height / 4) * 3;
let dx4 = randomNumber(5, 15);
let dy4 = randomNumber(5, 15);
// Function used for speed and direction
function randomNumber(min, max) {
var num = Math.floor(Math.random() * (max - min + 1)) + min;
var isNegative = Math.random() < 0.5;
return isNegative ? -num : num;
}
// Function to get the percentage Values
function percentage(x) {
return Math.round((x * 100) / (numSquaresX * numSquaresY));
}
// Draw of the teams fields
function teamsFields() {
for (let i = 0; i < numSquaresX; i++) {
squares[i] = [];
for (let j = 0; j < numSquaresY; j++) {
if (i < numSquaresX / 2) {
squares[i][j] = j < numSquaresY / 2 ? colors.Earth : colors.Air;
} else {
squares[i][j] = j < numSquaresY / 2 ? colors.Water : colors.Fire;
}
}
}
}
function drawBall(x, y, color) {
ctx.beginPath();
ctx.arc(x, y, SQUARE_SIZE / 2, 0, Math.PI * 2, false);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
function drawSquares() {
for (let i = 0; i < numSquaresX; i++) {
for (let j = 0; j < numSquaresY; j++) {
ctx.fillStyle = squares[i][j];
ctx.fillRect(
i * SQUARE_SIZE,
j * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE
);
}
}
}
function updateSquareAndBounce(x, y, dx, dy, color) {
let updatedDx = dx;
let updatedDy = dy;
// Check multiple points around the ball's circumference
for (let angle = 0; angle < Math.PI * 2; angle += Math.PI / 4) {
let checkX = x + Math.cos(angle) * (SQUARE_SIZE / 2);
let checkY = y + Math.sin(angle) * (SQUARE_SIZE / 2);
let i = Math.floor(checkX / SQUARE_SIZE);
let j = Math.floor(checkY / SQUARE_SIZE);
if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) {
if (squares[i][j] !== color) {
squares[i][j] = color;
// Determine bounce direction based on the angle
if (Math.abs(Math.cos(angle)) > Math.abs(Math.sin(angle))) {
updatedDx = -updatedDx;
} else {
updatedDy = -updatedDy;
}
}
}
}
return {dx: updatedDx, dy: updatedDy};
}
function checkBoundaryCollision(x, y, dx, dy) {
if (x + dx > canvas.width - SQUARE_SIZE / 2 || x + dx < SQUARE_SIZE / 2) {
dx = -dx;
}
if (
y + dy > canvas.height - SQUARE_SIZE / 2 ||
y + dy < SQUARE_SIZE / 2
) {
dy = -dy;
}
return {dx: dx, dy: dy};
}
function maxScore(Score) {
for (key of Object.keys(ScoreMax)) {
if (Score[key] > ScoreMax[key]) {
ScoreMax[key] = (Score[key]);
}
}
}
function minScore(Score) {
for (key of Object.keys(ScoreMin)) {
if (Score[key] < ScoreMin[key]) {
ScoreMin[key] = (Score[key]);
}
}
}
function winnerScore() {
const maxValue = Math.max(...Object.values(ScoreMax));
const maxElement = Object.keys(ScoreMax).find(key => ScoreMax[key] === maxValue);
winner.innerHTML = `<b>${maxElement} with ${percentage(maxValue)}%</b>`;
}
function updateScoreElement() {
const Score = Object.fromEntries(keysNames.map(k => [k, valueMin]))
for (let i = 0; i < numSquaresX; i++) {
for (let j = 0; j < numSquaresY; j++) {
// Update Values
keysNames.map(item => {
if (squares[i][j] === colors[item]) {
Score[item]++;
}
}
);
// Update Scores
for (key of Object.keys(ScoreMax)) {
if (Score[key] > ScoreMax[key]) {
ScoreMax[key] = (Score[key]);
}
}
}
}
for (key of Object.keys(Score)) {
document.getElementById(String(key)).innerHTML = `${key}: ${percentage(Score[key])}% <small><b>(${percentage(ScoreMax[key])}% Max | ${percentage(ScoreMin[key])}% Min) </b></small>`;
}
maxScore(Score);
minScore(Score);
winnerScore();
}
function animationSpeed() {
speed.addEventListener('change', (event) => {
speedInput = event.target.value;
});
window.requestAnimationFrame(() => {
setTimeout(this.draw.bind(this), (1000 / Number(speedInput)));
});
}
// Timer
var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var hoursLabel = document.getElementById("hours");
var totalSeconds = 0;
function setTime() {
++totalSeconds;
d = Number(totalSeconds);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
secondsLabel.innerHTML = sDisplay;
minutesLabel.innerHTML = mDisplay;
hoursLabel.innerHTML = hDisplay;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSquares();
drawBall(x1, y1, colorBalls.Earth);
let bounce1 = updateSquareAndBounce(x1, y1, dx1, dy1, colors.Earth);
dx1 = bounce1.dx;
dy1 = bounce1.dy;
drawBall(x2, y2, colorBalls.Water);
let bounce2 = updateSquareAndBounce(x2, y2, dx2, dy2, colors.Water);
dx2 = bounce2.dx;
dy2 = bounce2.dy;
drawBall(x3, y3, colorBalls.Air);
let bounce3 = updateSquareAndBounce(x3, y3, dx3, dy3, colors.Air);
dx3 = bounce3.dx;
dy3 = bounce3.dy;
drawBall(x4, y4, colorBalls.Fire);
let bounce4 = updateSquareAndBounce(x4, y4, dx4, dy4, colors.Fire);
dx4 = bounce4.dx;
dy4 = bounce4.dy;
let boundary1 = checkBoundaryCollision(x1, y1, dx1, dy1);
dx1 = boundary1.dx;
dy1 = boundary1.dy;
x1 += dx1;
y1 += dy1;
let boundary2 = checkBoundaryCollision(x2, y2, dx2, dy2);
dx2 = boundary2.dx;
dy2 = boundary2.dy;
x2 += dx2;
y2 += dy2;
//
let boundary3 = checkBoundaryCollision(x3, y3, dx3, dy3);
dx3 = boundary3.dx;
dy3 = boundary3.dy;
x3 += dx3;
y3 += dy3;
//
//
let boundary4 = checkBoundaryCollision(x4, y4, dx4, dy4);
dx4 = boundary4.dx;
dy4 = boundary4.dy;
x4 += dx4;
y4 += dy4;
updateScoreElement();
animationSpeed();
}
teamsFields();
requestAnimationFrame(draw);
setTime();
setInterval(setTime, 1000);