<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>3D Червяк</title>
    <style>
        body { margin: 0; overflow: hidden; }
        #menu {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.8);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 10;
        }
        #menu h1 { margin-bottom: 20px; }
        #menu label { margin-bottom: 10px; }
        #menu button { padding: 10px 20px; font-size: 16px; }
        #score {
            position: absolute;
            top: 10px;
            left: 10px;
            color: white;
            font-size: 24px;
            z-index: 5;
        }
    </style>
</head>
<body>
    <div id="menu">
        <h1>Выберите настройки червяка</h1>
        <label>
            Выберите тип червяка:
            <select id="snakeType">
                <option value="default">Обычный червяк</option>
                <option value="textured">Червяк с текстурами</option>
            </select>
        </label>
        <div id="colorPicker">
            <label>
                Выберите цвет червяка:
                <input type="color" id="snakeColor" value="#FFFF00">
            </label>
        </div>
        <div id="textureUploader" style="display: none;">
            <label>
                Загрузите изображение для текстуры:
                <input type="file" id="textureFile" accept="image/png, image/jpeg">
            </label>
        </div>
        <button id="startButton">Начать игру</button>
    </div>

    <div id="score">Счет: 0</div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
    <script>
        let scene, camera, renderer;
        let wormMesh, pathPoints = [];
        let direction = new THREE.Vector3(0, 0, -1);
        let speed = 0.1;
        let food;
        let keyStates = {};
        let maxPoints = 10; // Червяк начнет с 10 сегментов
        const initialRadius = 0.5; // Размер червяка
        const radialSegments = 8;
        const closed = false;
        let score = 0;
        let snake, textureGlobal, snakeColorGlobal = "#FFFF00", snakeTypeGlobal = "default";
        let tubularSegments = 200; // Количество сегментов трубки

        document.getElementById('snakeType').addEventListener('change', function() {
            snakeTypeGlobal = this.value;
            document.getElementById('textureUploader').style.display = snakeTypeGlobal === 'textured' ? 'block' : 'none';
            document.getElementById('colorPicker').style.display = snakeTypeGlobal === 'default' ? 'block' : 'none';
        });

        document.getElementById('startButton').addEventListener('click', function() {
            snakeColorGlobal = document.getElementById('snakeColor').value;
            const textureFile = document.getElementById('textureFile').files[0];
            document.getElementById('menu').style.display = 'none';
            init(snakeTypeGlobal, snakeColorGlobal, textureFile);
        });

function init(snakeType, snakeColor, textureFile) {
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0x87CEEB);

    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.set(0, 5, 20); // Увеличьте расстояние по оси Z

    renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    const light = new THREE.AmbientLight(0xffffff, 0.5);
    scene.add(light);

    const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
    directionalLight.position.set(0, 10, 0);
    scene.add(directionalLight);

    const groundGeometry = new THREE.PlaneGeometry(100, 100);
    const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x008000 });
    const ground = new THREE.Mesh(groundGeometry, groundMaterial);
    ground.rotation.x = -Math.PI / 2;
    scene.add(ground);

    if (snakeType === 'textured' && textureFile) {
        const reader = new FileReader();
        reader.onload = function(event) {
            const textureLoader = new THREE.TextureLoader();
            textureLoader.load(event.target.result, function(texture) {
                textureGlobal = texture;
                createSnake();
                createFood();
                animate();
            });
        };
        reader.readAsDataURL(textureFile);
    } else {
        createSnake();
        createFood();
        animate();
    }

    document.addEventListener('keydown', onKeyDown);
    document.addEventListener('keyup', onKeyUp);
    window.addEventListener('resize', onWindowResize);
}


        function createSnake() {
            pathPoints = [];
            const initialPosition = new THREE.Vector3(0, 0.5, 0);

            for (let i = 0; i < maxPoints; i++) {
                const point = initialPosition.clone().add(direction.clone().multiplyScalar(i * speed * 5));
                pathPoints.push(point);
            }

            updateWormMesh();

            snake = { position: initialPosition.clone() };
        }

        function updateWormMesh() {
            const wormCurve = new THREE.CatmullRomCurve3(pathPoints);
            const geometry = new THREE.TubeGeometry(
                wormCurve,
                tubularSegments,
                initialRadius,
                radialSegments,
                closed
            );

            let material;

            if (snakeTypeGlobal === 'textured' && textureGlobal) {
                material = new THREE.MeshStandardMaterial({ map: textureGlobal });
            } else {
                if (snakeColorGlobal && typeof snakeColorGlobal === 'string') {
                    material = new THREE.MeshStandardMaterial({ color: new THREE.Color(snakeColorGlobal) });
                } else {
                    material = new THREE.MeshStandardMaterial({ color: 0xFFFF00 });
                }
            }

            if (wormMesh) {
                scene.remove(wormMesh);
                wormMesh.geometry.dispose();
                wormMesh.material.dispose();
            }

            wormMesh = new THREE.Mesh(geometry, material);
            scene.add(wormMesh);
        }

        function createFood() {
            const geometry = new THREE.SphereGeometry(0.5, 16, 16);
            const material = new THREE.MeshStandardMaterial({ color: 0xff0000 });
            food = new THREE.Mesh(geometry, material);
            food.position.set(
                Math.floor(Math.random() * 20 - 10),
                0.5,
                Math.floor(Math.random() * 20 - 10)
            );
            scene.add(food);
        }

        function onKeyDown(event) { keyStates[event.code] = true; }
        function onKeyUp(event) { keyStates[event.code] = false; }

        function updateDirection() {
            const turnSpeed = 0.05;
            if (keyStates['KeyA']) direction.applyAxisAngle(new THREE.Vector3(0, 1, 0), turnSpeed);
            if (keyStates['KeyD']) direction.applyAxisAngle(new THREE.Vector3(0, 1, 0), -turnSpeed);
            direction.normalize();
        }

        function animate() {
            requestAnimationFrame(animate);

            updateDirection();

            let currentSpeed = speed;
            if (keyStates['KeyW']) currentSpeed = speed * 1.5;
            if (keyStates['KeyS']) currentSpeed = speed * 0.5;

            const newPosition = snake.position.clone().add(direction.clone().multiplyScalar(currentSpeed));
            newPosition.y = 0.5;
            snake.position.copy(newPosition);

            pathPoints.push(snake.position.clone());

            if (pathPoints.length > maxPoints) {
                pathPoints.shift();
            }

            updateWormMesh();

            if (snake.position.distanceTo(food.position) < 1) {
                scene.remove(food);
                createFood();
                maxPoints += 5;
                score += 10;
                document.getElementById('score').textContent = `Счет: ${score}`;
            }

            const cameraOffset = direction.clone().multiplyScalar(-10).add(new THREE.Vector3(0, 5, 0));
            camera.position.lerp(snake.position.clone().add(cameraOffset), 0.1);
            camera.lookAt(snake.position);

            renderer.render(scene, camera);
        }

        function onWindowResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        }
    </script>
</body>
</html>
исправь код нужно исправить червяка переработай его всю логику и перепиши исправленный код