-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGalacticVanguard.html
More file actions
160 lines (140 loc) · 4.74 KB
/
GalacticVanguard.html
File metadata and controls
160 lines (140 loc) · 4.74 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
<!DOCTYPE html>
<html>
<head>
<title>Galactic Vanguard</title>
<style>
body { margin: 0; background: #020205; overflow: hidden; font-family: 'Segoe UI', sans-serif; }
canvas { display: block; }
#overlay {
position: absolute; top: 20px; left: 20px; color: #00f2ff;
text-shadow: 0 0 10px #00f2ff; font-size: 20px; pointer-events: none;
}
</style>
</head>
<body>
<div id="overlay">SCORE: <span id="score">0</span></div>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let score = 0;
const particles = [];
const bullets = [];
const enemies = [];
const stars = Array.from({length: 100}, () => ({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
s: Math.random() * 2
}));
const player = {
x: canvas.width / 2,
y: canvas.height - 80,
w: 40, h: 40,
vx: 0,
friction: 0.9,
accel: 0.8
};
const keys = {};
window.addEventListener('keydown', e => keys[e.code] = true);
window.addEventListener('keyup', e => keys[e.code] = false);
class Particle {
constructor(x, y, color) {
this.x = x; this.y = y;
this.v = { x: (Math.random()-0.5)*8, y: (Math.random()-0.5)*8 };
this.alpha = 1;
this.color = color;
}
draw() {
ctx.globalAlpha = this.alpha;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, 2, 0, Math.PI*2);
ctx.fill();
}
update() {
this.x += this.v.x; this.y += this.v.y;
this.alpha -= 0.02;
}
}
function createExplosion(x, y, color) {
for(let i=0; i<15; i++) particles.push(new Particle(x, y, color));
}
function update() {
// Starfield
stars.forEach(s => {
s.y += s.s;
if(s.y > canvas.height) s.y = 0;
});
// Player Physics (Smooth Movement)
if (keys['ArrowLeft']) player.vx -= player.accel;
if (keys['ArrowRight']) player.vx += player.accel;
player.vx *= player.friction;
player.x += player.vx;
player.x = Math.max(0, Math.min(canvas.width - player.w, player.x));
if (keys['Space'] && (bullets.length === 0 || bullets[bullets.length-1].y < player.y - 100)) {
bullets.push({ x: player.x + player.w/2 - 2, y: player.y, w: 4, h: 20 });
}
bullets.forEach((b, i) => {
b.y -= 12;
if (b.y < 0) bullets.splice(i, 1);
});
enemies.forEach((e, ei) => {
e.y += e.speed;
// Collision Logic
bullets.forEach((b, bi) => {
if (b.x < e.x + e.w && b.x + b.w > e.x && b.y < e.y + e.h) {
createExplosion(e.x + e.w/2, e.y + e.h/2, '#ff4d4d');
enemies.splice(ei, 1);
bullets.splice(bi, 1);
score += 10;
document.getElementById('score').innerText = score;
}
});
if (e.y > canvas.height) enemies.splice(ei, 1);
});
particles.forEach((p, i) => {
p.update();
if(p.alpha <= 0) particles.splice(i, 1);
});
if (Math.random() < 0.04) enemies.push({
x: Math.random() * (canvas.width - 40), y: -50,
w: 40, h: 40, speed: 3 + Math.random() * 2
});
}
function draw() {
ctx.fillStyle = '#020205';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw Stars
ctx.fillStyle = '#fff';
stars.forEach(s => ctx.fillRect(s.x, s.y, s.s, s.s));
// Draw Player (Neon Glow)
ctx.shadowBlur = 15;
ctx.shadowColor = '#00f2ff';
ctx.fillStyle = '#00f2ff';
ctx.beginPath();
ctx.moveTo(player.x + player.w/2, player.y);
ctx.lineTo(player.x, player.y + player.h);
ctx.lineTo(player.x + player.w, player.y + player.h);
ctx.fill();
ctx.shadowBlur = 0;
// Draw Bullets
ctx.fillStyle = '#00f2ff';
bullets.forEach(b => ctx.fillRect(b.x, b.y, b.w, b.h));
// Draw Enemies
enemies.forEach(e => {
ctx.fillStyle = '#ff4d4d';
ctx.shadowBlur = 10;
ctx.shadowColor = '#ff4d4d';
ctx.fillRect(e.x, e.y, e.w, e.h);
});
ctx.shadowBlur = 0;
particles.forEach(p => p.draw());
ctx.globalAlpha = 1;
requestAnimationFrame(() => { update(); draw(); });
}
draw();
</script>
</body>
</html>