试用gork3一键生成俄罗斯方块html5可直接运行代码


试用了x软件里的gork3,感觉还行,没有进行任何修改,在手机上直接粘贴到html里运行了。

玩着没bug,可以的

附上代码

手机俄罗斯方块 body { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; background-color: #f0f0f0; overflow: hidden; } #gameContainer { position: relative; } canvas { border: 2px solid #333; background-color: #fff; } .controls { display: flex; justify-content: space-around; width: 100%; max-width: 300px; margin-top: 20px; } button { padding: 15px 25px; font-size: 18px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; touch-action: manipulation; } button:active { background-color: #45a049; } #score { font-size: 20px; margin-top: 10px; }
得分: 0
左 右 旋转 下
const canvas = document.getElementById('tetris'); const context = canvas.getContext('2d'); const scoreDisplay = document.getElementById('score'); const grid = 24; // 每个格子大小 const cols = 10; // 列数 const rows = 20; // 行数
    // 游戏区域
    let board = Array.from({ length: rows }, () => Array(cols).fill(0));
    let currentPiece; // 当前方块
    let currentX; // 当前方块x坐标
    let currentY; // 当前方块y坐标
    let score = 0; // 得分
    let gameOver = false; // 游戏结束状态
    let dropCounter = 0; // 下落计数器
    let dropInterval = 1000; // 下落间隔(毫秒)
    let lastTime = 0; // 上次更新时间

    // 方块形状
    const shapes = [
        [[1,1,1,1]], // I
        [[1,1],[1,1]], // O
        [[1,1,1],[0,1,0]], // T
        [[1,1,1],[1,0,0]], // L
        [[1,1,1],[0,0,1]], // J
        [[1,1,0],[0,1,1]], // S
        [[0,1,1],[1,1,0]] // Z
    ];

    // 方块颜色
    const colors = [
        'cyan', 'yellow', 'purple', 'orange', 'blue', 'green', 'red'
    ];

    // 方块类
    class Piece {
        constructor(shape, color) {
            this.shape = shape;
            this.color = color;
        }

        rotate() {
            const rows = this.shape.length;
            const cols = this.shape[0].length;
            const newShape = Array.from({ length: cols }, () => Array(rows).fill(0));
            for (let y = 0; y < rows; y++) {
                for (let x = 0; x < cols; x++) {
                    newShape[x][rows - 1 - y] = this.shape[y][x];
                }
            }
            return newShape;
        }
    }

    // 生成新方块
    function newPiece() {
        const shapeIndex = Math.floor(Math.random() * shapes.length);
        currentPiece = new Piece(shapes[shapeIndex], colors[shapeIndex]);
        currentX = Math.floor(cols / 2) - Math.floor(currentPiece.shape[0].length / 2);
        currentY = 0;
        if (!canMove(0, 0)) {
            gameOver = true;
            alert('游戏结束!您的得分是:' + score);
            board = Array.from({ length: rows }, () => Array(cols).fill(0));
            score = 0;
            gameOver = false;
            newPiece();
        }
    }

    // 碰撞检测
    function canMove(dx, dy, newShape = currentPiece.shape) {
        for (let y = 0; y < newShape.length; y++) {
            for (let x = 0; x < newShape[y].length; x++) {
                if (newShape[y][x]) {
                    const newX = currentX + x + dx;
                    const newY = currentY + y + dy;
                    if (newX < 0 || newX >= cols || newY >= rows || (newY >= 0 && board[newY][newX])) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    // 移动方块
    function move(dx, dy) {
        if (canMove(dx, dy)) {
            currentX += dx;
            currentY += dy;
            return true;
        }
        return false;
    }

    // 旋转方块
    function rotatePiece() {
        const newShape = currentPiece.rotate();
        if (canMove(0, 0, newShape)) {
            currentPiece.shape = newShape;
        }
    }

    // 固定方块到游戏区域
    function merge() {
        for (let y = 0; y < currentPiece.shape.length; y++) {
            for (let x = 0; x < currentPiece.shape[y].length; x++) {
                if (currentPiece.shape[y][x]) {
                    board[currentY + y][currentX + x] = colors.indexOf(currentPiece.color) + 1;
                }
            }
        }
    }

    // 消行
    function clearLines() {
        let linesCleared = 0;
        for (let y = rows - 1; y >= 0; y--) {
            if (board[y].every(cell => cell !== 0)) {
                board.splice(y, 1);
                board.unshift(Array(cols).fill(0));
                linesCleared++;
                y++;
            }
        }
        if (linesCleared > 0) {
            score += linesCleared * 100;
            scoreDisplay.textContent = '得分: ' + score;
        }
    }

    // 绘制游戏
    function draw() {
        context.clearRect(0, 0, canvas.width, canvas.height);
        // 绘制游戏区域
        for (let y = 0; y < rows; y++) {
            for (let x = 0; x < cols; x++) {
                if (board[y][x]) {
                    context.fillStyle = colors[board[y][x] - 1];
                    context.fillRect(x * grid, y * grid, grid - 1, grid - 1);
                }
            }
        }
        // 绘制当前方块
        context.fillStyle = currentPiece.color;
        for (let y = 0; y < currentPiece.shape.length; y++) {
            for (let x = 0; x < currentPiece.shape[y].length; x++) {
                if (currentPiece.shape[y][x]) {
                    context.fillRect((currentX + x) * grid, (currentY + y) * grid, grid - 1, grid - 1);
                }
            }
        }
    }

    // 游戏循环
    function update(time = 0) {
        if (gameOver) return;
        const deltaTime = time - lastTime;
        lastTime = time;
        dropCounter += deltaTime;
        if (dropCounter > dropInterval) {
            if (!move(0, 1)) {
                merge();
                clearLines();
                newPiece();
            }
            dropCounter = 0;
        }
        draw();
        requestAnimationFrame(update);
    }

    // 触摸控制
    let moveInterval;
    let downInterval;

    function startMove(dx) {
        move(dx, 0);
        moveInterval = setInterval(() => move(dx, 0), 100);
    }

    function stopMove() {
        clearInterval(moveInterval);
    }

    function startDown() {
        move(0, 1);
        downInterval = setInterval(() => move(0, 1), 100);
    }

    function stopDown() {
        clearInterval(downInterval);
    }

    function moveLeft() { startMove(-1); }
    function moveRight() { startMove(1); }
    function rotate() { rotatePiece(); }
    function moveDown() { startDown(); }

    // 初始化游戏
    newPiece();
    update();
</script>
手机怎么插入代码,也没找到,就这样吧,开始午休了

还不错呀

保存html文件到本地直接浏览器打开不就可以了

此话题已在最后回复的 30 天后被自动关闭。不再允许新回复。