Aquí tienes un juego de supervivencia de zombies estilo Minecraft completamente funcional:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zombie Survival Craft</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: 'Courier New', monospace;
cursor: crosshair;
}
#gameCanvas {
display: block;
}
#hud {
position: absolute;
top: 10px;
left: 10px;
color: white;
text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
font-size: 16px;
font-weight: bold;
}
.health-bar {
width: 200px;
height: 20px;
background: rgba(0,0,0,0.5);
border: 2px solid white;
margin: 5px 0;
}
.health-fill {
height: 100%;
background: linear-gradient(to right, #ff0000, #00ff00);
transition: width 0.3s;
}
#inventory {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 5px;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
}
.inventory-slot {
width: 50px;
height: 50px;
border: 2px solid #666;
background: rgba(0,0,0,0.3);
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 12px;
cursor: pointer;
position: relative;
}
.inventory-slot.active {
border-color: #ffff00;
box-shadow: 0 0 10px #ffff00;
}
.item-count {
position: absolute;
bottom: 2px;
right: 2px;
font-size: 10px;
background: rgba(0,0,0,0.7);
padding: 2px 4px;
border-radius: 3px;
}
#crafting {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0,0,0,0.9);
padding: 20px;
border-radius: 10px;
color: white;
display: none;
}
.craft-grid {
display: grid;
grid-template-columns: repeat(3, 50px);
gap: 5px;
margin-bottom: 20px;
}
.craft-slot {
width: 50px;
height: 50px;
border: 1px solid #666;
background: rgba(255,255,255,0.1);
}
#instructions {
position: absolute;
top: 10px;
right: 10px;
color: white;
text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
font-size: 14px;
text-align: right;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0,0,0,0.9);
padding: 30px;
border-radius: 10px;
color: white;
text-align: center;
display: none;
}
button {
background: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background: #45a049;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="hud">
<div>Salud: <span id="health">100</span>/100</div>
<div class="health-bar">
<div class="health-fill" id="healthBar" style="width: 100%"></div>
</div>
<div>Zombies eliminados: <span id="kills">0</span></div>
<div>Día: <span id="day">1</span> - <span id="timeOfDay">Día</span></div>
</div>
<div id="inventory">
<div class="inventory-slot active" data-slot="0">
<span>✊</span>
<span class="item-count">∞</span>
</div>
<div class="inventory-slot" data-slot="1">
<span>🪵</span>
<span class="item-count" id="wood-count">0</span>
</div>
<div class="inventory-slot" data-slot="2">
<span>🪨</span>
<span class="item-count" id="stone-count">0</span>
</div>
<div class="inventory-slot" data-slot="3">
<span>🗡️</span>
<span class="item-count" id="sword-count">0</span>
</div>
<div class="inventory-slot" data-slot="4">
<span>🧱</span>
<span class="item-count" id="wall-count">0</span>
</div>
</div>
<div id="instructions">
<div>WASD - Mover</div>
<div>Click - Atacar/Construir</div>
<div>1-5 - Cambiar herramienta</div>
<div>E - Menú de crafteo</div>
<div>R - Recoger recursos</div>
</div>
<div id="crafting">
<h2>Crafteo</h2>
<div class="craft-grid">
<div class="craft-slot"></div>
<div class="craft-slot"></div>
<div class="craft-slot"></div>
<div class="craft-slot"></div>
<div class="craft-slot"></div>
<div class="craft-slot"></div>
<div class="craft-slot"></div>
<div class="craft-slot"></div>
<div class="craft-slot"></div>
</div>
<button onclick="craft('sword')">Crear Espada (3 🪵 + 2 🪨)</button>
<button onclick="craft('wall')">Crear Muro (4 🪨)</button>
<button onclick="closeCrafting()">Cerrar</button>
</div>
<div id="gameOver">
<h2>¡Has muerto!</h2>
<p>Sobreviviste <span id="finalDays">0</span> días</p>
<p>Eliminaste <span id="finalKills">0</span> zombies</p>
<button onclick="restartGame()">Reiniciar</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Variables globales
let scene, camera, renderer;
let player, playerMesh;
let zombies = [];
let resources = [];
let buildings = [];
let particles = [];
// Estado del juego
let gameState = {
health: 100,
kills: 0,
day: 1,
time: 0,
isNight: false,
inventory: {
wood: 0,
stone: 0,
sword: 0,
wall: 0
},
selectedItem: 0,
craftingOpen: false,
gameOver: false
};
// Controles
let keys = {};
let mouse = { x: 0, y: 0 };
// Inicializar Three.js
function init() {
// Escena
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x87CEEB, 10, 100);
// Cámara
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 10);
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById('gameCanvas').appendChild(renderer.domElement);
// Iluminación
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(50, 50, 50);
directionalLight.castShadow = true;
directionalLight.shadow.camera.left = -50;
directionalLight.shadow.camera.right = 50;
directionalLight.shadow.camera.top = 50;
directionalLight.shadow.camera.bottom = -50;
scene.add(directionalLight);
// Crear el mundo
createWorld();
createPlayer();
generateResources();
// Event listeners
document.addEventListener('keydown', (e) => keys[e.key.toLowerCase()] = true);
document.addEventListener('keyup', (e) => keys[e.key.toLowerCase()] = false);
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('click', onClick);
window.addEventListener('resize', onWindowResize);
// Atajos de teclado para inventario
for (let i = 1; i <= 5; i++) {
document.addEventListener('keydown', (e) => {
if (e.key === i.toString()) {
selectInventorySlot(i - 1);
}
});
}
// Tecla E para crafteo
document.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() === 'e') {
toggleCrafting();
}
});
}
// Crear el mundo
function createWorld() {
// Suelo
const groundGeometry = new THREE.PlaneGeometry(200, 200);
const groundMaterial = new THREE.MeshLambertMaterial({ color: 0x3a5f3a });
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// Crear algunos árboles
for (let i = 0; i < 20; i++) {
createTree(
Math.random() * 100 - 50,
Math.random() * 100 - 50
);
}
}
// Crear árbol
function createTree(x, z) {
const trunkGeometry = new THREE.CylinderGeometry(0.5, 0.5, 4);
const trunkMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
trunk.position.set(x, 2, z);
trunk.castShadow = true;
scene.add(trunk);
const leavesGeometry = new THREE.SphereGeometry(2, 8, 6);
const leavesMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 });
const leaves = new THREE.Mesh(leavesGeometry, leavesMaterial);
leaves.position.set(x, 5, z);
leaves.castShadow = true;
scene.add(leaves);
}
// Crear jugador
function createPlayer() {
const geometry = new THREE.BoxGeometry(1, 2, 1);
const material = new THREE.MeshLambertMaterial({ color: 0x0000ff });
playerMesh = new THREE.Mesh(geometry, material);
playerMesh.position.y = 1;
playerMesh.castShadow = true;
scene.add(playerMesh);
player = {
position: playerMesh.position,
velocity: new THREE.Vector3(),
speed: 0.1
};
}
// Generar recursos
function generateResources() {
// Madera
for (let i = 0; i < 15; i++) {
const woodGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const woodMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
const wood = new THREE.Mesh(woodGeometry, woodMaterial);
wood.position.set(
Math.random() * 80 - 40,
0.25,
Math.random() * 80 - 40
);
wood.userData = { type: 'wood', collected: false };
wood.castShadow = true;
resources.push(wood);
scene.add(wood);
}
// Piedra
for (let i = 0; i < 10; i++) {
const stoneGeometry = new THREE.SphereGeometry(0.3, 6, 6);
const stoneMaterial = new THREE.MeshLambertMaterial({ color: 0x808080 });
const stone = new THREE.Mesh(stoneGeometry, stoneMaterial);
stone.position.set(
Math.random() * 80 - 40,
0.15,
Math.random() * 80 - 40
);
stone.userData = { type: 'stone', collected: false };
stone.castShadow = true;
resources.push(stone);
scene.add(stone);
}
}
// Crear zombie
function createZombie() {
const geometry = new THREE.BoxGeometry(0.8, 1.8, 0.8);
const material = new THREE.MeshLambertMaterial({ color: 0x2d5016 });
const zombieMesh = new THREE.Mesh(geometry, material);
// Posición aleatoria lejos del jugador
const angle = Math.random() * Math.PI * 2;
const distance = 30 + Math.random() * 20;
zombieMesh.position.set(
Math.cos(angle) * distance,
0.9,
Math.sin(angle) * distance
);
zombieMesh.castShadow = true;
const zombie = {
mesh: zombieMesh,
health