Aquí tienes un juego de Space Invaders original con gráficos modernos, todo en un solo archivo HTML con CSS y JavaScript integrados:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Neo Space Invaders</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #0a0a1a;
font-family: 'Arial', sans-serif;
overflow: hidden;
color: #fff;
}
#game-container {
position: relative;
width: 800px;
height: 600px;
border: 2px solid #30cfd0;
box-shadow: 0 0 20px #30cfd0, 0 0 40px rgba(48, 207, 208, 0.3);
overflow: hidden;
background: linear-gradient(to bottom, #0a0a1a 0%, #1a1a3a 100%);
}
#game-canvas {
width: 100%;
height: 100%;
}
#start-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: rgba(10, 10, 26, 0.8);
z-index: 10;
}
#game-over-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
background: rgba(10, 10, 26, 0.8);
z-index: 10;
}
h1 {
font-size: 48px;
margin-bottom: 20px;
text-transform: uppercase;
letter-spacing: 3px;
color: #30cfd0;
text-shadow: 0 0 10px #30cfd0, 0 0 20px #30cfd0;
}
button {
padding: 15px 30px;
font-size: 18px;
background: transparent;
color: #fff;
border: 2px solid #30cfd0;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
text-transform: uppercase;
letter-spacing: 2px;
margin-top: 20px;
}
button:hover {
background-color: #30cfd0;
color: #0a0a1a;
box-shadow: 0 0 15px #30cfd0;
}
#score-display {
position: absolute;
top: 20px;
left: 20px;
font-size: 24px;
color: #fff;
text-shadow: 0 0 5px #30cfd0;
}
#lives-display {
position: absolute;
top: 20px;
right: 20px;
font-size: 24px;
color: #fff;
text-shadow: 0 0 5px #30cfd0;
}
.stars {
position: absolute;
width: 100%;
height: 100%;
z-index: 1;
}
.star {
position: absolute;
background-color: #fff;
border-radius: 50%;
}
</style>
</head>
<body>
<div id="game-container">
<div class="stars" id="stars"></div>
<canvas id="game-canvas"></canvas>
<div id="score-display">Puntos: 0</div>
<div id="lives-display">Vidas: 3</div>
<div id="start-screen">
<h1>Neo Space Invaders</h1>
<button id="start-button">Iniciar Juego</button>
</div>
<div id="game-over-screen">
<h1>Game Over</h1>
<div id="final-score">Puntuación: 0</div>
<button id="restart-button">Jugar de Nuevo</button>
</div>
</div>
<script>
// Configuración del juego
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const startScreen = document.getElementById('start-screen');
const gameOverScreen = document.getElementById('game-over-screen');
const startButton = document.getElementById('start-button');
const restartButton = document.getElementById('restart-button');
const scoreDisplay = document.getElementById('score-display');
const livesDisplay = document.getElementById('lives-display');
const finalScore = document.getElementById('final-score');
// Ajustar el tamaño del canvas
canvas.width = 800;
canvas.height = 600;
// Variables del juego
let score = 0;
let lives = 3;
let gameActive = false;
let lastTime = 0;
let enemySpeed = 1;
let enemyDirection = 1;
let enemyMoveDown = false;
let shootCooldown = 0;
let enemyShootCooldown = 0;
// Jugador
const player = {
x: canvas.width / 2 - 25,
y: canvas.height - 60,
width: 50,
height: 30,
speed: 5,
color: '#30cfd0',
moving: {
left: false,
right: false
},
shooting: false,
bullets: []
};
// Enemigos
const enemies = [];
const enemyBullets = [];
const enemyRows = 5;
const enemyCols = 10;
const enemyWidth = 40;
const enemyHeight = 30;
const enemyPadding = 15;
// Estrellas de fondo
const stars = [];
const numStars = 100;
// Crear estrellas
function createStars() {
const starsContainer = document.getElementById('stars');
starsContainer.innerHTML = '';
for (let i = 0; i < numStars; i++) {
const star = document.createElement('div');
star.classList.add('star');
const size = Math.random() * 3;
const x = Math.random() * 100;
const y = Math.random() * 100;
const opacity = Math.random() * 0.8 + 0.2;
const animationDuration = Math.random() * 3 + 2;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
star.style.left = `${x}%`;
star.style.top = `${y}%`;
star.style.opacity = opacity;
star.style.animation = `twinkle ${animationDuration}s infinite alternate`;
starsContainer.appendChild(star);
stars.push({
element: star,
x,
y,
speed: Math.random() * 0.05 + 0.01
});
}
}
// Inicializar enemigos
function initEnemies() {
enemies.length = 0;
for (let row = 0; row < enemyRows; row++) {
for (let col = 0; col < enemyCols; col++) {
const enemy = {
x: col * (enemyWidth + enemyPadding) + 50,
y: row * (enemyHeight + enemyPadding) + 50,
width: enemyWidth,
height: enemyHeight,
alive: true,
type: row, // Tipo de enemigo basado en la fila
points: (enemyRows - row) * 10 // Puntos basados en la fila
};
enemies.push(enemy);
}
}
}
// Reiniciar juego
function resetGame() {
score = 0;
lives = 3;
enemySpeed = 1;
player.x = canvas.width / 2 - 25;
player.bullets = [];
enemyBullets.length = 0;
scoreDisplay.textContent = `Puntos: ${score}`;
livesDisplay.textContent = `Vidas: ${lives}`;
initEnemies();
}
// Iniciar juego
function startGame() {
resetGame();
gameActive = true;
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
requestAnimationFrame(gameLoop);
}
// Game Over
function gameOver() {
gameActive = false;
gameOverScreen.style.display = 'flex';
finalScore.textContent = `Puntuación: ${score}`;
}
// Dibujar jugador
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.beginPath();
ctx.moveTo(player.x + player.width / 2, player.y);
ctx.lineTo(player.x + player.width, player.y + player.height);
ctx.lineTo(player.x, player.y + player.height);
ctx.closePath();
ctx.fill();
// Efecto de brillo
ctx.shadowBlur = 10;
ctx.shadowColor = player.color;
ctx.stroke();
ctx.shadowBlur = 0;
}
// Dibujar enemigos
function drawEnemies() {
enemies.forEach(enemy => {
if (enemy.alive) {
// Diferentes colores según el tipo
const colors = ['#ff4d4d', '#ff9933', '#ffff33', '#66ff66', '#66ccff'];
ctx.fillStyle = colors[enemy.type % colors.length];
// Diferentes formas según el tipo
ctx.beginPath();
if (enemy.type === 0) {
// Tipo 1: Ovni
ctx.ellipse(
enemy.x + enemy.width / 2,
enemy.y + enemy.height / 2,
enemy.width / 2,
enemy.height / 3,
0, 0, Math.PI * 2
);
ctx.fill();
// Cúpula
ctx.fillStyle = '#aaddff';
ctx.beginPath();
ctx.ellipse(
enemy.x + enemy.width / 2,
enemy.y + enemy.height / 2 - 5,
enemy.width / 3,
enemy.height / 4,
0, 0, Math.PI
);
ctx.fill();
} else if (enemy.type === 1) {
// Tipo 2: Cangrejo
ctx.rect(enemy.x + 5, enemy.y + 5, enemy.width - 10, enemy.height - 10);
ctx.fill();
// Pinzas
ctx.beginPath();
ctx.moveTo(enemy.x, enemy.y + enemy.height / 2);
ctx.lineTo(enemy.x + 5, enemy.y + enemy.height / 2 - 5);
ctx.lineTo(enemy.x + 5, enemy.y + enemy.height / 2 + 5);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.moveTo(enemy.x + enemy.width, enemy.y + enemy.height / 2);
ctx.lineTo(enemy.x + enemy.width - 5, enemy.y + enemy.height / 2 - 5);
ctx.lineTo(enemy.x + enemy.width - 5, enemy.y + enemy.height / 2 + 5);
ctx.closePath();
ctx.fill();
} else {
// Otros tipos: Formas geométricas
const vertices = 3 + enemy.type;
const radius = enemy.width / 2;
const centerX = enemy.x + enemy.width / 2;
const centerY = enemy.y + enemy.height / 2;
ctx.beginPath();
for (let i = 0; i < vertices; i++) {
const angle = (i * 2 * Math.PI / vertices) - Math.PI / 2;
const x = centerX + radius * Math.cos(angle);
const y = centerY + radius * Math.sin(angle);
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.closePath();
ctx.fill();
}
// Efecto de brillo
ctx.shadowBlur = 5;
ctx.shadowColor = colors[enemy.type % colors.length];
ctx.stroke();
ctx.shadowBlur = 0;
}
});
}
// Dibujar balas
function drawBullets() {
// Balas del jugador
ctx.fillStyle = '#30cfd0';
player.bullets.forEach(bullet => {
ctx.beginPath();
ctx.rect(bullet.x, bullet.y, bullet.width, bullet.height);
ctx.fill();
// Efecto de estela
ctx.fillStyle = 'rgba(48, 207, 208, 0.3)';
ctx.beginPath();
ctx.rect(bullet.x, bullet.y + bullet.height, bullet.width, bullet.height * 2);
ctx.fill();
ctx.fillStyle = '#30cfd0';
});
// Balas de enemigos
ctx.fillStyle = '#ff4d4d';
enemyBullets.forEach(bullet => {
ctx.beginPath();
ctx.rect(bullet.x, bullet.y, bullet.width, bullet.height);
ctx.fill();
// Efecto de estela
ctx.fillStyle = 'rgba(255, 77, 77, 0.3)';
ctx.beginPath();
ctx.rect(bullet.x, bullet.y - bullet.height, bullet.width, bullet.height * 2);
ctx.fill();
ctx.fillStyle = '#ff4d4d';
});
}
// Actualizar jugador
function updatePlayer(deltaTime) {
// Movimiento
if (player.moving.left && player.x > 0) {
player.x -= player.speed;
}
if (player.moving.right && player.x < canvas.width - player.width) {
player.x += player.speed;
}
// Disparar