-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
245 lines (205 loc) · 6.31 KB
/
Copy pathindex.html
File metadata and controls
245 lines (205 loc) · 6.31 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
<!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" />
<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%;
}
#pongCanvas {
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="800" height="800"></canvas>
<div id="score"></div>
<p id="made">
made by <a href="https://koenvangilst.nl">Koen van Gilst</a> | source on
<a href="https://github.com/vnglst/pong-wars">github</a>
</p>
</div>
</body>
<script>
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 numSquaresX = canvas.width / SQUARE_SIZE;
const numSquaresY = canvas.height / SQUARE_SIZE;
let squares = [];
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;
}
}
let x1 = canvas.width / 4;
let y1 = canvas.height / 2;
let dx1 = 12.5;
let dy1 = -12.5;
let x2 = (canvas.width / 4) * 3;
let y2 = canvas.height / 2;
let dx2 = -12.5;
let dy2 = 12.5;
let iteration = 0;
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 updateScoreElement() {
let dayScore = 0;
let nightScore = 0;
for (let i = 0; i < numSquaresX; i++) {
for (let j = 0; j < numSquaresY; j++) {
if (squares[i][j] === DAY_COLOR) {
dayScore++;
} else if (squares[i][j] === NIGHT_COLOR) {
nightScore++;
}
}
}
scoreElement.textContent = `day ${dayScore} | night ${nightScore}`;
}
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 draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSquares();
drawBall(x1, y1, DAY_BALL_COLOR);
let bounce1 = updateSquareAndBounce(x1, y1, dx1, dy1, DAY_COLOR);
dx1 = bounce1.dx;
dy1 = bounce1.dy;
drawBall(x2, y2, NIGHT_BALL_COLOR);
let bounce2 = updateSquareAndBounce(x2, y2, dx2, dy2, NIGHT_COLOR);
dx2 = bounce2.dx;
dy2 = bounce2.dy;
let boundary1 = checkBoundaryCollision(x1, y1, dx1, dy1);
dx1 = boundary1.dx;
dy1 = boundary1.dy;
let boundary2 = checkBoundaryCollision(x2, y2, dx2, dy2);
dx2 = boundary2.dx;
dy2 = boundary2.dy;
x1 += dx1;
y1 += dy1;
x2 += dx2;
y2 += dy2;
iteration++;
if (iteration % 1_000 === 0) console.log("iteration", iteration);
updateScoreElement();
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
</script>
</html>