Исправь код:

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Игра: ИИ Управляет Кубиком</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            height: 100vh;
            background-color: #282c34;
            margin: 0;
            color: white;
            font-family: Arial, sans-serif;
        }

        #gameArea {
            position: relative;
            width: 400px;
            height: 400px;
            border: 2px solid white;
            overflow: hidden;
        }

        #player {
            position: absolute;
            width: 30px;
            height: 30px;
            background-color: blue;
        }

        #star {
            position: absolute;
            width: 20px;
            height: 20px;
            background-color: gold;
            border-radius: 50%;
        }

        #score {
            margin-top: 20px;
            font-size: 20px;
        }

        #downloadBtn {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background-color: white;
            color: black;
            border: none;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div id="gameArea">
        <div id="player"></div>
        <div id="star"></div>
    </div>
    <div id="score">Счет: 0</div>
    <button id="downloadBtn">Скачать результаты</button>
    <script>
        let player = document.getElementById('player');
        let star = document.getElementById('star');
        let scoreDisplay = document.getElementById('score');
        let score = 0;

        let playerPosition = { x: 0, y: 0 };
        const startPosition = { x: 0, y: 0 };
        let moving = true;

        let logEntries = [];
        const boundaries = { left: false, right: false, top: false, bottom: false };

        function placeStar() {
            const x = Math.random() * (380);
            const y = Math.random() * (380);
            star.style.left = `${x}px`;
            star.style.top = `${y}px`;
        }

        function placePlayer(x, y) {
            playerPosition.x = Math.max(0, Math.min(x, 400 - 30));
            playerPosition.y = Math.max(0, Math.min(y, 400 - 30));
            player.style.left = `${playerPosition.x}px`;
            player.style.top = `${playerPosition.y}px`;
        }

        function checkCollision(targetRect) {
            const playerRect = player.getBoundingClientRect();
            return !(
                playerRect.top > targetRect.bottom ||
                playerRect.bottom < targetRect.top ||
                playerRect.left > targetRect.right ||
                playerRect.right < targetRect.left
            );
        }

        function moveAI() {
            const starRect = star.getBoundingClientRect();
            const targetX = starRect.left + starRect.width / 2;
            const targetY = starRect.top + starRect.height / 2;
            const speed = 5;

            const dx = targetX - playerPosition.x;
            const dy = targetY - playerPosition.y;
            const distance = Math.sqrt(dx * dx + dy * dy);

            if (distance > speed) {
                playerPosition.x += (dx / distance) * speed;
                playerPosition.y += (dy / distance) * speed;
            } else {
                playerPosition.x = targetX;
                playerPosition.y = targetY;
            }

            handleWallCollision();
            placePlayer(playerPosition.x, playerPosition.y);

            if (checkCollision(starRect)) {
                score++;
                scoreDisplay.textContent = `Счет: ${score}`;
                logEntries.push(`Собрана звезда в координатах (X: ${playerPosition.x}, Y: ${playerPosition.y})`);
                placeStar();
            }
        }

        function handleWallCollision() {
            const atLeftWall = playerPosition.x <= 0;
            const atRightWall = playerPosition.x >= 400 - 30;
            const atTopWall = playerPosition.y <= 0;
            const atBottomWall = playerPosition.y >= 400 - 30;

            if (atLeftWall) {
                playerPosition.x = 0;
                logEntries.push(`Отскок от левой стенки в (X: ${playerPosition.x}, Y: ${playerPosition.y})`);
            } else if (atRightWall) {
                playerPosition.x = 400 - 30;
                logEntries.push(`Отскок от правой стенки в (X: ${playerPosition.x}, Y: ${playerPosition.y})`);
            }

            if (atTopWall) {
                playerPosition.y = 0;
                logEntries.push(`Отскок от верхней стенки в (X: ${playerPosition.x}, Y: ${playerPosition.y})`);
            } else if (atBottomWall) {
                playerPosition.y = 400 - 30;
                logEntries.push(`Отскок от нижней стенки в (X: ${playerPosition.x}, Y: ${playerPosition.y})`);
            }
        }

        function gameLoop() {
            moveAI();
            requestAnimationFrame(gameLoop);
        }

        document.addEventListener('keydown', (event) => {
            if (event.key === 'r' || event.key === 'R') {
                playerPosition = { ...startPosition };
                placePlayer(playerPosition.x, playerPosition.y);
                logEntries.push(`Возвращение на начальную позицию (X: ${startPosition.x}, Y: ${startPosition.y})`);
            }
        });

        document.getElementById('downloadBtn').addEventListener('click', () => {
            const filename = `popitka_${score}.txt`;
            const content = logEntries.join('\n');
            const blob = new Blob([content], { type: 'text/plain' });
            const url = URL.createObjectURL(blob);

            const a = document.createElement('a');
            a.href = url;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        });

        placeStar();
        moving = true;
        gameLoop();
    </script>
</body>
</html>