吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 412|回复: 12
收起左侧

[学习记录] html版的“俄罗斯方块”游戏

[复制链接]
top7777 发表于 2025-5-6 11:14
本帖最后由 top7777 于 2025-5-6 13:17 编辑

bug已修改。用trae一键生成,用cursor优化形成:
[HTML] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html>
<head>
  <title>Tetris</title>
  <style>
    body {
      background: #202028;
      color: #fff;
      font-family: sans-serif;
      font-size: 2em;
      text-align: center;
    }
    canvas {
      border: 2px solid #fff;
      background: #000;
    }
    .container {
      display: flex;
      justify-content: center;
      gap: 20px;
      align-items: center;
      margin-top: 20px;
    }
    #score {
      margin-top: 1em;
    }
    .instructions {
      font-size: 0.6em;
      line-height: 1.5;
      text-align: left;
      min-width: 200px;
    }
  </style>
</head>
<body>
  <div id="score">0</div>
  <div class="container">
    <canvas id="tetris" width="240" height="400"></canvas>
    <div>
      <div>Next:</div>
      <canvas id="next" width="120" height="120"></canvas>
      <div class="instructions">
        <div>操作说明:</div>
        <ul>
          <li>← →方向键:左右移动</li>
          <li>↑键:旋转方块</li>
          <li>↓键:加速下落</li>
          <li>空格键:直接落下</li>
        </ul>
      </div>
    </div>
  </div>
 
  <script>
    const canvas = document.getElementById('tetris');
    const context = canvas.getContext('2d');
    const nextCanvas = document.getElementById('next');
    const nextContext = nextCanvas.getContext('2d');
    const scoreElement = document.getElementById('score');
 
    context.scale(20, 20);
    nextContext.scale(20, 20);
 
    const COLORS = [
      '#FF0D72', // I
      '#0DC2FF', // O
      '#0DFF72', // T
      '#F538FF', // L
      '#FF8E0D', // J
      '#FFE138', // S
      '#3877FF'  // Z
    ];
 
    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
    ];
 
    let dropCounter = 0;
    let dropInterval = 1000;
    let lastTime = 0;
    let score = 0;
    let gameOver = false;
 
    // 初始化矩阵
    const matrix = createMatrix(12, 20);
    const player = {
      pos: {x: 5, y: 0},
      matrix: null,
      nextMatrix: null,
      score: 0,
      color: null,
      nextColor: null
    };
 
    // 游戏逻辑初始化
    playerReset();
    updateScore();
 
    function update(time = 0) {
      if (gameOver) return;
       
      const deltaTime = time - lastTime;
      lastTime = time;
 
      dropCounter += deltaTime;
      if (dropCounter > dropInterval) {
        playerDrop();
      }
 
      draw();
      requestAnimationFrame(update);
    }
 
    function draw() {
      context.fillStyle = '#000';
      context.fillRect(0, 0, canvas.width, canvas.height);
 
      // 绘制已固定的方块
      matrix.forEach((row, y) => {
        row.forEach((value, x) => {
          if (value) {
            context.fillStyle = value;
            context.fillRect(x, y, 1, 1);
          }
        });
      });
 
      // 绘制当前方块
      drawMatrix(context, player.matrix, player.pos, player.color);
       
      // 绘制下一个方块
      nextContext.fillStyle = '#000';
      nextContext.fillRect(0, 0, nextCanvas.width, nextCanvas.height);
      drawMatrix(nextContext, player.nextMatrix, {x: 1, y: 1}, player.nextColor);
    }
 
    function drawMatrix(context, matrix, offset, color) {
      matrix.forEach((row, y) => {
        row.forEach((value, x) => {
          if (value) {
            context.fillStyle = color;
            context.fillRect(x + offset.x, y + offset.y, 1, 1);
          }
        });
      });
    }
 
    function collide(playerMatrix, pos) {
      for (let y = 0; y < playerMatrix.length; ++y) {
        for (let x = 0; x < playerMatrix[y].length; ++x) {
          if (playerMatrix[y][x] !== 0) {
            if (pos.y + y >= 20 || // 超出底部
                pos.x + x < 0 || // 超出左边界
                pos.x + x >= 12 || // 超出右边界
                matrix[pos.y + y][pos.x + x]) { // 与其他方块重叠
              return true;
            }
          }
        }
      }
      return false;
    }
 
    function playerRotate() {
      const matrix = player.matrix;
      const newMatrix = matrix[0].map((_, i) =>
        matrix.map(row => row[row.length - 1 - i])
      );
      if (!collide(newMatrix, player.pos)) {
        player.matrix = newMatrix;
      }
    }
 
    function playerMove(dir) {
      player.pos.x += dir;
      if (collide(player.matrix, player.pos)) {
        player.pos.x -= dir;
      }
    }
 
    function playerDrop() {
      player.pos.y++;
      if (collide(player.matrix, player.pos)) {
        player.pos.y--;
        merge();
        playerReset();
        sweep();
        updateScore();
      }
      dropCounter = 0;
    }
 
    function merge() {
      player.matrix.forEach((row, y) => {
        row.forEach((value, x) => {
          if (value) {
            matrix[y + player.pos.y][x + player.pos.x] = player.color;
          }
        });
      });
    }
 
    function sweep() {
      let rowCount = 0;
      outer: for (let y = matrix.length - 1; y >= 0; y--) {
        for (let x = 0; x < matrix[y].length; x++) {
          if (!matrix[y][x]) continue outer;
        }
        const row = matrix.splice(y, 1)[0];
        matrix.unshift(Array(12).fill(0));
        rowCount++;
        y++;
      }
      if (rowCount) {
        score += rowCount * 100;
      }
    }
 
    function updateScore() {
      scoreElement.textContent = score;
      dropInterval = Math.max(100, 1000 - (Math.floor(score / 1000) * 100));
    }
 
    function playerReset() {
      const pieces = 'ILJOTSZ';
      const index = pieces.length * Math.random() | 0;
      player.matrix = player.nextMatrix || createPiece(pieces[index]);
      player.color = player.nextColor || COLORS[index];
       
      const nextIndex = pieces.length * Math.random() | 0;
      player.nextMatrix = createPiece(pieces[nextIndex]);
      player.nextColor = COLORS[nextIndex];
       
      player.pos.y = 0;
      player.pos.x = (12 / 2 | 0) - (player.matrix[0].length / 2 | 0);
       
      if (collide(player.matrix, player.pos)) {
        gameOver = true;
        alert('游戏结束!得分:' + score);
        matrix.forEach(row => row.fill(0));
        score = 0;
        updateScore();
        gameOver = false;
      }
    }
 
    function createMatrix(w, h) {
      return Array(h).fill().map(() => Array(w).fill(0));
    }
 
    function createPiece(type) {
      const index = 'ILJOTSZ'.indexOf(type);
      return SHAPES[index];
    }
 
    // 初始化键盘控制
    document.addEventListener('keydown', event => {
      if (gameOver) return;
       
      if (event.keyCode === 37) {
        playerMove(-1);
      } else if (event.keyCode === 39) {
        playerMove(1);
      } else if (event.keyCode === 40) {
        playerDrop();
      } else if (event.keyCode === 38) {
        playerRotate();
      } else if (event.keyCode === 32) {
        while(!collide(player.matrix, {x: player.pos.x, y: player.pos.y + 1})) {
          player.pos.y++;
        }
        merge();
        playerReset();
        sweep();
        updateScore();
      }
    });
 
    update();
  </script>
</body>
</html>

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lengbingling 发表于 2025-5-6 11:26
经测试,试玩了一下,好像有bug.
laotzudao0 发表于 2025-5-6 11:41
xuzhihao 发表于 2025-5-6 11:42
冒个泡 发表于 2025-5-6 11:46
好久好久没玩过了,哈哈,下来试试
okmad 发表于 2025-5-6 11:47
不错不错,可以玩
冒个泡 发表于 2025-5-6 11:52
lengbingling 发表于 2025-5-6 11:26
经测试,试玩了一下,好像有bug.

确实有BUG,模型会被穿透
xl117 发表于 2025-5-6 12:35
不错不错,可以玩
fineetec 发表于 2025-5-6 12:37
可以无限叠加
 楼主| top7777 发表于 2025-5-6 13:18
冒个泡 发表于 2025-5-6 11:52
确实有BUG,模型会被穿透

bug已修改
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-20 17:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表