<!DOCTYPE html>
<html>
<head>
    <title>俄罗斯方块</title>
    <style>
        #game-board {
            border: 1px solid black;
            width: 200px;
            height: 400px;
            position: relative;
        }
        .block {
            width: 20px;
            height: 20px;
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="game-board"></div>
<pre><code>&lt;script&gt;
    const boardWidth = 10;
    const boardHeight = 20;
    const blockSize = 20;
    const shapes = [
        [[1, 1, 1, 1]],
        [[1, 1], [1, 1]],
        [[1, 1, 0], [0, 1, 1]],
        [[0, 1, 1], [1, 1, 0]],
        [[1, 1, 1], [1, 0, 0]],
        [[1, 1, 1], [0, 0, 1]],
        [[0, 1, 0], [1, 1, 1]]
    ];

    let board = [];
    let currentShape = null;
    let currentShapeX = 0;
    let currentShapeY = 0;

    function createBoard() {
        for (let row = 0; row &lt; boardHeight; row++) {
            board[row] = [];
            for (let col = 0; col &lt; boardWidth; col++) {
                board[row][col] = 0;
            }
        }
    }

    function createShape() {
        const randomIndex = Math.floor(Math.random() * shapes.length);
        currentShape = shapes[randomIndex];
        currentShapeX = Math.floor((boardWidth - currentShape[0].length) / 2);
        currentShapeY = 0;
    }

    function drawBoard() {
        const gameBoard = document.getElementById(&quot;game-board&quot;);
        gameBoard.innerHTML = &quot;&quot;;

        for (let row = 0; row &lt; boardHeight; row++) {
            for (let col = 0; col &lt; boardWidth; col++) {
                if (board[row][col] === 1) {
                    const block = document.createElement(&quot;div&quot;);
                    block.className = &quot;block&quot;;
                    block.style.left = col * blockSize + &quot;px&quot;;
                    block.style.top = row * blockSize + &quot;px&quot;;
                    gameBoard.appendChild(block);
                }
            }
        }
    }

    function drawCurrentShape() {
        for (let row = 0; row &lt; currentShape.length; row++) {
            for (let col = 0; col &lt; currentShape[row].length; col++) {
                if (currentShape[row][col] === 1) {
                    const block = document.createElement(&quot;div&quot;);
                    block.className = &quot;block&quot;;
                    block.style.left = (currentShapeX + col) * blockSize + &quot;px&quot;;
                    block.style.top = (currentShapeY + row) * blockSize + &quot;px&quot;;
                    document.getElementById(&quot;game-board&quot;).appendChild(block);
                }
            }
        }
    }

    function checkCollision() {
        for (let row = 0; row &lt; currentShape.length; row++) {
            for (let col = 0; col &lt; currentShape[row].length; col++) {
                if (currentShape[row][col] === 1) {
                    const newX = currentShapeX + col;
                    const newY = currentShapeY + row;

                    if (newX &lt; 0 || newX &gt;= boardWidth || newY &gt;= boardHeight || board[newY][newX] === 1) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    function mergeShape() {
        for (let row = 0; row &lt; currentShape.length; row++) {
            for (let col = 0; col &lt; currentShape[row].length; col++) {
                if (currentShape[row][col] === 1) {
                    const boardX = currentShapeX + col;
                    const boardY = currentShapeY + row;
                    board[boardY][boardX] = 1;
                }
            }
        }
    }

    function removeCompletedRows() {
        for (let row = boardHeight - 1; row &gt;= 0; row--) {
            const isRowCompleted = board[row].every(cell =&gt; cell === 1);

            if (isRowCompleted) {
                board.splice(row, 1);
                board.unshift(new Array(boardWidth).fill(0));
            }
        }
    }

    function update() {
        currentShapeY++;

        if (checkCollision()) {
            currentShapeY--;
            mergeShape();
            removeCompletedRows();
            createShape();

            if (checkCollision()) {
                alert(&quot;Game Over!&quot;);
                createBoard();
            }
        }

        drawBoard();
        drawCurrentShape();
    }

    function handleKeyPress(event) {
        switch (event.keyCode) {
            case 37: // Left arrow
                currentShapeX--;
                if (checkCollision()) {
                    currentShapeX++;
                }
                break;
            case 39: // Right arrow
                currentShapeX++;
                if (checkCollision()) {
                    currentShapeX--;
                }
                break;
            case 40: // Down arrow
                currentShapeY++;
                if (checkCollision()) {
                    currentShapeY--;
                }
                break;
        }
    }

    createBoard();
    createShape();
    setInterval(update, 1000);
    document.addEventListener(&quot;keydown&quot;, handleKeyPress);
&lt;/script&gt;
</code></pre>
</body>
</html>

原文地址: https://www.cveoy.top/t/topic/pxUi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录