-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathindex.html
219 lines (184 loc) · 5.56 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Pong wars</title>
</head>
<body>
<div id="container">
<canvas id="pongCanvas" width="500" height="500"></canvas>
<div id="score"></div>
</div>
<style>
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(to bottom, #172b36 0%, #d9e8e3 100%);
}
#container {
display: block;
margin: auto;
}
#pongCanvas {
display: block;
margin: auto;
border-radius: 4px;
overflow: hidden;
}
#score {
text-align: center;
font-family: monospace;
margin-top: 20px;
padding-left: 15px;
color: #172b36;
}
</style>
<script>
// Source palette: https://twitter.com/AlexCristache/status/1738610343499157872
const colorPalette = {
ArcticPowder: "#F1F6F4",
MysticMint: "#D9E8E3",
Forsythia: "#FFC801",
DeepSaffron: "#FF9932",
NocturnalExpedition: "#114C5A",
OceanicNoir: "#172B36",
};
// Idea for Pong wars: https://twitter.com/nicolasdnl/status/1749715070928433161
const canvas = document.getElementById("pongCanvas");
const ctx = canvas.getContext("2d");
const scoreElement = document.getElementById("score");
const TEAM1 = colorPalette.MysticMint;
const TEAM1_BALL = colorPalette.NocturnalExpedition;
const TEAM2 = colorPalette.NocturnalExpedition;
const TEAM2_BALL = colorPalette.MysticMint;
const squareSize = 25;
const numSquaresX = canvas.width / squareSize;
const numSquaresY = canvas.height / squareSize;
let squares = [];
for (let i = 0; i < numSquaresX; i++) {
squares[i] = [];
for (let j = 0; j < numSquaresY; j++) {
squares[i][j] = i < numSquaresX / 2 ? TEAM1 : TEAM2;
}
}
let x1 = canvas.width / 4;
let y1 = canvas.height / 2;
let dx1 = 10;
let dy1 = 10;
let x2 = (canvas.width / 4) * 3;
let y2 = canvas.height / 2;
let dx2 = -10;
let dy2 = -10;
function drawBall(x, y, color) {
ctx.beginPath();
ctx.arc(x, y, squareSize / 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 * squareSize,
j * squareSize,
squareSize,
squareSize
);
}
}
}
function randomNum(min, max) {
return Math.random() * (max - min) + min;
}
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) * (squareSize / 2);
let checkY = y + Math.sin(angle) * (squareSize / 2);
let i = Math.floor(checkX / squareSize);
let j = Math.floor(checkY / squareSize);
if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) {
if (squares[i][j] !== color) {
squares[i][j] = color;
let rnd = randomNum(-0.25, 0.25);
// Determine bounce direction based on the angle
if (Math.abs(Math.cos(angle)) > Math.abs(Math.sin(angle))) {
updatedDx = -updatedDx + rnd;
} else {
updatedDy = -updatedDy + rnd;
}
}
}
}
return { dx: updatedDx, dy: updatedDy };
}
function updateScoreElement() {
let team1Score = 0;
let team2Score = 0;
for (let i = 0; i < numSquaresX; i++) {
for (let j = 0; j < numSquaresY; j++) {
if (squares[i][j] === TEAM1) {
team1Score++;
} else if (squares[i][j] === TEAM2) {
team2Score++;
}
}
}
scoreElement.textContent = `day ${team1Score} | night ${team2Score}`;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSquares();
drawBall(x1, y1, TEAM1_BALL);
let bounce1 = updateSquareAndBounce(x1, y1, dx1, dy1, TEAM1);
dx1 = bounce1.dx;
dy1 = bounce1.dy;
drawBall(x2, y2, TEAM2_BALL);
let bounce2 = updateSquareAndBounce(x2, y2, dx2, dy2, TEAM2);
dx2 = bounce2.dx;
dy2 = bounce2.dy;
updateScoreElement();
if (
x1 + dx1 > canvas.width - squareSize / 2 ||
x1 + dx1 < squareSize / 2
) {
dx1 = -dx1;
}
if (
y1 + dy1 > canvas.height - squareSize / 2 ||
y1 + dy1 < squareSize / 2
) {
dy1 = -dy1;
}
if (
x2 + dx2 > canvas.width - squareSize / 2 ||
x2 + dx2 < squareSize / 2
) {
dx2 = -dx2;
}
if (
y2 + dy2 > canvas.height - squareSize / 2 ||
y2 + dy2 < squareSize / 2
) {
dy2 = -dy2;
}
x1 += dx1;
y1 += dy1;
x2 += dx2;
y2 += dy2;
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
</script>
</body>
</html>