Aquí te presento un Space Invaders moderno y original con gráficos avanzados, todo en una sola página:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Neon Space Invaders</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #000;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: 'Courier New', monospace;
overflow: hidden;
}
#gameWrapper {
position: relative;
width: 900px;
height: 700px;
}
canvas {
border: 2px solid #0ff;
box-shadow: 0 0 50px #0ff, inset 0 0 50px rgba(0,255,255,0.1);
background: radial-gradient(ellipse at center, #001122 0%, #000511 100%);
border-radius: 10px;
}
#startMenu, #gameOverMenu {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.9);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: #0ff;
backdrop-filter: blur(5px);
border-radius: 10px;
}
h1 {
font-size: 60px;
margin-bottom: 30px;
text-shadow: 0 0 30px #0ff, 0 0 60px #0ff;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
.btn {
padding: 20px 50px;
font-size: 28px;
background: transparent;
color: #0ff;
border: 2px solid #0ff;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 3px;
transition: all 0.3s;
margin: 10px;
font-family: 'Courier New', monospace;
}
.btn:hover {
background: #0ff;
color: #000;
box-shadow: 0 0 30px #0ff;
transform: scale(1.1);
}
#hud {
position: absolute;
top: 20px;
left: 20px;
color: #0ff;
font-size: 20px;
text-shadow: 0 0 10px #0ff;
}
#finalScore {
font-size: 30px;
color: #f0f;
margin: 20px 0;
text-shadow: 0 0 20px #f0f;
}
.hidden {
display: none !important;
}
</style>
</head>
<body>
<div id="gameWrapper">
<canvas id="gameCanvas" width="900" height="700"></canvas>
<div id="startMenu">
<h1>NEON INVADERS</h1>
<button class="btn" onclick="startGame()">INICIAR</button>
</div>
<div id="gameOverMenu" class="hidden">
<h1>GAME OVER</h1>
<div id="finalScore">Puntuación: 0</div>
<button class="btn" onclick="restartGame()">REINICIAR</button>
</div>
<div id="hud" class="hidden">
<div>PUNTOS: <span id="score">0</span></div>
<div>VIDAS: <span id="lives">3</span></div>
<div>NIVEL: <span id="level">1</span></div>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startMenu = document.getElementById('startMenu');
const gameOverMenu = document.getElementById('gameOverMenu');
const hud = document.getElementById('hud');
const scoreEl = document.getElementById('score');
const livesEl = document.getElementById('lives');
const levelEl = document.getElementById('level');
const finalScoreEl = document.getElementById('finalScore');
let game = {
active: false,
score: 0,
lives: 3,
level: 1,
enemySpeed: 1,
enemyDropDistance: 40
};
let stars = [];
let player = null;
let enemies = [];
let bullets = [];
let enemyBullets = [];
let particles = [];
let powerUps = [];
let enemyDirection = 1;
let lastEnemyShot = 0;
// Audio Context para sonidos
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();
function playSound(frequency, duration) {
const oscillator = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.frequency.value = frequency;
gainNode.gain.setValueAtTime(0.3, audioCtx.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + duration);
oscillator.start(audioCtx.currentTime);
oscillator.stop(audioCtx.currentTime + duration);
}
// Clase Estrella para el fondo
class Star {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 2;
this.speed = Math.random() * 2 + 0.5;
this.brightness = Math.random();
}
update() {
this.y += this.speed;
if (this.y > canvas.height) {
this.y = 0;
this.x = Math.random() * canvas.width;
}
this.brightness += (Math.random() - 0.5) * 0.1;
this.brightness = Math.max(0.3, Math.min(1, this.brightness));
}
draw() {
ctx.fillStyle = `rgba(255, 255, 255, ${this.brightness})`;
ctx.fillRect(this.x, this.y, this.size, this.size);
}
}
// Clase Jugador
class Player {
constructor() {
this.width = 60;
this.height = 40;
this.x = canvas.width / 2 - this.width / 2;
this.y = canvas.height - 100;
this.speed = 8;
this.canShoot = true;
this.shootCooldown = 0;
this.powerUpTime = 0;
this.shield = false;
}
update() {
this.shootCooldown--;
if (this.shootCooldown <= 0) this.canShoot = true;
if (this.powerUpTime > 0) {
this.powerUpTime--;
if (this.powerUpTime <= 0) this.shield = false;
}
}
draw() {
ctx.save();
// Efecto de escudo
if (this.shield) {
ctx.strokeStyle = '#ff0';
ctx.lineWidth = 3;
ctx.shadowBlur = 20;
ctx.shadowColor = '#ff0';
ctx.beginPath();
ctx.arc(this.x + this.width/2, this.y + this.height/2, 40, 0, Math.PI * 2);
ctx.stroke();
}
// Nave principal
ctx.fillStyle = '#0ff';
ctx.shadowBlur = 20;
ctx.shadowColor = '#0ff';
ctx.beginPath();
ctx.moveTo(this.x + this.width/2, this.y);
ctx.lineTo(this.x, this.y + this.height);
ctx.lineTo(this.x + 15, this.y + this.height - 10);
ctx.lineTo(this.x + this.width - 15, this.y + this.height - 10);
ctx.lineTo(this.x + this.width, this.y + this.height);
ctx.closePath();
ctx.fill();
// Detalles
ctx.fillStyle = '#066';
ctx.fillRect(this.x + this.width/2 - 5, this.y + 10, 10, 15);
ctx.restore();
}
shoot() {
if (!this.canShoot) return;
bullets.push(new Bullet(this.x + this.width/2 - 2, this.y, -15, '#0ff', 10));
playSound(800, 0.1);
this.canShoot = false;
this.shootCooldown = 10;
}
moveLeft() {
this.x = Math.max(0, this.x - this.speed);
}
moveRight() {
this.x = Math.min(canvas.width - this.width, this.x + this.speed);
}
}
// Clase Enemigo
class Enemy {
constructor(x, y, type) {
this.type = type;
this.x = x;
this.y = y;
this.width = 40;
this.height = 30;
this.alive = true;
this.animFrame = 0;
this.points = type === 1 ? 10 : type === 2 ? 20 : 50;
this.color = type === 1 ? '#f0f' : type === 2 ? '#ff0' : '#f00';
}
update() {
this.animFrame += 0.1;
}
draw() {
if (!this.alive) return;
ctx.save();
ctx.fillStyle = this.color;
ctx.shadowBlur = 15;
ctx.shadowColor = this.color;
const wobble = Math.sin(this.animFrame) * 2;
if (this.type === 1) {
// Enemigo básico
ctx.beginPath();
ctx.arc(this.x + this.width/2, this.y + this.height/2 + wobble, 15, 0, Math.PI * 2);
ctx.fill();
} else if (this.type === 2) {
// Enemigo medio
ctx.fillRect(this.x + 5, this.y + wobble, this.width - 10, this.height);
} else {
// Enemigo fuerte
ctx.beginPath();
ctx.moveTo(this.x + this.width/2, this.y + wobble);
ctx.lineTo(this.x, this.y + this.height + wobble);
ctx.lineTo(this.x + this.width, this.y + this.height + wobble);
ctx.closePath();
ctx.fill();
}
ctx.restore();
}
}
// Clase Bala
class Bullet {
constructor(x, y, speed, color, damage = 10) {
this.x = x;
this.y = y;
this.width = 4;
this.height = 15;
this.speed = speed;
this.color = color;
this.damage = damage;
}
update() {
this.y += this.speed;
}
draw() {
ctx.fillStyle = this.color;
ctx.shadowBlur = 10;
ctx.shadowColor = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
// Clase Partícula
class Particle {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.vx = (Math.random() - 0.5) * 8;
this.vy = (Math.random() - 0.5) * 8;
this.size = Math.random() * 5 + 2;
this.life = 30;
this.color = color;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.life--;
this.size *= 0.96;
}
draw() {
ctx.fillStyle = this.color;
ctx.globalAlpha = this.life / 30;
ctx.fillRect(this.x, this.y, this.size, this.size);
ctx.globalAlpha = 1;
}
}
// Clase PowerUp
class PowerUp {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 20;
this.speed = 2;
this.angle = 0;
}
update() {
this.y += this.speed;
this.angle += 0.1;
}
draw() {
ctx.save();
ctx.translate(this.x + this.size/2, this.y + this.size/2);
ctx.rotate(this.angle);
ctx.fillStyle = '#ff0';
ctx.shadowBlur = 20;
ctx.shadowColor = '#ff0';
ctx.fillRect(-this.size/2, -this.size/2, this.size, this.size);
ctx.restore();
}
}
// Inicializar estrellas
function initStars() {
for (let i = 0; i < 100; i++) {
stars.push(new Star());
}
}
// Crear enemigos
function createEnemies() {
enemies = [];
const rows = 4 + game.level;
const cols = 8;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const type = row < 2 ? 3 : row < 4 ? 2 : 1;
enemies.push(new Enemy(
col * 80 + 100,
row * 50 + 50,
type
));
}
}
}
// Sistema de entrada
const keys = {};
document.addEventListener('keydown', e => keys[e.key] = true);
document.addEventListener('keyup', e => keys[e.key] = false);
// Actualizar juego
function update() {
if (!game.active) return;
// Actualizar estrellas
stars.forEach(star => star.update());
// Actualizar jugador
player.update();
if (keys['ArrowLeft'] || keys['a'] || keys['A']) player.moveLeft();
if (keys['ArrowRight'] || keys['d'] || keys['D']) player.moveRight();
if (keys[' '] || keys['Enter']) player.shoot();
// Actualizar enemigos
let shouldMoveDown = false;
enemies.forEach(enemy => {
if (!enemy.alive) return;
enemy.update();
enemy.x += enemyDirection * game.enemySpeed;
if (enemy.x <= 0 || enemy.x + enemy.width >= canvas.width) {
shouldMoveDown = true;
}
});
if (shouldMoveDown) {
enemyDirection *= -1;
enemies.forEach(enemy => {
if (enemy.alive) {
enemy.y += game.enemyDropDistance;
if (enemy.y + enemy.height >= player.y) {
gameOver();
}
}
});
}
// Disparo enemigo
if (Date.now() - lastEnemyShot > 1000) {
const aliveEnemies = enemies.filter(e => e.alive);
if (aliveEnemies.length > 0) {
const shooter = aliveEnemies[Math.floor(Math.random() * aliveEnemies.length)];
enemyBullets.push(new Bullet(
shooter.x + shooter.width/2 - 2,
shooter.y + shooter.height,
5,
shooter.color
));
playSound(300, 0.1);
lastEnemyShot = Date.now();
}
}
// Actualizar balas del jugador
bullets.forEach((bullet, index) => {
bullet.update();
if (bullet.y < 0) {
bullets.splice(index, 1);
return;
}
// Colisión con enemigos
enemies.forEach(enemy => {
if (!enemy.alive) return;
if (collision(bullet, enemy)) {
enemy.alive = false;
bullets.splice(index, 1);
game.score += enemy.points;
updateHUD();
// Crear explosión
for (let i = 0; i < 10; i++) {
particles.push(new Particle(
enemy.x + enemy.width/2,
enemy.y + enemy.height/2,
enemy.color
));
}
// PowerUp aleatorio
if (Math.random() < 0.1) {
powerUps.push(new PowerUp(
enemy.x + enemy.width/2 - 10,
enemy.y
));
}
playSound(200, 0.2);
}
});
});
// Actualizar balas enemigas
enemyBullets.forEach((bullet, index) => {
bullet.update();
if (bullet.y > canvas.height) {
enemyBullets.splice(index, 1);
return;
}
// Colisión con jugador
if (collision(bullet, player)) {
if (!player.shield) {
game.lives--;
updateHUD();
playSound(100, 0.3);
if (game.lives <= 0) {
gameOver();
}
}
enemyBullets.splice(index, 1);
// Partículas de impacto
for (let i = 0; i < 15; i++) {
particles.push(new Particle(
player.x + player.width/2,
player.y + player.height/2,
'#0ff'
));
}
}
});
// Actualizar partículas
particles.forEach((particle, index) => {
particle.update();
if (particle.life <= 0) {
particles.splice(index, 1);
}
});
// Actualizar power-ups
powerUps.forEach((powerUp, index) => {
powerUp.update();
if (powerUp.y > canvas.height) {
powerUps.splice(index, 1);
return;
}
// Colisión con jugador
if (collision(powerUp, player)) {
player.shield = true;
player.powerUpTime = 300;
powerUps.splice(index, 1);
playSound(1000, 0.2);
}
});
// Verificar si se completó el nivel
if (enemies.every(enemy => !enemy.alive)) {
nextLevel();
}
}
// Función de colisión
function collision(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;
}
// Dibujar todo
function draw() {
ctx.fillStyle = 'rgba(0, 5, 17, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Dibujar estrellas
stars.forEach(star => star.draw());
// Dibujar elementos del juego
if (game.active) {
player.draw();
enemies.forEach(enemy => enemy.draw());
bullets.forEach(bullet => bullet.draw());
enemyBullets.forEach(bullet => bullet.draw());
particles.forEach(particle => particle.draw());
powerUps.forEach(powerUp => powerUp.draw());
}
}
// Game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Actualizar HUD
function updateHUD() {
scoreEl.textContent = game.score;
livesEl.textContent = game.lives;
levelEl.textContent = game.level;
}
// Siguiente nivel
function nextLevel() {
game.level++;
game.enemySpeed += 0.5;
createEnemies();
updateHUD();
}
// Game Over
function gameOver() {
game.active = false;
gameOverMenu.classList.remove('hidden');
finalScoreEl.textContent = `Puntuación: ${game.score}`;
hud.classList.add('hidden');
}
// Iniciar juego
function startGame() {
startMenu.classList.add('hidden');
hud.classList.remove('hidden');
game.active = true;
game.score = 0;
game.lives = 3;
game.level = 1;
game.enemySpeed = 1;
player = new Player();
bullets = [];
enemyBullets = [];
particles = [];
powerUps = [];
createEnemies();
updateHUD();
}
// Reiniciar juego
function restartGame() {
gameOverMenu.classList.add('hidden');
startGame();
}
// Inicializar
initStars();
gameLoop();
</script>
</body>
</html>
Características del juego:
Gráficos Modernos:
- Efectos de neón con sombras brillantes
- Sistema de partículas para explosiones
- Fondo animado con estrellas en movimiento
- Animaciones fluidas para enemigos
- Efectos visuales de escudo y power-ups
Jugabilidad:
- Controles: Flechas/WASD para mover, Espacio/Enter para disparar
- 3 tipos de enemigos con diferentes puntos
- Sistema de niveles progresivos
- Power-ups de escudo protector
- Sonidos generados dinámicamente con Web Audio API
Características Originales:
- Enemigos animados con movimiento ondulante
- Sistema de partículas para efectos visuales
- Power-ups aleatorios al destruir enemigos
- Dificultad progresiva por niveles
- HUD moderno con información en tiempo real
Cómo jugar:
- Guarda el código en un archivo
.html
- Ábrelo en cualquier navegador moderno
- Haz clic en "INICIAR"
- Usa las teclas para moverte y disparar
- Recoge power-ups amarillos para activar el escudo
- Sobrevive el mayor tiempo posible
El juego incluye todo lo necesario en una sola página y está completamente funcional con gráficos modernos tipo neón.