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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>Arcade Blaster</title>
|
| 7 |
+
<style>
|
| 8 |
+
* {
|
| 9 |
+
margin: 0;
|
| 10 |
+
padding: 0;
|
| 11 |
+
box-sizing: border-box;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
body {
|
| 15 |
+
font-family: 'Arial', sans-serif;
|
| 16 |
+
background: linear-gradient(to bottom, #0a0a2a, #1a1a3a);
|
| 17 |
+
color: #fff;
|
| 18 |
+
min-height: 100vh;
|
| 19 |
+
display: flex;
|
| 20 |
+
flex-direction: column;
|
| 21 |
+
align-items: center;
|
| 22 |
+
justify-content: center;
|
| 23 |
+
overflow: hidden;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
.game-container {
|
| 27 |
+
position: relative;
|
| 28 |
+
width: 800px;
|
| 29 |
+
height: 500px;
|
| 30 |
+
border: 4px solid #ff0080;
|
| 31 |
+
border-radius: 10px;
|
| 32 |
+
box-shadow: 0 0 20px rgba(255, 0, 128, 0.5);
|
| 33 |
+
overflow: hidden;
|
| 34 |
+
background: #000;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
#gameCanvas {
|
| 38 |
+
background: #000;
|
| 39 |
+
display: block;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
.overlay {
|
| 43 |
+
position: absolute;
|
| 44 |
+
top: 0;
|
| 45 |
+
left: 0;
|
| 46 |
+
width: 100%;
|
| 47 |
+
height: 100%;
|
| 48 |
+
display: flex;
|
| 49 |
+
flex-direction: column;
|
| 50 |
+
align-items: center;
|
| 51 |
+
justify-content: center;
|
| 52 |
+
background: rgba(0, 0, 0, 0.8);
|
| 53 |
+
z-index: 10;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
h1 {
|
| 57 |
+
font-size: 3.5rem;
|
| 58 |
+
margin-bottom: 20px;
|
| 59 |
+
text-shadow: 0 0 10px #ff0080, 0 0 20px #ff0080;
|
| 60 |
+
color: #fff;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
.btn {
|
| 64 |
+
background: #ff0080;
|
| 65 |
+
color: white;
|
| 66 |
+
border: none;
|
| 67 |
+
padding: 15px 30px;
|
| 68 |
+
font-size: 1.2rem;
|
| 69 |
+
border-radius: 30px;
|
| 70 |
+
cursor: pointer;
|
| 71 |
+
margin: 15px 0;
|
| 72 |
+
transition: all 0.3s;
|
| 73 |
+
box-shadow: 0 0 15px rgba(255, 0, 128, 0.7);
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.btn:hover {
|
| 77 |
+
background: #ff3399;
|
| 78 |
+
transform: scale(1.05);
|
| 79 |
+
box-shadow: 0 0 20px rgba(255, 0, 128, 0.9);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.score-display {
|
| 83 |
+
position: absolute;
|
| 84 |
+
top: 20px;
|
| 85 |
+
left: 20px;
|
| 86 |
+
font-size: 1.5rem;
|
| 87 |
+
color: #fff;
|
| 88 |
+
text-shadow: 0 0 5px #ff0080;
|
| 89 |
+
z-index: 5;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
.instructions {
|
| 93 |
+
max-width: 600px;
|
| 94 |
+
text-align: center;
|
| 95 |
+
margin-top: 20px;
|
| 96 |
+
line-height: 1.6;
|
| 97 |
+
color: #aaa;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.controls {
|
| 101 |
+
display: flex;
|
| 102 |
+
gap: 20px;
|
| 103 |
+
margin-top: 15px;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.key {
|
| 107 |
+
background: #333;
|
| 108 |
+
padding: 5px 10px;
|
| 109 |
+
border-radius: 5px;
|
| 110 |
+
font-weight: bold;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
.hidden {
|
| 114 |
+
display: none;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
.game-over {
|
| 118 |
+
text-align: center;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
.final-score {
|
| 122 |
+
font-size: 2rem;
|
| 123 |
+
margin: 20px 0;
|
| 124 |
+
color: #ff0080;
|
| 125 |
+
}
|
| 126 |
+
</style>
|
| 127 |
+
</head>
|
| 128 |
+
<body>
|
| 129 |
+
<h1>ARCADE BLASTER</h1>
|
| 130 |
+
|
| 131 |
+
<div class="game-container">
|
| 132 |
+
<div class="score-display">SCORE: <span id="score">0</span></div>
|
| 133 |
+
|
| 134 |
+
<canvas id="gameCanvas" width="800" height="500"></canvas>
|
| 135 |
+
|
| 136 |
+
<div id="startScreen" class="overlay">
|
| 137 |
+
<h2>SPACE BLASTER</h2>
|
| 138 |
+
<p>Blast the asteroids before they hit your ship!</p>
|
| 139 |
+
<button id="startBtn" class="btn">START GAME</button>
|
| 140 |
+
<div class="controls">
|
| 141 |
+
<div><span class="key">←</span> <span class="key">→</span> Move</div>
|
| 142 |
+
<div><span class="key">SPACE</span> Shoot</div>
|
| 143 |
+
</div>
|
| 144 |
+
</div>
|
| 145 |
+
|
| 146 |
+
<div id="gameOverScreen" class="overlay hidden">
|
| 147 |
+
<div class="game-over">
|
| 148 |
+
<h2>GAME OVER</h2>
|
| 149 |
+
<p>Your ship was destroyed!</p>
|
| 150 |
+
<div class="final-score">FINAL SCORE: <span id="finalScore">0</span></div>
|
| 151 |
+
<button id="restartBtn" class="btn">PLAY AGAIN</button>
|
| 152 |
+
</div>
|
| 153 |
+
</div>
|
| 154 |
+
</div>
|
| 155 |
+
|
| 156 |
+
<div class="instructions">
|
| 157 |
+
<p>Destroy as many asteroids as possible. Each hit gives you 10 points.</p>
|
| 158 |
+
<p>Game gets faster as your score increases. Avoid colliding with asteroids!</p>
|
| 159 |
+
</div>
|
| 160 |
+
|
| 161 |
+
<script>
|
| 162 |
+
// Game elements
|
| 163 |
+
const canvas = document.getElementById('gameCanvas');
|
| 164 |
+
const ctx = canvas.getContext('2d');
|
| 165 |
+
const scoreElement = document.getElementById('score');
|
| 166 |
+
const finalScoreElement = document.getElementById('finalScore');
|
| 167 |
+
const startScreen = document.getElementById('startScreen');
|
| 168 |
+
const gameOverScreen = document.getElementById('gameOverScreen');
|
| 169 |
+
const startBtn = document.getElementById('startBtn');
|
| 170 |
+
const restartBtn = document.getElementById('restartBtn');
|
| 171 |
+
|
| 172 |
+
// Game state
|
| 173 |
+
let score = 0;
|
| 174 |
+
let gameRunning = false;
|
| 175 |
+
let animationId;
|
| 176 |
+
|
| 177 |
+
// Player ship
|
| 178 |
+
const player = {
|
| 179 |
+
x: canvas.width / 2 - 25,
|
| 180 |
+
y: canvas.height - 70,
|
| 181 |
+
width: 50,
|
| 182 |
+
height: 60,
|
| 183 |
+
speed: 8,
|
| 184 |
+
movingLeft: false,
|
| 185 |
+
movingRight: false,
|
| 186 |
+
color: '#3a86ff'
|
| 187 |
+
};
|
| 188 |
+
|
| 189 |
+
// Lasers array
|
| 190 |
+
const lasers = [];
|
| 191 |
+
const laserSpeed = 10;
|
| 192 |
+
|
| 193 |
+
// Asteroids array
|
| 194 |
+
const asteroids = [];
|
| 195 |
+
let asteroidSpeed = 2;
|
| 196 |
+
|
| 197 |
+
// Initialize game
|
| 198 |
+
function init() {
|
| 199 |
+
score = 0;
|
| 200 |
+
scoreElement.textContent = score;
|
| 201 |
+
player.x = canvas.width / 2 - 25;
|
| 202 |
+
lasers.length = 0;
|
| 203 |
+
asteroids.length = 0;
|
| 204 |
+
asteroidSpeed = 2;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
// Draw player ship
|
| 208 |
+
function drawPlayer() {
|
| 209 |
+
// Ship body
|
| 210 |
+
ctx.fillStyle = player.color;
|
| 211 |
+
ctx.beginPath();
|
| 212 |
+
ctx.moveTo(player.x + player.width / 2, player.y);
|
| 213 |
+
ctx.lineTo(player.x + player.width, player.y + player.height);
|
| 214 |
+
ctx.lineTo(player.x, player.y + player.height);
|
| 215 |
+
ctx.closePath();
|
| 216 |
+
ctx.fill();
|
| 217 |
+
|
| 218 |
+
// Cockpit
|
| 219 |
+
ctx.fillStyle = '#ffbe0b';
|
| 220 |
+
ctx.beginPath();
|
| 221 |
+
ctx.arc(player.x + player.width / 2, player.y + 20, 10, 0, Math.PI * 2);
|
| 222 |
+
ctx.fill();
|
| 223 |
+
|
| 224 |
+
// Engine glow
|
| 225 |
+
ctx.fillStyle = '#ff006e';
|
| 226 |
+
ctx.beginPath();
|
| 227 |
+
ctx.arc(player.x + player.width / 2, player.y + player.height + 5, 8, 0, Math.PI);
|
| 228 |
+
ctx.fill();
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
// Draw lasers
|
| 232 |
+
function drawLasers() {
|
| 233 |
+
ctx.fillStyle = '#00ff9d';
|
| 234 |
+
for (let i = 0; i < lasers.length; i++) {
|
| 235 |
+
ctx.beginPath();
|
| 236 |
+
ctx.arc(lasers[i].x, lasers[i].y, 4, 0, Math.PI * 2);
|
| 237 |
+
ctx.fill();
|
| 238 |
+
}
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
// Draw asteroids
|
| 242 |
+
function drawAsteroids() {
|
| 243 |
+
ctx.fillStyle = '#8a817c';
|
| 244 |
+
for (let i = 0; i < asteroids.length; i++) {
|
| 245 |
+
ctx.beginPath();
|
| 246 |
+
ctx.arc(asteroids[i].x, asteroids[i].y, asteroids[i].radius, 0, Math.PI * 2);
|
| 247 |
+
ctx.fill();
|
| 248 |
+
|
| 249 |
+
// Add some crater details
|
| 250 |
+
ctx.fillStyle = '#5a5754';
|
| 251 |
+
ctx.beginPath();
|
| 252 |
+
ctx.arc(asteroids[i].x - 5, asteroids[i].y - 3, 3, 0, Math.PI * 2);
|
| 253 |
+
ctx.arc(asteroids[i].x + 6, asteroids[i].y + 4, 2, 0, Math.PI * 2);
|
| 254 |
+
ctx.fill();
|
| 255 |
+
ctx.fillStyle = '#8a817c';
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
// Update game state
|
| 260 |
+
function update() {
|
| 261 |
+
// Move player
|
| 262 |
+
if (player.movingLeft && player.x > 0) {
|
| 263 |
+
player.x -= player.speed;
|
| 264 |
+
}
|
| 265 |
+
if (player.movingRight && player.x + player.width < canvas.width) {
|
| 266 |
+
player.x += player.speed;
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
// Move lasers
|
| 270 |
+
for (let i = lasers.length - 1; i >= 0; i--) {
|
| 271 |
+
lasers[i].y -= laserSpeed;
|
| 272 |
+
|
| 273 |
+
// Remove lasers that go off screen
|
| 274 |
+
if (lasers[i].y < 0) {
|
| 275 |
+
lasers.splice(i, 1);
|
| 276 |
+
}
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
// Move asteroids and check for collisions
|
| 280 |
+
for (let i = asteroids.length - 1; i >= 0; i--) {
|
| 281 |
+
asteroids[i].y += asteroids[i].speed;
|
| 282 |
+
|
| 283 |
+
// Check if asteroid is out of screen
|
| 284 |
+
if (asteroids[i].y > canvas.height) {
|
| 285 |
+
asteroids.splice(i, 1);
|
| 286 |
+
continue;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
// Check for collision with player
|
| 290 |
+
const dx = asteroids[i].x - (player.x + player.width / 2);
|
| 291 |
+
const dy = asteroids[i].y - (player.y + player.height / 2);
|
| 292 |
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
| 293 |
+
|
| 294 |
+
if (distance < asteroids[i].radius + player.width / 2) {
|
| 295 |
+
gameOver();
|
| 296 |
+
return;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
// Check for collision with lasers
|
| 300 |
+
for (let j = lasers.length - 1; j >= 0; j--) {
|
| 301 |
+
const ldx = asteroids[i].x - lasers[j].x;
|
| 302 |
+
const ldy = asteroids[i].y - lasers[j].y;
|
| 303 |
+
const laserDistance = Math.sqrt(ldx * ldx + ldy * ldy);
|
| 304 |
+
|
| 305 |
+
if (laserDistance < asteroids[i].radius + 4) {
|
| 306 |
+
// Remove the laser and asteroid
|
| 307 |
+
lasers.splice(j, 1);
|
| 308 |
+
asteroids.splice(i, 1);
|
| 309 |
+
|
| 310 |
+
// Increase score
|
| 311 |
+
score += 10;
|
| 312 |
+
scoreElement.textContent = score;
|
| 313 |
+
|
| 314 |
+
// Increase difficulty every 100 points
|
| 315 |
+
if (score % 100 === 0) {
|
| 316 |
+
asteroidSpeed += 0.5;
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
break;
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
// Add new asteroids
|
| 325 |
+
if (Math.random() < 0.03) {
|
| 326 |
+
createAsteroid();
|
| 327 |
+
}
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
// Create a new asteroid
|
| 331 |
+
function createAsteroid() {
|
| 332 |
+
const radius = Math.random() * 20 + 10;
|
| 333 |
+
const x = Math.random() * (canvas.width - radius * 2) + radius;
|
| 334 |
+
asteroids.push({
|
| 335 |
+
x: x,
|
| 336 |
+
y: -radius,
|
| 337 |
+
radius: radius,
|
| 338 |
+
speed: Math.random() * 2 + asteroidSpeed
|
| 339 |
+
});
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
// Draw stars in the background
|
| 343 |
+
function drawStars() {
|
| 344 |
+
ctx.fillStyle = '#ffffff';
|
| 345 |
+
for (let i = 0; i < 100; i++) {
|
| 346 |
+
const x = Math.random() * canvas.width;
|
| 347 |
+
const y = Math.random() * canvas.height;
|
| 348 |
+
const size = Math.random() * 2;
|
| 349 |
+
ctx.beginPath();
|
| 350 |
+
ctx.arc(x, y, size, 0, Math.PI * 2);
|
| 351 |
+
ctx.fill();
|
| 352 |
+
}
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
// Main game loop
|
| 356 |
+
function gameLoop() {
|
| 357 |
+
if (!gameRunning) return;
|
| 358 |
+
|
| 359 |
+
// Clear canvas
|
| 360 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 361 |
+
|
| 362 |
+
// Draw background
|
| 363 |
+
drawStars();
|
| 364 |
+
|
| 365 |
+
// Update game state
|
| 366 |
+
update();
|
| 367 |
+
|
| 368 |
+
// Draw game elements
|
| 369 |
+
drawPlayer();
|
| 370 |
+
drawLasers();
|
| 371 |
+
drawAsteroids();
|
| 372 |
+
|
| 373 |
+
// Request next frame
|
| 374 |
+
animationId = requestAnimationFrame(gameLoop);
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
// Start the game
|
| 378 |
+
function startGame() {
|
| 379 |
+
init();
|
| 380 |
+
gameRunning = true;
|
| 381 |
+
startScreen.classList.add('hidden');
|
| 382 |
+
gameOverScreen.classList.add('hidden');
|
| 383 |
+
gameLoop();
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
// Game over
|
| 387 |
+
function gameOver() {
|
| 388 |
+
gameRunning = false;
|
| 389 |
+
cancelAnimationFrame(animationId);
|
| 390 |
+
finalScoreElement.textContent = score;
|
| 391 |
+
gameOverScreen.classList.remove('hidden');
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
// Fire a laser
|
| 395 |
+
function fireLaser() {
|
| 396 |
+
if (!gameRunning) return;
|
| 397 |
+
|
| 398 |
+
lasers.push({
|
| 399 |
+
x: player.x + player.width / 2,
|
| 400 |
+
y: player.y
|
| 401 |
+
});
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
// Event listeners
|
| 405 |
+
startBtn.addEventListener('click', startGame);
|
| 406 |
+
restartBtn.addEventListener('click', startGame);
|
| 407 |
+
|
| 408 |
+
// Keyboard controls
|
| 409 |
+
document.addEventListener('keydown', (e) => {
|
| 410 |
+
if (e.key === 'ArrowLeft') player.movingLeft = true;
|
| 411 |
+
if (e.key === 'ArrowRight') player.movingRight = true;
|
| 412 |
+
if (e.key === ' ' || e.key === 'Spacebar') fireLaser();
|
| 413 |
+
});
|
| 414 |
+
|
| 415 |
+
document.addEventListener('keyup', (e) => {
|
| 416 |
+
if (e.key === 'ArrowLeft') player.movingLeft = false;
|
| 417 |
+
if (e.key === 'ArrowRight') player.movingRight = false;
|
| 418 |
+
});
|
| 419 |
+
|
| 420 |
+
// Touch controls for mobile
|
| 421 |
+
let touchStartX = 0;
|
| 422 |
+
|
| 423 |
+
canvas.addEventListener('touchstart', (e) => {
|
| 424 |
+
touchStartX = e.touches[0].clientX;
|
| 425 |
+
e.preventDefault();
|
| 426 |
+
});
|
| 427 |
+
|
| 428 |
+
canvas.addEventListener('touchmove', (e) => {
|
| 429 |
+
const touchX = e.touches[0].clientX;
|
| 430 |
+
const diffX = touchX - touchStartX;
|
| 431 |
+
|
| 432 |
+
if (diffX > 10) {
|
| 433 |
+
player.movingRight = true;
|
| 434 |
+
player.movingLeft = false;
|
| 435 |
+
} else if (diffX < -10) {
|
| 436 |
+
player.movingLeft = true;
|
| 437 |
+
player.movingRight = false;
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
touchStartX = touchX;
|
| 441 |
+
e.preventDefault();
|
| 442 |
+
});
|
| 443 |
+
|
| 444 |
+
canvas.addEventListener('touchend', () => {
|
| 445 |
+
player.movingLeft = false;
|
| 446 |
+
player.movingRight = false;
|
| 447 |
+
});
|
| 448 |
+
|
| 449 |
+
canvas.addEventListener('click', () => {
|
| 450 |
+
if (gameRunning) fireLaser();
|
| 451 |
+
});
|
| 452 |
+
|
| 453 |
+
// Initialize the game
|
| 454 |
+
init();
|
| 455 |
+
</script>
|
| 456 |
+
</body>
|
| 457 |
+
</html>
|