HTML+CSS+JS俄罗斯方块小游戏

源码在效果图后面 五个按钮控制

效果图        

2dd7b7c590144ef48cc3107c4f0296f4.jpg

 源代码

<!DOCTYPE html>
<html lang="zh-CN">
<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;
            height: 100vh;
            font-family: 'Arial', sans-serif;
            background-color: #f0f0f0;
        }
        canvas {
            border: 2px solid #333;
            border-radius: 10px;
            box-shadow: 2px 2px 12px rgba(0, 0, 0, 0.5);
            background-color: #fff;
        }
        #controls {
            margin-left: 20px;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        button {
            padding: 10px 15px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
            box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <canvas id="board" width="300" height="600"></canvas>
    <div id="controls">
        <button id="left">左</button>
        <button id="right">右</button>
        <button id="down">下</button>
        <button id="rotate">旋转</button>
        <button id="start">开始</button>
    </div>

    <script>
        const canvas = document.getElementById('board');
        const ctx = canvas.getContext('2d');
        const cols = 10;
        const rows = 20;
        const blockSize = 30;

        const colors = [
            null,
            'cyan',
            'blue',
            'orange',
            'yellow',
            'green',
            'purple',
            'red',
        ];

        const pieces = [
            [[1, 1, 1, 1]], // I
            [[1, 1, 1], [0, 1, 0]], // T
            [[1, 1, 0], [0, 1, 1]], // Z
            [[0, 1, 1], [1, 1, 0]], // S
            [[1, 1], [1, 1]], // O
            [[1, 1, 1], [1, 0, 0]], // L
            [[1, 1, 1], [0, 0, 1]], // J
        ];

        let board = Array.from({ length: rows }, () => Array(cols).fill(0));
        let currentPiece;
        let currentPosition;
        let dropInterval;
        let gameOver = false;

        function drawBoard() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            for (let r = 0; r < rows; r++) {
                for (let c = 0; c < cols; c++) {
                    if (board[r][c]) {
                        ctx.fillStyle = colors[board[r][c]];
                        ctx.fillRect(c * blockSize, r * blockSize, blockSize, blockSize);
                        ctx.strokeStyle = '#333';
                        ctx.strokeRect(c * blockSize, r * blockSize, blockSize, blockSize);
                    }
                }
            }
            drawPiece();
        }

        function drawPiece() {
            ctx.fillStyle = colors[currentPiece.color];
            currentPiece.shape.forEach((row, r) => {
                row.forEach((value, c) => {
                    if (value) {
                        ctx.fillRect((currentPosition.x + c) * blockSize, (currentPosition.y + r) * blockSize, blockSize, blockSize);
                        ctx.strokeRect((currentPosition.x + c) * blockSize, (currentPosition.y + r) * blockSize, blockSize, blockSize);
                    }
                });
            });
        }

        function rotate(piece) {
            return piece[0].map((_, index) => piece.map(row => row[index])).reverse();
        }

        function collision(offsetX = 0, offsetY = 0, piece = currentPiece) {
            return piece.shape.some((row, r) => {
                return row.some((value, c) => {
                    if (value) {
                        const newX = currentPosition.x + c + offsetX;
                        const newY = currentPosition.y + r + offsetY;
                        return (
                            newX < 0 ||
                            newX >= cols ||
                            newY >= rows ||
                            (newY >= 0 && board[newY][newX] !== 0)
                        );
                    }
                    return false;
                });
            });
        }

        function merge() {
            currentPiece.shape.forEach((row, r) => {
                row.forEach((value, c) => {
                    if (value) {
                        board[currentPosition.y + r][currentPosition.x + c] = currentPiece.color;
                    }
                });
            });

            clearRows();
        }

        function clearRows() {
            for (let r = rows - 1; r >= 0; r--) {
                if (board[r].every(cell => cell !== 0)) {
                    board.splice(r, 1);
                    board.unshift(Array(cols).fill(0));
                }
            }
        }

        function spawnPiece() {
            const index = Math.floor(Math.random() * pieces.length);
            currentPiece = {
                shape: pieces[index],
                color: index + 1,
            };
            currentPosition = { x: Math.floor(cols / 2) - 1, y: 0 };
            if (collision(0, 0)) {
                gameOver = true;
            }
        }

        function drop() {
            if (!gameOver) {
                if (!collision(0, 1)) {
                    currentPosition.y++;
                } else {
                    merge();
                    spawnPiece();
                }
                drawBoard();
            } else {
                clearInterval(dropInterval);
                alert('游戏结束!');
            }
        }

        function startGame() {
            board = Array.from({ length: rows }, () => Array(cols).fill(0));
            currentPiece = null;
            currentPosition = null;
            gameOver = false;
            spawnPiece();
            drawBoard();
            dropInterval = setInterval(drop, 1000);
        }

        document.getElementById('left').addEventListener('click', () => {
            if (!collision(-1, 0)) currentPosition.x--;
            drawBoard();
        });

        document.getElementById('right').addEventListener('click', () => {
            if (!collision(1, 0)) currentPosition.x++;
            drawBoard();
        });

        document.getElementById('down').addEventListener('click', () => {
            drop();
        });

        document.getElementById('rotate').addEventListener('click', () => {
            const rotated = rotate(currentPiece.shape);
            if (!collision(0, 0, { shape: rotated, color: currentPiece.color })) {
                currentPiece.shape = rotated;
            }
            drawBoard();
        });

        document.getElementById('start').addEventListener('click', startGame);

        drawBoard();
    </script>
</body>
</html>

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

睿智的海鸥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值