<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Snake on a Cube Surface</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
background: #000;
}
#game {
position: absolute;
top: 50%;
left: 50%;
transform-style: preserve-3d;
transform-origin: center center;
width: 400px;
height: 400px;
margin: -200px 0 0 -200px;
}
.face {
position: absolute;
width: 200px;
height: 200px;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
transform-style: preserve-3d;
}
/* Cube faces */
#front { transform: rotateY(0deg) translateZ(100px); }
#back { transform: rotateY(180deg) translateZ(100px); }
#right { transform: rotateY(90deg) translateZ(100px); }
#left { transform: rotateY(-90deg) translateZ(100px); }
#top { transform: rotateX(90deg) translateZ(100px); }
#bottom { transform: rotateX(-90deg) translateZ(100px); }
.cell {
position: absolute;
width: 20px;
height: 20px;
box-sizing: border-box;
}
.snake {
background: lime;
}
.apple {
background: red;
}
</style>
</head>
<body>
<div id="game">
<div class="face" id="front"></div>
<div class="face" id="back"></div>
<div class="face" id="right"></div>
<div class="face" id="left"></div>
<div class="face" id="top"></div>
<div class="face" id="bottom"></div>
</div>
<script>
// Game variables
const FACE_NAMES = ["front", "right", "back", "left", "top", "bottom"];
const gridSize = 10;
let snake = [];
let direction = null;
let faces = {};
let apple = null;
let gameInterval = null;
let rotating = false;
const gameElement = document.getElementById('game');
// Initialize faces
FACE_NAMES.forEach(name => {
faces[name] = {
element: document.getElementById(name),
grid: []
};
for (let y = 0; y < gridSize; y++) {
faces[name].grid[y] = [];
for (let x = 0; x < gridSize; x++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.style.left = (x * 20) + 'px';
cell.style.top = (y * 20) + 'px';
faces[name].element.appendChild(cell);
faces[name].grid[y][x] = cell;
}
}
});
// Start or restart the game
function startGame() {
snake.forEach(part => updateCell(part, 'remove', 'snake'));
if (apple) updateCell(apple, 'remove', 'apple');
snake = [{ face: 'front', x: 5, y: 5 }];
direction = getRandomDirection();
placeApple();
updateCell(snake[0], 'add', 'snake');
if (gameInterval) clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, 200);
}
// Game loop
function gameLoop() {
const head = { ...snake[0] };
move(head);
if (checkCollision(head)) {
clearInterval(gameInterval);
alert('Game Over! Press SPACE to restart.');
return;
}
snake.unshift(head);
updateCell(head, 'add', 'snake');
if (head.face === apple.face && head.x === apple.x && head.y === apple.y) {
placeApple();
} else {
const tail = snake.pop();
updateCell(tail, 'remove', 'snake');
}
rotateCube(head.face);
}
// Move the snake's head
function move(part) {
switch (direction) {
case 'up': part.y--; break;
case 'down': part.y++; break;
case 'left': part.x--; break;
case 'right': part.x++; break;
}
// Check for wrap-around
if (part.x < 0 || part.x >= gridSize || part.y < 0 || part.y >= gridSize) {
wrapAround(part);
}
}
// Handle wrap-around between faces
function wrapAround(part) {
const transitions = {
'front': { 'top': 'top', 'bottom': 'bottom', 'left': 'left', 'right': 'right' },
'back': { 'top': 'top', 'bottom': 'bottom', 'left': 'right', 'right': 'left' },
'left': { 'top': 'top', 'bottom': 'bottom', 'left': 'back', 'right': 'front' },
'right': { 'top': 'top', 'bottom': 'bottom', 'left': 'front', 'right': 'back' },
'top': { 'up': 'back', 'down': 'front', 'left': 'left', 'right': 'right' },
'bottom': { 'up': 'front', 'down': 'back', 'left': 'left', 'right': 'right' },
};
let newFace = part.face;
if (part.x < 0) {
newFace = transitions[part.face]['left'];
part.x = gridSize - 1;
} else if (part.x >= gridSize) {
newFace = transitions[part.face]['right'];
part.x = 0;
} else if (part.y < 0) {
newFace = transitions[part.face] === 'top' ? 'top' : transitions[part.face]['up'];
part.y = gridSize - 1;
} else if (part.y >= gridSize) {
newFace = transitions[part.face] === 'bottom' ? 'bottom' : transitions[part.face]['down'];
part.y = 0;
}
part.face = newFace;
}
// Update cell appearance
function updateCell(part, action, className) {
const cell = faces[part.face].grid[part.y][part.x];
cell.classList[action](className);
}
// Check for collisions
function checkCollision(part) {
return snake.some(s => s.face === part.face && s.x === part.x && s.y === part.y);
}
// Place a new apple
function placeApple() {
if (apple) updateCell(apple, 'remove', 'apple');
let valid = false;
while (!valid) {
const face = FACE_NAMES[Math.floor(Math.random() * 6)];
const x = Math.floor(Math.random() * gridSize);
const y = Math.floor(Math.random() * gridSize);
if (!snake.some(s => s.face === face && s.x === x && s.y === y)) {
apple = { face, x, y };
valid = true;
}
}
updateCell(apple, 'add', 'apple');
}
// Rotate cube to face the snake's head
function rotateCube(face) {
if (rotating) return;
rotating = true;
let rotation = '';
switch (face) {
case 'front': rotation = 'rotateX(0deg) rotateY(0deg)'; break;
case 'back': rotation = 'rotateX(0deg) rotateY(180deg)'; break;
case 'left': rotation = 'rotateX(0deg) rotateY(-90deg)'; break;
case 'right': rotation = 'rotateX(0deg) rotateY(90deg)'; break;
case 'top': rotation = 'rotateX(-90deg) rotateY(0deg)'; break;
case 'bottom': rotation = 'rotateX(90deg) rotateY(0deg)'; break;
}
gameElement.style.transition = 'transform 0.5s';
gameElement.style.transform = rotation;
setTimeout(() => { rotating = false; }, 500);
}
// Get random initial direction
function getRandomDirection() {
const directions = ['up', 'down', 'left', 'right'];
return directions[Math.floor(Math.random() * directions.length)];
}
// Handle key presses
document.addEventListener('keydown', (e) => {
switch (e.key) {
case 'w': if (direction !== 'down') direction = 'up'; break;
case 's': if (direction !== 'up') direction = 'down'; break;
case 'a': if (direction !== 'right') direction = 'left'; break;
case 'd': if (direction !== 'left') direction = 'right'; break;
case ' ': startGame(); break;
}
});
</script>
</body>
</html>