I'll create a modern Space Invaders clone with particle effects, power-ups, and smooth graphics. Here's the complete implementation:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space Invaders - Modern</title>
<style>
body {
margin: 0;
padding: 0;
background: linear-gradient(135deg, #0c0c0c 0%, #1a1a2e 50%, #16213e 100%);
font-family: 'Courier New', monospace;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
}
#gameContainer {
text-align: center;
position: relative;
}
#gameCanvas {
border: 2px solid #00ffff;
border-radius: 10px;
box-shadow: 0 0 30px rgba(0, 255, 255, 0.3);
background: radial-gradient(circle at center, #001122 0%, #000011 100%);
}
#ui {
position: absolute;
top: 10px;
left: 10px;
color: #00ffff;
font-size: 18px;
text-shadow: 0 0 10px rgba(0, 255, 255, 0.8);
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #ff0040;
font-size: 32px;
text-shadow: 0 0 20px rgba(255, 0, 64, 0.8);
display: none;
}
.controls {
margin-top: 20px;
color: #00ffff;
font-size: 14px;
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="ui">
<div>Score: <span id="score">0</span></div>
<div>Lives: <span id="lives">3</span></div>
<div>Level: <span id="level">1</span></div>
</div>
<div id="gameOver">
<div>GAME OVER</div>
<div style="font-size: 16px; margin-top: 10px;">Press SPACE to restart</div>
</div>
<div class="controls">
Use ARROW KEYS to move • SPACEBAR to shoot • Collect power-ups!
</div>
</div>
<script>
class Game {
constructor() {
this.canvas = document.getElementById('gameCanvas');
this.ctx = this.canvas.getContext('2d');
this.width = this.canvas.width;
this.height = this.canvas.height;
this.reset();
this.setupEventListeners();
this.gameLoop();
}
reset() {
this.player = new Player(this.width / 2, this.height - 60);
this.bullets = [];
this.enemies = [];
this.enemyBullets = [];
this.powerUps = [];
this.particles = [];
this.score = 0;
this.lives = 3;
this.level = 1;
this.gameOver = false;
this.keys = {};
this.lastShot = 0;
this.enemySpeed = 1;
this.enemyDirection = 1;
this.lastEnemyShot = 0;
this.createEnemies();
this.updateUI();
}
createEnemies() {
this.enemies = [];
const rows = 5;
const cols = 10;
const enemyWidth = 40;
const enemyHeight = 30;
const spacing = 60;
const startX = (this.width - (cols * spacing)) / 2;
const startY = 80;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = startX + col * spacing;
const y = startY + row * spacing;
const type = row < 2 ? 'fast' : row < 4 ? 'medium' : 'heavy';
this.enemies.push(new Enemy(x, y, type));
}
}
}
setupEventListeners() {
document.addEventListener('keydown', (e) => {
this.keys[e.code] = true;
if (e.code === 'Space' && this.gameOver) {
this.reset();
document.getElementById('gameOver').style.display = 'none';
}
});
document.addEventListener('keyup', (e) => {
this.keys[e.code] = false;
});
}
update() {
if (this.gameOver) return;
// Player movement
if (this.keys['ArrowLeft'] && this.player.x > 0) {
this.player.x -= this.player.speed;
}
if (this.keys['ArrowRight'] && this.player.x < this.width - this.player.width) {
this.player.x += this.player.speed;
}
// Player shooting
if (this.keys['Space'] && Date.now() - this.lastShot > this.player.fireRate) {
this.player.shoot(this.bullets);
this.lastShot = Date.now();
}
// Update bullets
this.bullets = this.bullets.filter(bullet => {
bullet.update();
return bullet.y > 0;
});
// Update enemy bullets
this.enemyBullets = this.enemyBullets.filter(bullet => {
bullet.update();
return bullet.y < this.height;
});
// Update enemies
let hitEdge = false;
this.enemies.forEach(enemy => {
enemy.x += this.enemySpeed * this.enemyDirection;
if (enemy.x <= 0 || enemy.x >= this.width - enemy.width) {
hitEdge = true;
}
});
if (hitEdge) {
this.enemyDirection *= -1;
this.enemies.forEach(enemy => {
enemy.y += 30;
});
}
// Enemy shooting
if (Date.now() - this.lastEnemyShot > 1000 + Math.random() * 2000) {
const shooters = this.enemies.filter(enemy =>
!this.enemies.some(other =>
other.x === enemy.x && other.y > enemy.y
)
);
if (shooters.length > 0) {
const shooter = shooters[Math.floor(Math.random() * shooters.length)];
this.enemyBullets.push(new Bullet(
shooter.x + shooter.width / 2,
shooter.y + shooter.height,
3,
'#ff0040'
));
}
this.lastEnemyShot = Date.now();
}
// Check collisions
this.checkCollisions();
// Update power-ups
this.powerUps = this.powerUps.filter(powerUp => {
powerUp.update();
return powerUp.y < this.height;
});
// Update particles
this.particles = this.particles.filter(particle => {
particle.update();
return particle.life > 0;
});
// Check win/lose conditions
if (this.enemies.length === 0) {
this.level++;
this.enemySpeed += 0.5;
this.createEnemies();
}
if (this.lives <= 0) {
this.gameOver = true;
document.getElementById('gameOver').style.display = 'block';
}
this.updateUI();
}
checkCollisions() {
// Player bullets vs enemies
this.bullets.forEach((bullet, bulletIndex) => {
this.enemies.forEach((enemy, enemyIndex) => {
if (this.isColliding(bullet, enemy)) {
// Create explosion particles
this.createExplosion(enemy.x + enemy.width / 2, enemy.y + enemy.height / 2);
// Update score
this.score += enemy.points;
// Chance to drop power-up
if (Math.random() < 0.15) {
this.powerUps.push(new PowerUp(enemy.x, enemy.y));
}
// Remove bullet and enemy
this.bullets.splice(bulletIndex, 1);
this.enemies.splice(enemyIndex, 1);
}
});
});
// Enemy bullets vs player
this.enemyBullets.forEach((bullet, bulletIndex) => {
if (this.isColliding(bullet, this.player)) {
this.lives--;
this.createExplosion(this.player.x + this.player.width / 2, this.player.y + this.player.height / 2);
this.enemyBullets.splice(bulletIndex, 1);
}
});
// Power-ups vs player
this.powerUps.forEach((powerUp, powerUpIndex) => {
if (this.isColliding(powerUp, this.player)) {
this.player.applyPowerUp(powerUp.type);
this.powerUps.splice(powerUpIndex, 1);
this.createPickupEffect(powerUp.x, powerUp.y);
}
});
// Enemies vs player
this.enemies.forEach(enemy => {
if (this.isColliding(enemy, this.player)) {
this.lives = 0;
}
});
}
isColliding(obj1, obj2) {
return obj1.x < obj2.x + obj2.width &&
obj1.x + obj1.width > obj2.x &&
obj1.y < obj2.y + obj2.height &&
obj1.y + obj1.height > obj2.y;
}
createExplosion(x, y) {
for (let i = 0; i < 15; i++) {
this.particles.push(new Particle(x, y, 'explosion'));
}
}
createPickupEffect(x, y) {
for (let i = 0; i < 10; i++) {
this.particles.push(new Particle(x, y, 'pickup'));
}
}
render() {
// Clear canvas with gradient
const gradient = this.ctx.createRadialGradient(
this.width / 2, this.height / 2, 0,
this.width / 2, this.height / 2, this.width
);
gradient.addColorStop(0, '#001122');
gradient.addColorStop(1, '#000011');
this.ctx.fillStyle = gradient;
this.ctx.fillRect(0, 0, this.width, this.height);
// Draw stars
this.drawStars();
// Draw game objects
this.player.draw(this.ctx);
this.bullets.forEach(bullet => bullet.draw(this.ctx));
this.enemyBullets.forEach(bullet => bullet.draw(this.ctx));
this.enemies.forEach(enemy => enemy.draw(this.ctx));
this.powerUps.forEach(powerUp => powerUp.draw(this.ctx));
this.particles.forEach(particle => particle.draw(this.ctx));
}
drawStars() {
this.ctx.fillStyle = '#ffffff';
for (let i = 0; i < 100; i++) {
const x = (i * 7) % this.width;
const y = (i * 13) % this.height;
const size = Math.sin(Date.now() * 0.001 + i) * 0.5 + 1;
this.ctx.globalAlpha = size * 0.5;
this.ctx.fillRect(x, y, 1, 1);
}
this.ctx.globalAlpha = 1;
}
updateUI() {
document.getElementById('score').textContent = this.score;
document.getElementById('lives').textContent = this.lives;
document.getElementById('level').textContent = this.level;
}
gameLoop() {
this.update();
this.render();
requestAnimationFrame(() => this.gameLoop());
}
}
class Player {
constructor(x, y) {
this.x = x;
this.y = y;
this.width = 40;
this.height = 30;
this.speed = 5;
this.fireRate = 200;
this.multiShot = false;
this.rapidFire = false;
this.powerUpTime = 0;
}
shoot(bullets) {
if (this.multiShot) {
bullets.push(new Bullet(this.x + 5, this.y, -8, '#00ffff'));
bullets.push(new Bullet(this.x + this.width / 2, this.y, -8, '#00ffff'));
bullets.push(new Bullet(this.x + this.width - 5, this.y, -8, '#00ffff'));
} else {
bullets.push(new Bullet(this.x + this.width / 2, this.y, -8, '#00ffff'));
}
}
applyPowerUp(type) {
this.powerUpTime = Date.now() + 10000; // 10 seconds
switch (type) {
case 'multiShot':
this.multiShot = true;
break;
case 'rapidFire':
this.fireRate = 100;
this.rapidFire = true;
break;
}
setTimeout(() => {
this.multiShot = false;
this.rapidFire = false;
this.fireRate = 200;
}, 10000);
}
draw(ctx) {
// Ship body
ctx.fillStyle = '#00ffff';
ctx.fillRect(this.x + 10, this.y + 10, 20, 20);
// Ship nose
ctx.beginPath();
ctx.moveTo(this.x + 20, this.y);
ctx.lineTo(this.x + 10, this.y + 10);
ctx.lineTo(this.x + 30, this.y + 10);
ctx.closePath();
ctx.fill();
// Engine glow
const time = Date.now() * 0.01;
ctx.fillStyle = `rgba(0, 255, 255, ${0.5 + Math.sin(time) * 0.3})`;
ctx.fillRect(this.x + 5, this.y + 25, 8, 8);
ctx.fillRect(this.x + 27, this.y + 25, 8, 8);
// Power-up indicator
if (this.powerUpTime > Date.now()) {
ctx.strokeStyle = '#ffff00';
ctx.lineWidth = 2;
ctx.strokeRect(this.x - 5, this.y - 5, this.width + 10, this.height + 10);
}
}
}
class Enemy {
constructor(x, y, type) {
this.x = x;
this.y = y;
this.width = 30;
this.height = 20;
this.type = type;
switch (type) {
case 'fast':
this.color = '#ff0040';
this.points = 30;
break;
case 'medium':
this.color = '#ff8000';
this.points = 20;
break;
case 'heavy':
this.color = '#40ff00';
this.points = 10;
break;
}
}
draw(ctx) {
const time = Date.now() * 0.005;
const pulse = Math.sin(time + this.x * 0.01) * 0.2 + 0.8;
ctx.fillStyle = this.color;
ctx.globalAlpha = pulse;
// Enemy body
ctx.fillRect(this.x + 5, this.y + 5, 20, 10);
// Enemy eyes
ctx.fillStyle = '#ffffff';
ctx.fillRect(this.x + 8, this.y + 2, 4, 4);
ctx.fillRect(this.x + 18, this.y + 2, 4, 4);
// Enemy legs
ctx.fillStyle = this.color;
ctx.fillRect(this.x + 2, this.y + 15, 4, 5);
ctx.fillRect(this.x + 12, this.y + 15, 4, 5);
ctx.fillRect(this.x + 24, this.y + 15, 4, 5);
ctx.globalAlpha = 1;
}
}
class Bullet {
constructor(x, y, speed, color) {
this.x = x;
this.y = y;
this.width = 4;
this.height = 8;
this.speed = speed;
this.color = color;
}
update() {
this.y += this.speed;
}
draw(ctx) {
ctx.fillStyle = this.color;
ctx.fillRect(this.x - 2, this.y, this.width, this.height);
// Bullet trail
ctx.globalAlpha = 0.5;
ctx.fillRect(this.x - 1, this.y + this.height, 2, 10);
ctx.globalAlpha = 1;
}
}
class PowerUp {
constructor(x, y) {
this.x = x;
this.y = y;
this.width = 20;
this.height = 20;
this.speed = 2;
this.type = Math.random() < 0.5 ? 'multiShot' : 'rapidFire';
this.angle = 0;
}
update() {
this.y += this.speed;
this.angle += 0.1;
}
draw(ctx) {
ctx.save();
ctx.translate(this.x + this.width / 2, this.y + this.height / 2);
ctx.rotate(this.angle);
if (this.type === 'multiShot') {
ctx.fillStyle = '#ffff00';
ctx.fillRect(-8, -2, 16, 4);
ctx.fillRect(-2, -8, 4, 16);
} else {
ctx.fillStyle = '#ff00ff';
for (let i = 0; i < 6; i++) {
ctx.rotate(Math.PI / 3);
ctx.fillRect(-6, -1, 12, 2);
}
}
ctx.restore();
// Glow effect
ctx.shadowColor = this.type === 'multiShot' ? '#ffff00' : '#ff00ff';
ctx.shadowBlur = 10;
ctx.globalAlpha = 0.3;
ctx.fillStyle = this.type === 'multiShot' ? '#ffff00' : '#ff00ff';
ctx.fillRect(this.x - 5, this.y - 5, this.width + 10, this.height + 10);
ctx.shadowBlur = 0;
ctx.globalAlpha = 1;
}
}
class Particle {
constructor(x, y, type) {
this.x = x;
this.y = y;
this.vx = (Math.random() - 0.5) * 8;
this.vy = (Math.random() - 0.5) * 8;
this.life = 1;
this.decay = 0.02;
this.size = Math.random() * 4 + 2;
this.type = type;
if (type === 'explosion') {
this.color = `hsl(${Math.random() * 60}, 100%, 50%)`;
} else {
this.color = `hsl(${120 + Math.random() * 60}, 100%, 50%)`;
}
}
update() {
this.x += this.vx;
this.y += this.vy;
this.life -= this.decay;
this.size *= 0.98;
}
draw(ctx) {
ctx.globalAlpha = this.life;
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.size, this.size);
ctx.globalAlpha = 1;
}
}
// Start the game
window.addEventListener('load', () => {
new Game();
});
</script>
</body>
</html>
The game automatically creates new waves of enemies when you clear a level, and the difficulty increases progressively. Power-ups have a 15% drop chance and last for 10 seconds when collected.