-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
524 lines (443 loc) · 18.9 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<!DOCTYPE html>
<html lang="en">
<body style="background-color:black;">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BioDefender: Boss Edition</title>
<style>
canvas {
border: 1px solid #000;
display: block;
margin: 0 auto;
}
#deathMessage {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 24px;
color: white;
background-color: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 10px;
display: none;
}
h3 {
text-align: center;
}
</style>
</head>
<body>
</body>
<body>
<canvas id="gameCanvas" width="1200" height="600"></canvas>
<div id="deathMessage">You are dead. Try again? Press Enter to restart.</div>
<audio id="backgroundMusic" loop>
<source src="background.mp3" type="audio/mpeg">
</audio>
<audio id="explosionSound">
<source src="explosion.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const deathMessage = document.getElementById('deathMessage');
const backgroundMusic = document.getElementById('backgroundMusic');
backgroundMusic.play().catch(e => console.log("Audio play failed:", e));
const explosionSound = document.getElementById('explosionSound');
const backgroundImage = new Image();
backgroundImage.src = 'bg.png';
let player, bullets, enemies, explosions, particles, keys, score, gameOver, spawnInterval, bossSpawnTimer;
function initGame() {
player = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 15,
angle: 0,
thrust: 0,
rotationSpeed: 0.1,
velocity: { x: 0, y: 0 },
acceleration: 0.15,
friction: 0.99,
};
bullets = [];
enemies = [];
explosions = [];
particles = [];
keys = {};
score = 0;
gameOver = false;
spawnInterval = 1000;
bossSpawnTimer = 0;
deathMessage.style.display = 'none';
backgroundMusic.play().catch(e => console.log("Audio play failed:", e));
}
const MAX_ENEMIES = 50;
const ENEMY_SPEED = 1.8;
const ENEMY_ACCELERATION = 0.01;
const SAFE_SPAWN_DISTANCE = 150;
const MAX_SPAWN_ATTEMPTS = 50;
let screenShake = 0;
// Add these variables at the top of your script, after other declarations
const numLayers = 3;
const layers = [];
// Load background layers
for (let i = 0; i < numLayers; i++) {
const layer = new Image();
layer.src = `bg_layer_${i + 1}.png`; // Ensure you have these images
layers.push({
image: layer,
x: 0,
speed: (i + 1) * 0.5 // Adjust speed as needed
});
}
function drawPlayer() {
ctx.save();
ctx.translate(player.x, player.y);
ctx.rotate(player.angle);
ctx.beginPath();
ctx.moveTo(player.radius, 0);
ctx.lineTo(-player.radius, -player.radius / 2);
ctx.lineTo(-player.radius, player.radius / 2);
ctx.closePath();
// Fill the player shape
ctx.fillStyle = 'white';
ctx.fill();
// Add a black stroke outline
ctx.lineWidth = 2;
ctx.strokeStyle = 'black';
ctx.stroke();
ctx.restore();
}
function updatePlayer() {
if (keys.ArrowUp) player.thrust = player.acceleration;
else player.thrust = 0;
if (keys.ArrowLeft) player.angle -= player.rotationSpeed;
if (keys.ArrowRight) player.angle += player.rotationSpeed;
player.velocity.x += Math.cos(player.angle) * player.thrust;
player.velocity.y += Math.sin(player.angle) * player.thrust;
player.velocity.x *= player.friction;
player.velocity.y *= player.friction;
player.x += player.velocity.x;
player.y += player.velocity.y;
if (player.x > canvas.width) player.x = 0;
if (player.x < 0) player.x = canvas.width;
if (player.y > canvas.height) player.y = 0;
if (player.y < 0) player.y = canvas.height;
}
function createEnemy(x, y, radius, speed, canSplit = true, isBoss = false) {
if (enemies.length >= MAX_ENEMIES && !isBoss) return null;
let enemyX, enemyY;
let attempts = 0;
do {
enemyX = x || Math.random() * canvas.width;
enemyY = y || Math.random() * canvas.height;
attempts++;
if (attempts > MAX_SPAWN_ATTEMPTS) return null;
} while (distanceBetween(enemyX, enemyY, player.x, player.y) < SAFE_SPAWN_DISTANCE);
return {
x: enemyX,
y: enemyY,
radius: radius || (isBoss ? 50 : 15),
velocity: { x: 0, y: 0 },
speed: speed || (isBoss ? ENEMY_SPEED * 0.5 : ENEMY_SPEED),
canSplit: canSplit,
// colors
color: isBoss ? 'white' : (canSplit ? 'red' : 'pink'),
isBoss: isBoss,
health: isBoss ? 15 : 1
};
}
function distanceBetween(x1, y1, x2, y2) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
}
function updateAndDrawEnemies() {
enemies.forEach(enemy => {
let dx = player.x - enemy.x;
let dy = player.y - enemy.y;
let distance = Math.sqrt(dx * dx + dy * dy);
dx /= distance;
dy /= distance;
enemy.velocity.x += dx * ENEMY_ACCELERATION;
enemy.velocity.y += dy * ENEMY_ACCELERATION;
let speed = Math.sqrt(enemy.velocity.x * enemy.velocity.x + enemy.velocity.y * enemy.velocity.y);
if (speed > enemy.speed) {
enemy.velocity.x = (enemy.velocity.x / speed) * enemy.speed;
enemy.velocity.y = (enemy.velocity.y / speed) * enemy.speed;
}
enemy.x += enemy.velocity.x;
enemy.y += enemy.velocity.y;
if (enemy.x > canvas.width) enemy.x = 0;
if (enemy.x < 0) enemy.x = canvas.width;
if (enemy.y > canvas.height) enemy.y = 0;
if (enemy.y < 0) enemy.y = canvas.height;
// Draw the enemy
ctx.beginPath();
ctx.arc(enemy.x, enemy.y, enemy.radius, 0, Math.PI * 2);
ctx.fillStyle = enemy.color;
ctx.fill();
// Add a black stroke outline
ctx.lineWidth = 0;
ctx.strokeStyle = 'black';
ctx.stroke();
if (enemy.isBoss) {
ctx.fillStyle = 'white';
ctx.font = '12px Arial';
ctx.textAlign = 'center';
ctx.fillText(enemy.health, enemy.x, enemy.y + 5);
}
});
}
function createExplosion(x, y, isSmall = false, isBoss = false) {
const baseRadius = isSmall ? 30 : (isBoss ? 60 : 40);
const particleCount = isSmall ? 40 : (isBoss ? 120 : 60);
// colors
const color = isSmall ? 'Yellow' : (isBoss ? 'Purple' : 'orange');
explosions.push({
x: x,
y: y,
radius: 1,
maxRadius: baseRadius,
growthRate: 2,
alpha: 0.7,
color: color
});
explosions.push({
x: x,
y: y,
radius: 1,
maxRadius: baseRadius * 1.5,
growthRate: 3,
alpha: 0.3,
color: 'white'
});
for (let i = 0; i < particleCount; i++) {
let speed = 1 + Math.random() * 3;
let angle = Math.random() * Math.PI * 2;
particles.push({
x: x,
y: y,
radius: 0.5 + Math.random() * 2,
velocity: {
x: Math.cos(angle) * speed,
y: Math.sin(angle) * speed
},
color: color,
alpha: 0.8,
decay: 0.01 + Math.random() * 0.02
});
}
playExplosionSound();
screenShake = isBoss ? 15 : (isSmall ? 5 : 8);
}
function playExplosionSound() {
const newExplosionSound = new Audio('explosion.mp3');
newExplosionSound.play().catch(e => console.log("Audio play failed:", e));
}
function updateAndDrawExplosions() {
explosions.forEach((explosion, index) => {
ctx.beginPath();
ctx.arc(explosion.x, explosion.y, explosion.radius, 0, Math.PI * 2);
ctx.fillStyle = `${explosion.color}${Math.floor(explosion.alpha * 255).toString(16).padStart(2, '0')}`;
ctx.fill();
explosion.radius += explosion.growthRate;
explosion.alpha -= 0.02;
if (explosion.alpha <= 0 || explosion.radius >= explosion.maxRadius) {
explosions.splice(index, 1);
}
});
particles.forEach((particle, index) => {
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
ctx.fillStyle = `${particle.color}${Math.floor(particle.alpha * 255).toString(16).padStart(2, '0')}`;
ctx.fill();
particle.x += particle.velocity.x;
particle.y += particle.velocity.y;
particle.alpha -= particle.decay;
if (particle.alpha <= 0) {
particles.splice(index, 1);
}
});
}
function checkCollisions() {
bullets.forEach((bullet, bulletIndex) => {
enemies.forEach((enemy, enemyIndex) => {
const dx = bullet.x - enemy.x;
const dy = bullet.y - enemy.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < enemy.radius) {
bullets.splice(bulletIndex, 1);
if (enemy.isBoss) {
enemy.health--;
if (enemy.health <= 0) {
createExplosion(enemy.x, enemy.y, false, true);
enemies.splice(enemyIndex, 1);
score += 100;
for (let i = 0; i < 5; i++) {
const angle = (i / 10) * Math.PI * 2;
const newEnemy = createEnemy(
enemy.x + Math.cos(angle) * 50,
enemy.y + Math.sin(angle) * 50,
15,
ENEMY_SPEED * 1.00,
true
);
// color
if (newEnemy) {
newEnemy.color = 'red';
enemies.push(newEnemy);
}
}
}
} else {
createExplosion(enemy.x, enemy.y, !enemy.canSplit);
if (enemy.canSplit) {
const smallEnemy1 = createEnemy(enemy.x, enemy.y, enemy.radius / 2, enemy.speed * 1.5, false);
const smallEnemy2 = createEnemy(enemy.x, enemy.y, enemy.radius / 2, enemy.speed * 1.5, false);
if (smallEnemy1) enemies.push(smallEnemy1);
if (smallEnemy2) enemies.push(smallEnemy2);
score += 1;
} else {
score += 2;
}
enemies.splice(enemyIndex, 1);
}
updateSpawnInterval();
}
});
});
enemies.forEach(enemy => {
const dx = player.x - enemy.x;
const dy = player.y - enemy.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < player.radius/2 + enemy.radius) {
gameOver = true;
deathMessage.style.display = 'block';
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
}
});
}
// spawnrate
function updateSpawnInterval() {
spawnInterval = Math.max(100, 1000 * Math.exp(-score / 300));
clearInterval(enemySpawnTimer);
enemySpawnTimer = setInterval(() => {
if (!gameOver) {
const enemy = createEnemy();
if (enemy) enemies.push(enemy);
}
}, spawnInterval);
}
function shoot() {
bullets.push({
x: player.x + Math.cos(player.angle) * player.radius,
y: player.y + Math.sin(player.angle) * player.radius,
velocity: {
x: Math.cos(player.angle) * 5 + player.velocity.x,
y: Math.sin(player.angle) * 5 + player.velocity.y
}
});
}
function applyScreenShake() {
if (screenShake > 0) {
const shakeX = (Math.random() - 0.5) * screenShake;
const shakeY = (Math.random() - 0.5) * screenShake;
ctx.translate(shakeX, shakeY);
screenShake *= 0.9;
}
}
function drawScore() {
ctx.font = '28px Arial';
ctx.textAlign = 'center';
// Draw stroke first
ctx.lineWidth = 4;
ctx.strokeStyle = 'black';
ctx.strokeText(`Score: ${score}`, canvas.width / 2, 40);
// Draw fill on top
ctx.fillStyle = 'white';
ctx.fillText(`Score: ${score}`, canvas.width / 2, 40);
}
// Add this new function to draw the parallax background
function drawParallaxBackground() {
layers.forEach(layer => {
// Draw each layer twice to create a seamless scroll
ctx.drawImage(layer.image, layer.x, 0, canvas.width, canvas.height);
ctx.drawImage(layer.image, layer.x + canvas.width, 0, canvas.width, canvas.height);
// Move the layer
layer.x -= layer.speed;
// Reset the layer position if it's moved completely off-screen
if (layer.x <= -canvas.width) {
layer.x = 0;
}
});
}
function gameLoop() {
if (gameOver) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
applyScreenShake();
// Draw parallax background
drawParallaxBackground();
// ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
updatePlayer();
drawPlayer();
// color shots
ctx.fillStyle = 'Yellow';
bullets.forEach((bullet, index) => {
ctx.beginPath();
ctx.arc(bullet.x, bullet.y, 2, 0, Math.PI * 2);
ctx.fill();
bullet.x += bullet.velocity.x;
bullet.y += bullet.velocity.y;
if (bullet.x < 0 || bullet.x > canvas.width || bullet.y < 0 || bullet.y > canvas.height) {
bullets.splice(index, 1);
}
});
updateAndDrawEnemies();
updateAndDrawExplosions();
checkCollisions();
drawScore();
ctx.restore();
if (keys.Space) {
shoot();
}
bossSpawnTimer++;
if (bossSpawnTimer >= 1000) { // Spawn a boss every 1000 frames (adjust as needed)
const boss = createEnemy(null, null, null, null, false, true);
if (boss) enemies.push(boss);
bossSpawnTimer = 0;
}
requestAnimationFrame(gameLoop);
}
document.addEventListener('keydown', (e) => {
keys[e.code] = true;
if (e.code === 'Enter' && gameOver) {
initGame();
updateSpawnInterval();
gameLoop();
}
});
document.addEventListener('keyup', (e) => {
keys[e.code] = false;
});
let enemySpawnTimer;
backgroundImage.onload = function() {
initGame();
for (let i = 0; i < 5; i++) {
const enemy = createEnemy();
if (enemy) enemies.push(enemy);
}
updateSpawnInterval();
gameLoop();
};
// Add a click event listener to start audio (to handle autoplay restrictions)
document.addEventListener('click', () => {
if (backgroundMusic.paused) {
backgroundMusic.play().catch(e => console.log("Audio play failed:", e));
}
});
</script>
<p style="background-color:black;color:white;">Move with arrow keys, shoot with space</p>
<p style="background-color:black;color:white;">Written with <a href="https://www.anthropic.com/news/claude-3-5-sonnet">Claude 3.5 Sonnet</a> and <a href="https://suno.com/song/39da37ec-ca93-4c7a-85ed-7a49ef28089d">Suno.ai</a></p>