Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- assets/css/styles.css +2 -0
- assets/js/game.js +2 -0
- index.html +547 -19
assets/css/styles.css
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* This file is intentionally left empty.
|
| 2 |
+
All styles are embedded in the HTML file for this single-page game. */
|
assets/js/game.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// This file is intentionally left empty.
|
| 2 |
+
// All JavaScript is embedded in the HTML file for this single-page game.
|
index.html
CHANGED
|
@@ -1,19 +1,547 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Video Backdrop Game</title>
|
| 7 |
+
<style>
|
| 8 |
+
* {
|
| 9 |
+
margin: 0;
|
| 10 |
+
padding: 0;
|
| 11 |
+
box-sizing: border-box;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
body {
|
| 15 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 16 |
+
overflow: hidden;
|
| 17 |
+
background: #000;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
#gameContainer {
|
| 21 |
+
position: relative;
|
| 22 |
+
width: 100vw;
|
| 23 |
+
height: 100vh;
|
| 24 |
+
overflow: hidden;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
#videoBackground {
|
| 28 |
+
position: absolute;
|
| 29 |
+
top: 0;
|
| 30 |
+
left: 0;
|
| 31 |
+
width: 100%;
|
| 32 |
+
height: 100%;
|
| 33 |
+
object-fit: cover;
|
| 34 |
+
z-index: -1;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
#gameCanvas {
|
| 38 |
+
position: absolute;
|
| 39 |
+
top: 0;
|
| 40 |
+
left: 0;
|
| 41 |
+
width: 100%;
|
| 42 |
+
height: 100%;
|
| 43 |
+
z-index: 1;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
#ui {
|
| 47 |
+
position: absolute;
|
| 48 |
+
top: 20px;
|
| 49 |
+
left: 20px;
|
| 50 |
+
z-index: 2;
|
| 51 |
+
color: white;
|
| 52 |
+
font-size: 24px;
|
| 53 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
|
| 54 |
+
font-weight: bold;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
#startScreen, #gameOverScreen {
|
| 58 |
+
position: absolute;
|
| 59 |
+
top: 0;
|
| 60 |
+
left: 0;
|
| 61 |
+
width: 100%;
|
| 62 |
+
height: 100%;
|
| 63 |
+
background: rgba(0, 0, 0, 0.7);
|
| 64 |
+
display: flex;
|
| 65 |
+
flex-direction: column;
|
| 66 |
+
justify-content: center;
|
| 67 |
+
align-items: center;
|
| 68 |
+
z-index: 3;
|
| 69 |
+
color: white;
|
| 70 |
+
text-align: center;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
#startScreen h1, #gameOverScreen h1 {
|
| 74 |
+
font-size: 48px;
|
| 75 |
+
margin-bottom: 20px;
|
| 76 |
+
text-shadow: 3px 3px 6px rgba(0,0,0,0.9);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
#startScreen p, #gameOverScreen p {
|
| 80 |
+
font-size: 20px;
|
| 81 |
+
margin-bottom: 30px;
|
| 82 |
+
max-width: 600px;
|
| 83 |
+
line-height: 1.5;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
button {
|
| 87 |
+
padding: 15px 40px;
|
| 88 |
+
font-size: 24px;
|
| 89 |
+
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
|
| 90 |
+
color: white;
|
| 91 |
+
border: none;
|
| 92 |
+
border-radius: 50px;
|
| 93 |
+
cursor: pointer;
|
| 94 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 95 |
+
font-weight: bold;
|
| 96 |
+
text-transform: uppercase;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
button:hover {
|
| 100 |
+
transform: scale(1.05);
|
| 101 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
button:active {
|
| 105 |
+
transform: scale(0.95);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
#finalScore {
|
| 109 |
+
color: #4ecdc4;
|
| 110 |
+
font-size: 36px;
|
| 111 |
+
margin: 20px 0;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
.hidden {
|
| 115 |
+
display: none !important;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
#instructions {
|
| 119 |
+
margin-top: 20px;
|
| 120 |
+
font-size: 16px;
|
| 121 |
+
opacity: 0.8;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
#builtWith {
|
| 125 |
+
position: absolute;
|
| 126 |
+
bottom: 20px;
|
| 127 |
+
right: 20px;
|
| 128 |
+
z-index: 2;
|
| 129 |
+
color: white;
|
| 130 |
+
font-size: 14px;
|
| 131 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
#builtWith a {
|
| 135 |
+
color: #4ecdc4;
|
| 136 |
+
text-decoration: none;
|
| 137 |
+
font-weight: bold;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
#builtWith a:hover {
|
| 141 |
+
text-decoration: underline;
|
| 142 |
+
}
|
| 143 |
+
</style>
|
| 144 |
+
</head>
|
| 145 |
+
<body>
|
| 146 |
+
<div id="gameContainer">
|
| 147 |
+
<video id="videoBackground" autoplay muted loop>
|
| 148 |
+
<source src="https://assets.mixkit.co/videos/preview/mixkit-stars-in-space-2910-large.mp4" type="video/mp4">
|
| 149 |
+
Your browser does not support the video tag.
|
| 150 |
+
</video>
|
| 151 |
+
|
| 152 |
+
<canvas id="gameCanvas"></canvas>
|
| 153 |
+
|
| 154 |
+
<div id="ui">
|
| 155 |
+
<div>Score: <span id="score">0</span></div>
|
| 156 |
+
<div>Lives: <span id="lives">3</span></div>
|
| 157 |
+
</div>
|
| 158 |
+
|
| 159 |
+
<div id="builtWith">
|
| 160 |
+
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a>
|
| 161 |
+
</div>
|
| 162 |
+
|
| 163 |
+
<div id="startScreen">
|
| 164 |
+
<h1>Space Defender</h1>
|
| 165 |
+
<p>Defend your space station from incoming asteroids! Use your mouse or finger to move the spaceship.</p>
|
| 166 |
+
<button id="startButton">Start Game</button>
|
| 167 |
+
<div id="instructions">
|
| 168 |
+
<p>Move: Mouse/Touch | Auto-fire enabled</p>
|
| 169 |
+
</div>
|
| 170 |
+
</div>
|
| 171 |
+
|
| 172 |
+
<div id="gameOverScreen" class="hidden">
|
| 173 |
+
<h1>Game Over</h1>
|
| 174 |
+
<p id="finalScore">Final Score: 0</p>
|
| 175 |
+
<p id="gameOverMessage"></p>
|
| 176 |
+
<button id="restartButton">Play Again</button>
|
| 177 |
+
</div>
|
| 178 |
+
</div>
|
| 179 |
+
|
| 180 |
+
<script>
|
| 181 |
+
const canvas = document.getElementById('gameCanvas');
|
| 182 |
+
const ctx = canvas.getContext('2d');
|
| 183 |
+
const video = document.getElementById('videoBackground');
|
| 184 |
+
const startScreen = document.getElementById('startScreen');
|
| 185 |
+
const gameOverScreen = document.getElementById('gameOverScreen');
|
| 186 |
+
const startButton = document.getElementById('startButton');
|
| 187 |
+
const restartButton = document.getElementById('restartButton');
|
| 188 |
+
const scoreElement = document.getElementById('score');
|
| 189 |
+
const livesElement = document.getElementById('lives');
|
| 190 |
+
const finalScoreElement = document.getElementById('finalScore');
|
| 191 |
+
const gameOverMessageElement = document.getElementById('gameOverMessage');
|
| 192 |
+
|
| 193 |
+
// Set canvas size
|
| 194 |
+
function resizeCanvas() {
|
| 195 |
+
canvas.width = window.innerWidth;
|
| 196 |
+
canvas.height = window.innerHeight;
|
| 197 |
+
}
|
| 198 |
+
resizeCanvas();
|
| 199 |
+
window.addEventListener('resize', resizeCanvas);
|
| 200 |
+
|
| 201 |
+
// Game variables
|
| 202 |
+
let gameRunning = false;
|
| 203 |
+
let score = 0;
|
| 204 |
+
let lives = 3;
|
| 205 |
+
let mouseX = canvas.width / 2;
|
| 206 |
+
let mouseY = canvas.height - 100;
|
| 207 |
+
|
| 208 |
+
// Player spaceship
|
| 209 |
+
const player = {
|
| 210 |
+
x: canvas.width / 2,
|
| 211 |
+
y: canvas.height - 100,
|
| 212 |
+
width: 60,
|
| 213 |
+
height: 60,
|
| 214 |
+
speed: 8
|
| 215 |
+
};
|
| 216 |
+
|
| 217 |
+
// Game objects arrays
|
| 218 |
+
let asteroids = [];
|
| 219 |
+
let bullets = [];
|
| 220 |
+
let particles = [];
|
| 221 |
+
|
| 222 |
+
// Input handling
|
| 223 |
+
let isMouseDown = false;
|
| 224 |
+
|
| 225 |
+
canvas.addEventListener('mousemove', (e) => {
|
| 226 |
+
const rect = canvas.getBoundingClientRect();
|
| 227 |
+
mouseX = e.clientX - rect.left;
|
| 228 |
+
mouseY = e.clientY - rect.top;
|
| 229 |
+
});
|
| 230 |
+
|
| 231 |
+
canvas.addEventListener('touchmove', (e) => {
|
| 232 |
+
e.preventDefault();
|
| 233 |
+
const rect = canvas.getBoundingClientRect();
|
| 234 |
+
mouseX = e.touches[0].clientX - rect.left;
|
| 235 |
+
mouseY = e.touches[0].clientY - rect.top;
|
| 236 |
+
});
|
| 237 |
+
|
| 238 |
+
canvas.addEventListener('mousedown', () => isMouseDown = true);
|
| 239 |
+
canvas.addEventListener('mouseup', () => isMouseDown = false);
|
| 240 |
+
canvas.addEventListener('touchstart', (e) => {
|
| 241 |
+
e.preventDefault();
|
| 242 |
+
isMouseDown = true;
|
| 243 |
+
const rect = canvas.getBoundingClientRect();
|
| 244 |
+
mouseX = e.touches[0].clientX - rect.left;
|
| 245 |
+
mouseY = e.touches[0].clientY - rect.top;
|
| 246 |
+
});
|
| 247 |
+
canvas.addEventListener('touchend', () => isMouseDown = false);
|
| 248 |
+
|
| 249 |
+
// Asteroid class
|
| 250 |
+
class Asteroid {
|
| 251 |
+
constructor() {
|
| 252 |
+
this.width = 30 + Math.random() * 40;
|
| 253 |
+
this.height = this.width;
|
| 254 |
+
this.x = Math.random() * canvas.width;
|
| 255 |
+
this.y = -this.height;
|
| 256 |
+
this.speed = 2 + Math.random() * 4;
|
| 257 |
+
this.rotation = 0;
|
| 258 |
+
this.rotationSpeed = (Math.random() - 0.5) * 0.1;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
update() {
|
| 262 |
+
this.y += this.speed;
|
| 263 |
+
this.rotation += this.rotationSpeed;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
draw() {
|
| 267 |
+
ctx.save();
|
| 268 |
+
ctx.translate(this.x + this.width / 2, this.y + this.height / 2);
|
| 269 |
+
ctx.rotate(this.rotation);
|
| 270 |
+
ctx.strokeStyle = '#ff6b6b';
|
| 271 |
+
ctx.lineWidth = 2;
|
| 272 |
+
ctx.beginPath();
|
| 273 |
+
ctx.arc(0, 0, this.width / 2, 0, Math.PI * 2);
|
| 274 |
+
ctx.stroke();
|
| 275 |
+
ctx.restore();
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
isOffScreen() {
|
| 279 |
+
return this.y > canvas.height;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
collidesWith(obj) {
|
| 283 |
+
return this.x < obj.x + obj.width &&
|
| 284 |
+
this.x + this.width > obj.x &&
|
| 285 |
+
this.y < obj.y + obj.height &&
|
| 286 |
+
this.y + this.height > obj.y;
|
| 287 |
+
}
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
// Bullet class
|
| 291 |
+
class Bullet {
|
| 292 |
+
constructor(x, y) {
|
| 293 |
+
this.x = x;
|
| 294 |
+
this.y = y;
|
| 295 |
+
this.width = 4;
|
| 296 |
+
this.height = 15;
|
| 297 |
+
this.speed = 12;
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
update() {
|
| 301 |
+
this.y -= this.speed;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
draw() {
|
| 305 |
+
ctx.fillStyle = '#4ecdc4';
|
| 306 |
+
ctx.fillRect(this.x - this.width / 2, this.y, this.width, this.height);
|
| 307 |
+
// Add glow effect
|
| 308 |
+
ctx.shadowBlur = 10;
|
| 309 |
+
ctx.shadowColor = '#4ecdc4';
|
| 310 |
+
ctx.fillRect(this.x - this.width / 2, this.y, this.width, this.height);
|
| 311 |
+
ctx.shadowBlur = 0;
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
isOffScreen() {
|
| 315 |
+
return this.y < -this.height;
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
// Particle class for explosions
|
| 320 |
+
class Particle {
|
| 321 |
+
constructor(x, y, color) {
|
| 322 |
+
this.x = x;
|
| 323 |
+
this.y = y;
|
| 324 |
+
this.vx = (Math.random() - 0.5) * 8;
|
| 325 |
+
this.vy = (Math.random() - 0.5) * 8;
|
| 326 |
+
this.life = 1;
|
| 327 |
+
this.decay = 0.02;
|
| 328 |
+
this.color = color;
|
| 329 |
+
this.size = Math.random() * 4 + 2;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
update() {
|
| 333 |
+
this.x += this.vx;
|
| 334 |
+
this.y += this.vy;
|
| 335 |
+
this.life -= this.decay;
|
| 336 |
+
this.vx *= 0.98;
|
| 337 |
+
this.vy *= 0.98;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
draw() {
|
| 341 |
+
ctx.save();
|
| 342 |
+
ctx.globalAlpha = this.life;
|
| 343 |
+
ctx.fillStyle = this.color;
|
| 344 |
+
ctx.fillRect(this.x, this.y, this.size, this.size);
|
| 345 |
+
ctx.restore();
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
isDead() {
|
| 349 |
+
return this.life <= 0;
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
// Create explosion effect
|
| 354 |
+
function createExplosion(x, y, color) {
|
| 355 |
+
for (let i = 0; i < 20; i++) {
|
| 356 |
+
particles.push(new Particle(x, y, color));
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
// Spawn asteroids
|
| 361 |
+
let asteroidSpawnTimer = 0;
|
| 362 |
+
function spawnAsteroid() {
|
| 363 |
+
asteroids.push(new Asteroid());
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
// Update game
|
| 367 |
+
function update() {
|
| 368 |
+
if (!gameRunning) return;
|
| 369 |
+
|
| 370 |
+
// Update player position (smooth follow)
|
| 371 |
+
const dx = mouseX - player.x;
|
| 372 |
+
const dy = mouseY - player.y;
|
| 373 |
+
player.x += dx * 0.1;
|
| 374 |
+
player.y += dy * 0.1;
|
| 375 |
+
|
| 376 |
+
// Keep player on screen
|
| 377 |
+
player.x = Math.max(player.width / 2, Math.min(canvas.width - player.width / 2, player.x));
|
| 378 |
+
player.y = Math.max(player.height / 2, Math.min(canvas.height - player.height / 2, player.y));
|
| 379 |
+
|
| 380 |
+
// Auto-fire bullets
|
| 381 |
+
if (Math.random() < 0.15) {
|
| 382 |
+
bullets.push(new Bullet(player.x, player.y - player.height / 2));
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
// Update asteroids
|
| 386 |
+
asteroids.forEach(asteroid => asteroid.update());
|
| 387 |
+
|
| 388 |
+
// Update bullets
|
| 389 |
+
bullets.forEach(bullet => bullet.update());
|
| 390 |
+
|
| 391 |
+
// Update particles
|
| 392 |
+
particles.forEach(particle => particle.update());
|
| 393 |
+
|
| 394 |
+
// Check collisions
|
| 395 |
+
asteroids.forEach((asteroid, aIndex) => {
|
| 396 |
+
// Check collision with player
|
| 397 |
+
if (asteroid.collidesWith({
|
| 398 |
+
x: player.x - player.width / 2,
|
| 399 |
+
y: player.y - player.height / 2,
|
| 400 |
+
width: player.width,
|
| 401 |
+
height: player.height
|
| 402 |
+
})) {
|
| 403 |
+
asteroids.splice(aIndex, 1);
|
| 404 |
+
lives--;
|
| 405 |
+
createExplosion(player.x, player.y, '#ff6b6b');
|
| 406 |
+
updateUI();
|
| 407 |
+
|
| 408 |
+
if (lives <= 0) {
|
| 409 |
+
gameOver();
|
| 410 |
+
}
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
// Check collision with bullets
|
| 414 |
+
bullets.forEach((bullet, bIndex) => {
|
| 415 |
+
if (asteroid.collidesWith(bullet)) {
|
| 416 |
+
asteroids.splice(aIndex, 1);
|
| 417 |
+
bullets.splice(bIndex, 1);
|
| 418 |
+
score += Math.floor(asteroid.width);
|
| 419 |
+
createExplosion(asteroid.x + asteroid.width / 2, asteroid.y + asteroid.height / 2, '#ff6b6b');
|
| 420 |
+
updateUI();
|
| 421 |
+
}
|
| 422 |
+
});
|
| 423 |
+
});
|
| 424 |
+
|
| 425 |
+
// Remove off-screen objects
|
| 426 |
+
asteroids = asteroids.filter(asteroid => !asteroid.isOffScreen());
|
| 427 |
+
bullets = bullets.filter(bullet => !bullet.isOffScreen());
|
| 428 |
+
particles = particles.filter(particle => !particle.isDead());
|
| 429 |
+
|
| 430 |
+
// Spawn new asteroids
|
| 431 |
+
asteroidSpawnTimer++;
|
| 432 |
+
const spawnRate = Math.max(30, 120 - Math.floor(score / 100) * 10);
|
| 433 |
+
if (asteroidSpawnTimer > spawnRate) {
|
| 434 |
+
spawnAsteroid();
|
| 435 |
+
asteroidSpawnTimer = 0;
|
| 436 |
+
}
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
// Draw game
|
| 440 |
+
function draw() {
|
| 441 |
+
// Clear canvas
|
| 442 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 443 |
+
|
| 444 |
+
// Draw player spaceship
|
| 445 |
+
ctx.save();
|
| 446 |
+
ctx.translate(player.x, player.y);
|
| 447 |
+
|
| 448 |
+
// Draw ship body
|
| 449 |
+
ctx.fillStyle = '#4ecdc4';
|
| 450 |
+
ctx.beginPath();
|
| 451 |
+
ctx.moveTo(0, -player.height / 2);
|
| 452 |
+
ctx.lineTo(-player.width / 2, player.height / 2);
|
| 453 |
+
ctx.lineTo(0, player.height / 3);
|
| 454 |
+
ctx.lineTo(player.width / 2, player.height / 2);
|
| 455 |
+
ctx.closePath();
|
| 456 |
+
ctx.fill();
|
| 457 |
+
|
| 458 |
+
// Draw ship details
|
| 459 |
+
ctx.fillStyle = '#ffffff';
|
| 460 |
+
ctx.beginPath();
|
| 461 |
+
ctx.arc(0, 0, 5, 0, Math.PI * 2);
|
| 462 |
+
ctx.fill();
|
| 463 |
+
|
| 464 |
+
ctx.restore();
|
| 465 |
+
|
| 466 |
+
// Draw asteroids
|
| 467 |
+
asteroids.forEach(asteroid => asteroid.draw());
|
| 468 |
+
|
| 469 |
+
// Draw bullets
|
| 470 |
+
bullets.forEach(bullet => bullet.draw());
|
| 471 |
+
|
| 472 |
+
// Draw particles
|
| 473 |
+
particles.forEach(particle => particle.draw());
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
// Game loop
|
| 477 |
+
function gameLoop() {
|
| 478 |
+
update();
|
| 479 |
+
draw();
|
| 480 |
+
requestAnimationFrame(gameLoop);
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
// Update UI
|
| 484 |
+
function updateUI() {
|
| 485 |
+
scoreElement.textContent = score;
|
| 486 |
+
livesElement.textContent = lives;
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
// Start game
|
| 490 |
+
function startGame() {
|
| 491 |
+
gameRunning = true;
|
| 492 |
+
score = 0;
|
| 493 |
+
lives = 3;
|
| 494 |
+
asteroids = [];
|
| 495 |
+
bullets = [];
|
| 496 |
+
particles = [];
|
| 497 |
+
asteroidSpawnTimer = 0;
|
| 498 |
+
mouseX = canvas.width / 2;
|
| 499 |
+
mouseY = canvas.height - 100;
|
| 500 |
+
updateUI();
|
| 501 |
+
startScreen.classList.add('hidden');
|
| 502 |
+
gameOverScreen.classList.add('hidden');
|
| 503 |
+
}
|
| 504 |
+
|
| 505 |
+
// Game over
|
| 506 |
+
function gameOver() {
|
| 507 |
+
gameRunning = false;
|
| 508 |
+
finalScoreElement.textContent = `Final Score: ${score}`;
|
| 509 |
+
|
| 510 |
+
let message = '';
|
| 511 |
+
if (score < 100) {
|
| 512 |
+
message = "Keep practicing! You'll get better!";
|
| 513 |
+
} else if (score < 500) {
|
| 514 |
+
message = "Great job! You're getting the hang of it!";
|
| 515 |
+
} else if (score < 1000) {
|
| 516 |
+
message = "Excellent! You're a space defender!";
|
| 517 |
+
} else {
|
| 518 |
+
message = "Incredible! You're a legendary pilot!";
|
| 519 |
+
}
|
| 520 |
+
|
| 521 |
+
gameOverMessageElement.textContent = message;
|
| 522 |
+
gameOverScreen.classList.remove('hidden');
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
// Event listeners
|
| 526 |
+
startButton.addEventListener('click', startGame);
|
| 527 |
+
restartButton.addEventListener('click', startGame);
|
| 528 |
+
|
| 529 |
+
// Start game loop
|
| 530 |
+
gameLoop();
|
| 531 |
+
|
| 532 |
+
// Handle window resize
|
| 533 |
+
window.addEventListener('resize', () => {
|
| 534 |
+
resizeCanvas();
|
| 535 |
+
player.x = canvas.width / 2;
|
| 536 |
+
player.y = canvas.height - 100;
|
| 537 |
+
});
|
| 538 |
+
|
| 539 |
+
// Ensure video plays (some browsers require interaction)
|
| 540 |
+
document.addEventListener('click', () => {
|
| 541 |
+
if (video.paused) {
|
| 542 |
+
video.play().catch(e => console.log('Video play failed:', e));
|
| 543 |
+
}
|
| 544 |
+
}, { once: true });
|
| 545 |
+
</script>
|
| 546 |
+
</body>
|
| 547 |
+
</html>
|