Skip to content
TheChatArchiveThe Chat Archive
DeepSeekdeepseek-r1-0528· DeepSeekHugging Face LMArena Human Preference 140K

我来帮你创建一个完整的俄罗斯方块游戏,所有代码都在一个HTML文件,界面美观

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o4-mini-2025-04-16’s answer.
  1. User

    我来帮你创建一个完整的俄罗斯方块游戏,所有代码都在一个HTML文件,界面美观

  2. DeepSeek Assistant

    俄罗斯方块游戏

    我将设计一个美观且功能完整的俄罗斯方块游戏,所有代码都在一个HTML文件中。

    设计思路

    • 创建现代简约的游戏界面
    • 实现核心游戏逻辑:方块生成、移动、旋转、消行
    • 添加分数系统和下一个方块预览
    • 设计响应式布局,适配不同设备
    • 添加游戏控制按钮和键盘操作支持

    下面是完整的实现代码:

    <!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>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            }
            
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 100vh;
                background: linear-gradient(135deg, #1a2a6c, #b21f1f, #1a2a6c);
                padding: 20px;
                color: white;
            }
            
            .game-container {
                display: flex;
                flex-direction: column;
                align-items: center;
                max-width: 800px;
                width: 100%;
                background: rgba(0, 0, 0, 0.7);
                border-radius: 20px;
                padding: 25px;
                box-shadow: 0 15px 30px rgba(0, 0, 0, 0.5);
                backdrop-filter: blur(10px);
                border: 1px solid rgba(255, 255, 255, 0.1);
            }
            
            .header {
                text-align: center;
                margin-bottom: 20px;
            }
            
            h1 {
                font-size: 2.8rem;
                margin-bottom: 10px;
                text-shadow: 0 0 10px #ff0055, 0 0 20px #ff0055;
                letter-spacing: 3px;
            }
            
            .game-area {
                display: flex;
                flex-wrap: wrap;
                justify-content: center;
                gap: 30px;
            }
            
            .game-board {
                border: 3px solid #3a3a3a;
                border-radius: 8px;
                overflow: hidden;
                box-shadow: 0 0 20px rgba(0, 0, 0, 0.6);
                background: rgba(20, 20, 20, 0.9);
            }
            
            .side-panel {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                width: 180px;
            }
            
            .panel-section {
                background: rgba(30, 30, 40, 0.8);
                border-radius: 12px;
                padding: 15px;
                margin-bottom: 20px;
                box-shadow: 0 5px 15px rgba(0, 0, 0, 0.4);
                border: 1px solid rgba(255, 255, 255, 0.1);
            }
            
            .panel-title {
                font-size: 1.2rem;
                margin-bottom: 12px;
                color: #ffcc00;
                text-align: center;
            }
            
            .score-display {
                font-size: 2rem;
                font-weight: bold;
                text-align: center;
                color: #4caf50;
                text-shadow: 0 0 8px rgba(76, 175, 80, 0.6);
            }
            
            .next-piece {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 120px;
                background: rgba(10, 10, 20, 0.7);
                border-radius: 8px;
            }
            
            .controls {
                display: grid;
                grid-template-columns: repeat(2, 1fr);
                gap: 12px;
            }
            
            button {
                padding: 12px 15px;
                border: none;
                border-radius: 8px;
                background: linear-gradient(to right, #ff416c, #ff4b2b);
                color: white;
                font-size: 1rem;
                font-weight: bold;
                cursor: pointer;
                transition: all 0.3s ease;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
            }
            
            button:hover {
                transform: translateY(-3px);
                box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
            }
            
            button:active {
                transform: translateY(1px);
            }
            
            .instructions {
                margin-top: 25px;
                background: rgba(30, 30, 40, 0.8);
                border-radius: 12px;
                padding: 20px;
                width: 100%;
            }
            
            .instructions h2 {
                color: #ffcc00;
                margin-bottom: 15px;
                text-align: center;
            }
            
            .key-list {
                display: grid;
                grid-template-columns: repeat(2, 1fr);
                gap: 15px;
            }
            
            .key-item {
                display: flex;
                align-items: center;
            }
            
            .key {
                display: inline-block;
                padding: 5px 10px;
                background: rgba(255, 255, 255, 0.15);
                border-radius: 5px;
                margin-right: 10px;
                min-width: 40px;
                text-align: center;
                font-weight: bold;
            }
            
            canvas {
                display: block;
            }
            
            .game-over {
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: rgba(0, 0, 0, 0.85);
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                z-index: 10;
                display: none;
            }
            
            .game-over h2 {
                font-size: 3rem;
                color: #ff416c;
                margin-bottom: 20px;
                text-shadow: 0 0 10px rgba(255, 65, 108, 0.7);
            }
            
            @media (max-width: 650px) {
                .game-area {
                    flex-direction: column;
                    align-items: center;
                }
                
                .side-panel {
                    width: 100%;
                    max-width: 400px;
                    flex-direction: row;
                    flex-wrap: wrap;
                }
                
                .panel-section {
                    width: calc(50% - 10px);
                }
                
                .controls {
                    width: 100%;
                }
                
                .key-list {
                    grid-template-columns: 1fr;
                }
            }
        </style>
    </head>
    <body>
        <div class="game-container">
            <div class="header">
                <h1>俄罗斯方块</h1>
                <p>经典的益智游戏,挑战你的反应速度!</p>
            </div>
            
            <div class="game-area">
                <div class="game-board">
                    <canvas id="tetris" width="300" height="600"></canvas>
                </div>
                
                <div class="side-panel">
                    <div class="panel-section">
                        <div class="panel-title">分数</div>
                        <div class="score-display" id="score">0</div>
                    </div>
                    
                    <div class="panel-section">
                        <div class="panel-title">下一个方块</div>
                        <div class="next-piece">
                            <canvas id="next-piece" width="120" height="120"></canvas>
                        </div>
                    </div>
                    
                    <div class="panel-section">
                        <div class="controls">
                            <button id="start-btn">开始游戏</button>
                            <button id="pause-btn">暂停</button>
                            <button id="reset-btn">重新开始</button>
                            <button id="rotate-btn">旋转</button>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="instructions">
                <h2>游戏操作</h2>
                <div class="key-list">
                    <div class="key-item"><span class="key">←</span> 左移方块</div>
                    <div class="key-item"><span class="key">→</span> 右移方块</div>
                    <div class="key-item"><span class="key">↓</span> 加速下落</div>
                    <div class="key-item"><span class="key">↑</span> 旋转方块</div>
                    <div class="key-item"><span class="key">P</span> 暂停游戏</div>
                    <div class="key-item"><span class="key">空格</span> 直接落到底部</div>
                </div>
            </div>
        </div>
        
        <div class="game-over" id="game-over">
            <h2>游戏结束!</h2>
            <p>最终得分: <span id="final-score">0</span></p>
            <button id="restart-btn">再玩一次</button>
        </div>
    
        <script>
            // 游戏常量
            const COLS = 10;
            const ROWS = 20;
            const BLOCK_SIZE = 30;
            const COLORS = [
                null,
                '#FF0D72', // I
                '#0DC2FF', // J
                '#0DFF72', // L
                '#F538FF', // O
                '#FF8E0D', // S
                '#FFE138', // T
                '#3877FF'  // Z
            ];
    
            // 方块形状定义
            const SHAPES = [
                [],
                [[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], // I
                [[0, 0, 0], [1, 1, 1], [0, 0, 1]], // J
                [[0, 0, 0], [1, 1, 1], [1, 0, 0]], // L
                [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], // O
                [[0, 0, 0], [0, 1, 1], [1, 1, 0]], // S
                [[0, 0, 0], [0, 1, 0], [1, 1, 1]], // T
                [[0, 0, 0], [1, 1, 0], [0, 1, 1]]  // Z
            ];
    
            // 获取DOM元素
            const canvas = document.getElementById('tetris');
            const nextCanvas = document.getElementById('next-piece');
            const scoreElement = document.getElementById('score');
            const finalScoreElement = document.getElementById('final-score');
            const gameOverScreen = document.getElementById('game-over');
            const ctx = canvas.getContext('2d');
            const nextCtx = nextCanvas.getContext('2d');
    
            // 游戏状态
            let board = createBoard();
            let score = 0;
            let gameOver = false;
            let gameActive = false;
            let dropCounter = 0;
            let dropInterval = 1000;
            let lastTime = 0;
            let player = {
                pos: {x: 0, y: 0},
                matrix: null,
                nextMatrix: null,
                score: 0
            };
    
            // 初始化游戏
            function init() {
                resetGame();
                drawNextCanvas();
                
                // 添加事件监听器
                document.addEventListener('keydown', handleKeyPress);
                document.getElementById('start-btn').addEventListener('click', startGame);
                document.getElementById('pause-btn').addEventListener('click', togglePause);
                document.getElementById('reset-btn').addEventListener('click', resetGame);
                document.getElementById('restart-btn').addEventListener('click', resetGame);
                document.getElementById('rotate-btn').addEventListener('click', rotatePlayer);
            }
    
            // 创建游戏板
            function createBoard() {
                return Array.from(Array(ROWS), () => Array(COLS).fill(0));
            }
    
            // 重置游戏
            function resetGame() {
                board = createBoard();
                score = 0;
                gameOver = false;
                gameActive = false;
                dropInterval = 1000;
                scoreElement.textContent = score;
                player.score = 0;
                gameOverScreen.style.display = 'none';
                resetPlayer();
                draw();
            }
    
            // 开始游戏
            function startGame() {
                if (gameOver) resetGame();
                gameActive = true;
                requestAnimationFrame(update);
            }
    
            // 暂停/继续游戏
            function togglePause() {
                gameActive = !gameActive;
                if (gameActive) {
                    requestAnimationFrame(update);
                }
            }
    
            // 重置玩家状态
            function resetPlayer() {
                player.nextMatrix = createPiece();
                player.matrix = player.nextMatrix;
                player.nextMatrix = createPiece();
                player.pos.y = 0;
                player.pos.x = Math.floor(COLS / 2) - Math.floor(player.matrix[0].length / 2);
                
                // 检查游戏是否结束
                if (collide()) {
                    gameOver = true;
                    gameActive = false;
                    gameOverScreen.style.display = 'flex';
                    finalScoreElement.textContent = player.score;
                }
                
                drawNextCanvas();
            }
    
            // 创建随机方块
            function createPiece() {
                const pieceId = Math.floor(Math.random() * 7) + 1;
                const piece = SHAPES[pieceId];
                return {
                    matrix: piece,
                    id: pieceId
                };
            }
    
            // 绘制游戏板
            function draw() {
                ctx.fillStyle = '#111';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                
                drawMatrix(board, {x: 0, y: 0});
                if (player.matrix) {
                    drawMatrix(player.matrix.matrix, player.pos, player.matrix.id);
                }
            }
    
            // 绘制下一个方块预览
            function drawNextCanvas() {
                nextCtx.fillStyle = 'rgba(10, 10, 20, 0.7)';
                nextCtx.fillRect(0, 0, nextCanvas.width, nextCanvas.height);
                
                if (player.nextMatrix) {
                    const matrix = player.nextMatrix.matrix;
                    const id = player.nextMatrix.id;
                    
                    // 居中绘制预览方块
                    const offsetX = (nextCanvas.width - matrix[0].length * BLOCK_SIZE) / 2;
                    const offsetY = (nextCanvas.height - matrix.length * BLOCK_SIZE) / 2;
                    
                    drawMatrixOnNext(matrix, {x: offsetX, y: offsetY}, id);
                }
            }
    
            // 绘制矩阵
            function drawMatrix(matrix, offset, id = 0) {
                matrix.forEach((row, y) => {
                    row.forEach((value, x) => {
                        if (value !== 0) {
                            ctx.fillStyle = COLORS[id];
                            ctx.fillRect((x + offset.x) * BLOCK_SIZE, 
                                        (y + offset.y) * BLOCK_SIZE, 
                                        BLOCK_SIZE - 1, 
                                        BLOCK_SIZE - 1);
                            
                            // 添加方块内高光效果
                            ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
                            ctx.fillRect((x + offset.x) * BLOCK_SIZE, 
                                        (y + offset.y) * BLOCK_SIZE, 
                                        BLOCK_SIZE - 1, 
                                        2);
                                        
                            ctx.fillRect((x + offset.x) * BLOCK_SIZE, 
                                        (y + offset.y) * BLOCK_SIZE, 
                                        2, 
                                        BLOCK_SIZE - 1);
                        }
                    });
                });
            }
    
            // 在预览画布上绘制矩阵
            function drawMatrixOnNext(matrix, offset, id = 0) {
                matrix.forEach((row, y) => {
                    row.forEach((value, x) => {
                        if (value !== 0) {
                            nextCtx.fillStyle = COLORS[id];
                            nextCtx.fillRect(offset.x + x * BLOCK_SIZE, 
                                             offset.y + y * BLOCK_SIZE, 
                                             BLOCK_SIZE - 1, 
                                             BLOCK_SIZE - 1);
                            
                            // 添加方块内高光效果
                            nextCtx.fillStyle = 'rgba(255, 255, 255, 0.2)';
                            nextCtx.fillRect(offset.x + x * BLOCK_SIZE, 
                                           offset.y + y * BLOCK_SIZE, 
                                           BLOCK_SIZE - 1, 
                                           2);
                                        
                            nextCtx.fillRect(offset.x + x * BLOCK_SIZE, 
                                           offset.y + y * BLOCK_SIZE, 
                                           2, 
                                           BLOCK_SIZE - 1);
                        }
                    });
                });
            }
    
            // 碰撞检测
            function collide() {
                const [m, o] = [player.matrix.matrix, player.pos];
                for (let y = 0; y < m.length; ++y) {
                    for (let x = 0; x < m[y].length; ++x) {
                        if (m[y][x] !== 0 &&
                           (board[y + o.y] && 
                            board[y + o.y][x + o.x]) !== 0) {
                            return true;
                        }
                    }
                }
                return false;
            }
    
            // 合并方块到游戏板
            function merge() {
                player.matrix.matrix.forEach((row, y) => {
                    row.forEach((value, x) => {
                        if (value !== 0) {
                            board[y + player.pos.y][x + player.pos.x] = player.matrix.id;
                        }
                    });
                });
            }
    
            // 旋转玩家方块
            function rotatePlayer() {
                if (!gameActive || gameOver) return;
                
                const pos = player.pos.x;
                let offset = 1;
                rotate(player.matrix.matrix);
                
                while (collide()) {
                    player.pos.x += offset;
                    offset = -(offset + (offset > 0 ? 1 : -1));
                    if (offset > player.matrix.matrix[0].length) {
                        rotate(player.matrix.matrix);
                        player.pos.x = pos;
                        return;
                    }
                }
                draw();
            }
    
            // 旋转矩阵
            function rotate(matrix) {
                for (let y = 0; y < matrix.length; ++y) {
                    for (let x = 0; x < y; ++x) {
                        [matrix[x][y], matrix[y][x]] = [matrix[y][x], matrix[x][y]];
                    }
                }
                
                matrix.forEach(row => row.reverse());
            }
    
            // 玩家移动
            function playerMove(dir) {
                if (!gameActive || gameOver) return;
                
                player.pos.x += dir;
                if (collide()) {
                    player.pos.x -= dir;
                }
                draw();
            }
    
            // 玩家下落
            function playerDrop() {
                if (!gameActive || gameOver) return;
                
                player.pos.y++;
                if (collide()) {
                    player.pos.y--;
                    merge();
                    resetPlayer();
                    sweep();
                    updateScore();
                }
                dropCounter = 0;
                draw();
            }
    
            // 玩家快速下落
            function playerDropToBottom() {
                if (!gameActive || gameOver) return;
                
                while (!collide()) {
                    player.pos.y++;
                }
                player.pos.y--;
                playerDrop();
            }
    
            // 清除完整的行
            function sweep() {
                let rowCount = 0;
                outer: for (let y = board.length - 1; y > 0; --y) {
                    for (let x = 0; x < board[y].length; ++x) {
                        if (board[y][x] === 0) {
                            continue outer;
                        }
                    }
                    
                    // 移除该行并在顶部添加新行
                    const row = board.splice(y, 1)[0].fill(0);
                    board.unshift(row);
                    ++y;
                    rowCount++;
                }
                
                // 更新分数
                if (rowCount > 0) {
                    player.score += rowCount * 100 * rowCount;
                }
            }
    
            // 更新分数显示
            function updateScore() {
                scoreElement.textContent = player.score;
            }
    
            // 游戏更新循环
            function update(time = 0) {
                const deltaTime = time - lastTime;
                lastTime = time;
                
                dropCounter += deltaTime;
                if (dropCounter > dropInterval) {
                    playerDrop();
                }
                
                draw();
                
                if (gameActive && !gameOver) {
                    requestAnimationFrame(update);
                }
            }
    
            // 处理键盘事件
            function handleKeyPress(event) {
                if (!gameActive && event.key !== 'p') return;
                
                switch(event.key) {
                    case 'ArrowLeft':
                        playerMove(-1);
                        break;
                    case 'ArrowRight':
                        playerMove(1);
                        break;
                    case 'ArrowDown':
                        playerDrop();
                        break;
                    case 'ArrowUp':
                        rotatePlayer();
                        break;
                    case ' ':
                        playerDropToBottom();
                        break;
                    case 'p':
                        togglePause();
                        break;
                }
            }
    
            // 初始化游戏
            window.onload = init;
        </script>
    </body>
    </html>
    

    游戏功能说明

    这个俄罗斯方块游戏包含以下功能:

    1. 核心游戏机制:

      • 七种不同形状的方块随机生成
      • 方块旋转、左右移动和加速下落
      • 行消除检测和计分
      • 游戏结束判定
    2. 用户界面:

      • 游戏主区域显示当前方块和游戏板
      • 侧边面板显示分数和下一个方块预览
      • 游戏控制按钮(开始、暂停、重置、旋转)
      • 游戏结束画面
    3. 操作方式:

      • 键盘方向键控制方块移动和旋转
      • 空格键快速下落
      • P键暂停游戏
      • 按钮控制游戏流程
    4. 视觉效果:

      • 渐变背景和半透明游戏面板
      • 方块高光效果增强视觉体验
      • 响应式设计适配不同屏幕尺寸
      • 游戏结束动画效果

    游戏完全在浏览器中运行,所有代码都在单个HTML文件中,无需任何外部依赖。复制代码到HTML文件即可直接运行游戏。

    尽情享受这个经典的俄罗斯方块游戏吧!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.