Aqui está um exemplo de Tetris “verdadeiramente” 3D numa única página HTML, usando Three.js para renderizar cubos em um volume 3D. Você pode ampliar esse código adicionando mais shapes (tetracubos), ajustando controles, cores, níveis, etc.
html
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<title>Tetris 3D em um só HTML</title>
<style>
body { margin:0; overflow:hidden; background:#111; }
#info {
position:absolute; top:10px; left:10px; color:#fff;
font-family:sans-serif; z-index:1;
}
</style>
</head>
<body>
<div id="info">
← → : mover X ↑ ↓ : mover Z<br>
Q/W/E: rotX/rotY/rotZ Space: queda rápida
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// ————— Parâmetros do jogo —————
const WIDTH = 5; // eixos X (colunas)
const DEPTH = 5; // eixos Z (linhas em profundidade)
const HEIGHT = 10; // eixos Y (altura para queda)
const CUBE_SIZE = 1;
const DROP_INTERVAL = 800; // ms
// ————— Setup Three.js —————
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45,1,0.1,100);
camera.position.set(WIDTH*1.5,HEIGHT*1.5,DEPTH*1.5);
camera.lookAt(WIDTH/2,HEIGHT/2,DEPTH/2);
const renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// luz
scene.add(new THREE.AmbientLight(0x888888));
const dl = new THREE.DirectionalLight(0xffffff,0.6);
dl.position.set(1,2,1);
scene.add(dl);
// wireframe da “caixa” de jogo
const box = new THREE.BoxHelper(
new THREE.Mesh(
new THREE.BoxGeometry(WIDTH,CUBE_SIZE*HEIGHT,DEPTH)
),
0x444444
);
box.position.set(WIDTH/2,HEIGHT/2,DEPTH/2);
scene.add(box);
// ————— Grade de ocupação —————
// board[x][z][y] = null ou Mesh
const board = [];
for(let x=0;x<WIDTH;x++){
board[x] = [];
for(let z=0;z<DEPTH;z++){
board[x][z] = Array(HEIGHT).fill(null);
}
}
// ————— Definição de shapes (tetracubos planos) —————
// Offset [x,z,y]
const SHAPES = [
{ color:0x33ffcc, blocks:[
[0,0,0], [1,0,0], [-1,0,0], [-2,0,0] // I
]},
{ color:0xff5533, blocks:[
[0,0,0], [1,0,0], [0,0,1], [1,0,1] // O
]},
{ color:0x3388ff, blocks:[
[0,0,0], [0,0,1], [0,0,-1], [1,0,-1] // L
]},
{ color:0xffff33, blocks:[
[0,0,0], [-1,0,0], [1,0,0], [0,0,1] // T
]},
{ color:0xff33aa, blocks:[
[0,0,0], [1,0,0], [0,0,-1], [-1,0,-1] // S
]},
{ color:0x33ff11, blocks:[
[0,0,0], [-1,0,0], [0,0,-1], [1,0,-1] // Z
]}
];
// ————— Peça atual —————
let current = null;
function spawnPiece(){
const s = SHAPES[Math.floor(Math.random()*SHAPES.length)];
const meshGroup = new THREE.Group();
const mat = new THREE.MeshLambertMaterial({color:s.color});
s.blocks.forEach(off=>{
const cube = new THREE.Mesh(
new THREE.BoxGeometry(CUBE_SIZE,CUBE_SIZE,CUBE_SIZE),
mat
);
cube.position.set(off[0]+WIDTH/2, off[2]+HEIGHT-1, off[1]+DEPTH/2);
meshGroup.add(cube);
});
scene.add(meshGroup);
current = {
group: meshGroup,
offsets: s.blocks.map(a=>a.slice()), // clone
pos: { x: Math.floor(WIDTH/2), y: HEIGHT-1, z: Math.floor(DEPTH/2) },
color: s.color
};
}
// ————— Colocar peça no board —————
function lockPiece(){
current.group.children.forEach(cube=>{
const X = Math.round(cube.position.x);
const Y = Math.round(cube.position.y);
const Z = Math.round(cube.position.z);
if(Y>=HEIGHT) return; // fora do view
board[X][Z][Y] = cube;
});
clearLines();
current = null;
}
// ————— Limpar camadas completas (planos XY) —————
function clearLines(){
for(let y=0;y<HEIGHT;y++){
let full = true;
for(let x=0;x<WIDTH && full;x++){
for(let z=0;z<DEPTH;z++){
if(!board[x][z][y]){ full=false; break; }
}
}
if(full){
// remove meshes dessa camada
for(let x=0;x<WIDTH;x++)for(let z=0;z<DEPTH;z++){
scene.remove(board[x][z][y]);
board[x][z][y]=null;
}
// descer tudo acima
for(let yy=y+1;yy<HEIGHT;yy++){
for(let x=0;x<WIDTH;x++){
for(let z=0;z<DEPTH;z++){
const cube = board[x][z][yy];
board[x][z][yy] = null;
if(cube){
cube.position.y -= 1;
board[x][z][yy-1] = cube;
}
}
}
}
y--; // rechecar mesma camada
}
}
}
// ————— Checa colisão —————
function collide(offsets, pos){
return offsets.some(off=>{
const x = pos.x + off[0];
const y = pos.y + off[2];
const z = pos.z + off[1];
if(x<0||x>=WIDTH||z<0||z>=DEPTH||y<0) return true;
if(board[x][z][y]) return true;
});
}
// ————— Aplica posição atual aos cubes —————
function updateCurrent(){
current.group.children.forEach((cube,i)=>{
const off = current.offsets[i];
cube.position.set(
current.pos.x + off[0]+0.5,
current.pos.y + off[2]+0.5,
current.pos.z + off[1]+0.5
);
});
}
// ————— Rotação de offsets em torno de eixo X,Y,Z —————
function rotateOffsets(axis){
current.offsets = current.offsets.map(off=>{
let [x,z,y] = off;
let nx, ny, nz;
if(axis==='X'){
// rota Y<->Z
ny = -z; nz = y; nx = x;
} else
if(axis==='Y'){
nx = -z; nz = x; ny = y;
} else {
// Z eixo
nx = y; ny = -x; nz = z;
}
return [nx,nz,ny];
});
}
// ————— Input —————
window.addEventListener('keydown', e=>{
if(!current) return;
const mv = { x:0,z:0 };
if(e.key==='ArrowLeft') mv.x=-1;
if(e.key==='ArrowRight') mv.x=1;
if(e.key==='ArrowUp') mv.z=-1;
if(e.key==='ArrowDown') mv.z=1;
if(mv.x||mv.z){
const old = {...current.pos};
current.pos.x += mv.x; current.pos.z += mv.z;
if(collide(current.offsets, current.pos)) current.pos=old;
updateCurrent();
}
if(e.key===' '){
// drop rápido
while(!collide(current.offsets,{x:current.pos.x,y:current.pos.y-1,z:current.pos.z})){
current.pos.y--; updateCurrent();
}
lockPiece();
}
if(e.key==='q'){ // rotX
const oldOff = JSON.parse(JSON.stringify(current.offsets));
rotateOffsets('X'); if(collide(current.offsets, current.pos)) current.offsets = oldOff;
updateCurrent();
}
if(e.key==='w'){ // rotY
const oldOff = JSON.parse(JSON.stringify(current.offsets));
rotateOffsets('Y'); if(collide(current.offsets, current.pos)) current.offsets = oldOff;
updateCurrent();
}
if(e.key==='e'){ // rotZ
const oldOff = JSON.parse(JSON.stringify(current.offsets));
rotateOffsets('Z'); if(collide(current.offsets, current.pos)) current.offsets = oldOff;
updateCurrent();
}
});
// ————— Loop de queda —————
let lastDrop = 0;
function animate(ts){
requestAnimationFrame(animate);
if(!current){
spawnPiece();
} else if(ts - lastDrop > DROP_INTERVAL){
lastDrop = ts;
if(!collide(current.offsets, {x:current.pos.x,y:current.pos.y-1,z:current.pos.z})){
current.pos.y--;
updateCurrent();
} else {
lockPiece();
}
}
renderer.render(scene,camera);
}
animate();
// ajustar viewport
window.addEventListener('resize',()=>{
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
});
</script>
</body>
</html>
Explicação rápida:
Usamos Three.js para desenhar cubos 3D.
O “tabuleiro” é um array 3D board[x][z][y].
Shapes são definidas por offsets 3D de cubos em relação a uma “âncora”.
Há controles básicos:
←→ (X), ↑↓ (Z) para mover no plano horizontal
Q / W / E para rotacionar (X, Y, Z)
Barra de espaço para queda rápida
Camadas cheias (planos no eixo Y) são zeradas e tudo acima desce.
Você pode estender adicionando novos tetracubos, melhorando a UI, adicionando pontuação, níveis de velocidade e efeitos visuais. Bom jogo! 🎮