<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D Snake on a Cube</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            color: white;
            font-family: Arial, sans-serif;
        }
        .game-container {
            perspective: 1000px;
        }
        .cube {
            width: 200px;
            height: 200px;
            position: relative;
            transform-style: preserve-3d;
            transform: rotateX(-20deg) rotateY(20deg);
            animation: spin 30s infinite linear;
        }
        @keyframes spin {
            from { transform: rotateX(-20deg) rotateY(0deg); }
            to { transform: rotateX(-20deg) rotateY(360deg); }
        }
        .face {
            position: absolute;
            width: 200px;
            height: 200px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .face.front { transform: translateZ(100px); }
        .face.back { transform: rotateY(180deg) translateZ(100px); }
        .face.left { transform: rotateY(-90deg) translateZ(100px); }
        .face.right { transform: rotateY(90deg) translateZ(100px); }
        .face.top { transform: rotateX(90deg) translateZ(100px); }
        .face.bottom { transform: rotateX(-90deg) translateZ(100px); }

        .snake {
            position: absolute;
            width: 20px;
            height: 20px;
            background-color: lime;
        }
        .apple {
            position: absolute;
            width: 20px;
            height: 20px;
            background-color: red;
            border-radius: 50%;
        }

        .instructions {
            position: absolute;
            top: 20px;
            left: 20px;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <div class="game-container">
        <div class="cube">
            <div class="face front"></div>
            <div class="face back"></div>
            <div class="face left"></div>
            <div class="face right"></div>
            <div class="face top"></div>
            <div class="face bottom"></div>
        </div>
    </div>
    <div class="instructions">
        Press SPACE to start/restart the game<br>
        Use WASD to control the snake
    </div>

    <script>
        const cube = document.querySelector('.cube');
        const faces = document.querySelectorAll('.face');
        let snake = [{ x: 0, y: 0, z: 0 }];
        let direction = { x: 1, y: 0, z: 0 };
        let apple = { x: 0, y: 0, z: 0 };
        let interval;
        let isPlaying = false;

        function createSnakeElement() {
            const element = document.createElement('div');
            element.classList.add('snake');
            return element;
        }

        function createAppleElement() {
            const element = document.createElement('div');
            element.classList.add('apple');
            return element;
        }

        function placeSnake() {
            faces.forEach(face => face.innerHTML = ''); // Clear cube faces
            snake.forEach(segment => {
                const snakeElement = createSnakeElement();
                const face = faces[getFace(segment)];
                snakeElement.style.transform = `translate(${segment.x * 20}px, ${segment.y * 20}px)`;
                face.appendChild(snakeElement);
            });
        }

        function placeApple() {
            const appleElement = createAppleElement();
            const face = faces[getFace(apple)];
            appleElement.style.transform = `translate(${apple.x * 20}px, ${apple.y * 20}px)`;
            face.appendChild(appleElement);
        }

        function getFace(position) {
            if (position.z === 0) return 0; // front
            if (position.z === 1) return 1; // back
            if (position.x === -1) return 2; // left
            if (position.x === 1) return 3; // right
            if (position.y === 1) return 4; // top
            if (position.y === -1) return 5; // bottom
        }

        function moveSnake() {
            const newHead = { x: snake[0].x + direction.x, y: snake[0].y + direction.y, z: snake[0].z + direction.z };
            snake.unshift(newHead);
            snake.pop();
            placeSnake();
            placeApple();
        }

        function gameLoop() {
            moveSnake();
        }

        function startGame() {
            if (isPlaying) return;
            isPlaying = true;
            snake = [{ x: 0, y: 0, z: 0 }];
            direction = { x: 1, y: 0, z: 0 };
            apple = { x: Math.floor(Math.random() * 5), y: Math.floor(Math.random() * 5), z: Math.floor(Math.random() * 5) };
            placeSnake();
            placeApple();
            interval = setInterval(gameLoop, 300);
        }

        function changeDirection(event) {
            if (event.key === 'w') direction = { x: 0, y: -1, z: 0 };
            if (event.key === 's') direction = { x: 0, y: 1, z: 0 };
            if (event.key === 'a') direction = { x: -1, y: 0, z: 0 };
            if (event.key === 'd') direction = { x: 1, y: 0, z: 0 };
        }

        document.addEventListener('keydown', (event) => {
            if (event.code === 'Space') {
                startGame();
            } else {
                changeDirection(event);
            }
        });
    </script>
</body>
</html>