-
Notifications
You must be signed in to change notification settings - Fork 143
/
index.html
239 lines (204 loc) · 6.78 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pong wars | Koen van Gilst</title>
<meta
name="description"
content="The eternal battle between day and night, good and bad. Written in JavaScript with some HTML & CSS in one index.html. Feel free to reuse the code and create your own version."
/>
<link rel="canonical" href="https://pong-wars.koenvangilst.nl/" />
<link rel="author" href="https://koenvangilst.nl" />
<meta name="theme-color" content="#172B36" />
<meta name="creator" content="Koen van Gilst" />
<script defer data-domain="pong-wars.koenvangilst.nl" src="https://plausible.koenvangilst.nl/js/script.js"></script>
<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: flex;
align-items: center;
flex-direction: column;
width: min(70vh, 80%);
max-width: 600px;
height: 100%;
}
canvas {
display: block;
border-radius: 4px;
overflow: hidden;
width: 100%;
margin-top: auto;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
}
#score {
font-family: monospace;
margin-top: 30px;
font-size: 16px;
padding-left: 20px;
color: #172b36;
}
#made {
text-align: center;
line-height: 1.5;
font-family: monospace;
margin-top: auto;
margin-bottom: 20px;
font-size: 10px;
}
#made a {
color: #172b36;
}
</style>
</head>
<body>
<div id="container">
<canvas id="pongCanvas" width="600" height="600"></canvas>
<div id="score"></div>
<p id="made">
made by
<a href="https://koenvangilst.nl/labs/pong-wars">Koen van Gilst</a> | source on
<a href="https://github.com/vnglst/pong-wars">github</a>
</p>
</div>
</body>
<script>
// Source palette: https://twitter.com/AlexCristache/status/1738610343499157872
// Idea for Pong wars: https://twitter.com/nicolasdnl/status/1749715070928433161
const colorPalette = {
ArcticPowder: "#F1F6F4",
MysticMint: "#D9E8E3",
Forsythia: "#FFC801",
DeepSaffron: "#FF9932",
NocturnalExpedition: "#114C5A",
OceanicNoir: "#172B36",
};
const canvas = document.getElementById("pongCanvas");
const ctx = canvas.getContext("2d");
const scoreElement = document.getElementById("score");
const DAY_COLOR = colorPalette.MysticMint;
const DAY_BALL_COLOR = colorPalette.NocturnalExpedition;
const NIGHT_COLOR = colorPalette.NocturnalExpedition;
const NIGHT_BALL_COLOR = colorPalette.MysticMint;
const SQUARE_SIZE = 25;
const MIN_SPEED = 5;
const MAX_SPEED = 10;
const numSquaresX = canvas.width / SQUARE_SIZE;
const numSquaresY = canvas.height / SQUARE_SIZE;
let dayScore = 0;
let nightScore = 0;
const squares = [];
// Populate the fields, one half day, one half night
for (let i = 0; i < numSquaresX; i++) {
squares[i] = [];
for (let j = 0; j < numSquaresY; j++) {
squares[i][j] = i < numSquaresX / 2 ? DAY_COLOR : NIGHT_COLOR;
}
}
const balls = [
{
x: canvas.width / 4,
y: canvas.height / 2,
dx: 8,
dy: -8,
reverseColor: DAY_COLOR,
ballColor: DAY_BALL_COLOR,
},
{
x: (canvas.width / 4) * 3,
y: canvas.height / 2,
dx: -8,
dy: 8,
reverseColor: NIGHT_COLOR,
ballColor: NIGHT_BALL_COLOR,
},
];
let iteration = 0;
function drawBall(ball) {
ctx.beginPath();
ctx.arc(ball.x, ball.y, SQUARE_SIZE / 2, 0, Math.PI * 2, false);
ctx.fillStyle = ball.ballColor;
ctx.fill();
ctx.closePath();
}
function drawSquares() {
dayScore = 0;
nightScore = 0;
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);
// Update scores
if (squares[i][j] === DAY_COLOR) dayScore++;
if (squares[i][j] === NIGHT_COLOR) nightScore++;
}
}
}
function checkSquareCollision(ball) {
// Check multiple points around the ball's circumference
for (let angle = 0; angle < Math.PI * 2; angle += Math.PI / 4) {
const checkX = ball.x + Math.cos(angle) * (SQUARE_SIZE / 2);
const checkY = ball.y + Math.sin(angle) * (SQUARE_SIZE / 2);
const i = Math.floor(checkX / SQUARE_SIZE);
const j = Math.floor(checkY / SQUARE_SIZE);
if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) {
if (squares[i][j] !== ball.reverseColor) {
// Square hit! Update square color
squares[i][j] = ball.reverseColor;
// Determine bounce direction based on the angle
if (Math.abs(Math.cos(angle)) > Math.abs(Math.sin(angle))) {
ball.dx = -ball.dx;
} else {
ball.dy = -ball.dy;
}
}
}
}
}
function checkBoundaryCollision(ball) {
if (ball.x + ball.dx > canvas.width - SQUARE_SIZE / 2 || ball.x + ball.dx < SQUARE_SIZE / 2) {
ball.dx = -ball.dx;
}
if (ball.y + ball.dy > canvas.height - SQUARE_SIZE / 2 || ball.y + ball.dy < SQUARE_SIZE / 2) {
ball.dy = -ball.dy;
}
}
function addRandomness(ball) {
ball.dx += Math.random() * 0.02 - 0.01;
ball.dy += Math.random() * 0.02 - 0.01;
// Limit the speed of the ball
ball.dx = Math.min(Math.max(ball.dx, -MAX_SPEED), MAX_SPEED);
ball.dy = Math.min(Math.max(ball.dy, -MAX_SPEED), MAX_SPEED);
// Make sure the ball always maintains a minimum speed
if (Math.abs(ball.dx) < MIN_SPEED) ball.dx = ball.dx > 0 ? MIN_SPEED : -MIN_SPEED;
if (Math.abs(ball.dy) < MIN_SPEED) ball.dy = ball.dy > 0 ? MIN_SPEED : -MIN_SPEED;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSquares();
scoreElement.textContent = `day ${dayScore} | night ${nightScore}`;
balls.forEach((ball) => {
drawBall(ball);
checkSquareCollision(ball);
checkBoundaryCollision(ball);
ball.x += ball.dx;
ball.y += ball.dy;
addRandomness(ball);
});
iteration++;
if (iteration % 1_000 === 0) console.log("iteration", iteration);
}
const FRAME_RATE = 100;
setInterval(draw, 1000 / FRAME_RATE);
</script>
</html>