File size: 6,370 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dark Tetris</title>
<style>
  body { font-family: system-ui, sans-serif; background: #050505; color: #e0e0e0; text-align: center; margin: 0; padding: 16px; }
  h1 { color: #bb86fc; text-shadow: 0 0 8px #bb86fc; }
  #hud { margin: 8px; font-size: 16px; }
  #tetris { background: #0d0d0d; border: 2px solid #bb86fc; display: block; margin: 12px auto;
            box-shadow: 0 0 20px rgba(187,134,252,0.5); }
  p { color: #888; }
  button { padding: 8px 16px; font-size: 15px; border: none; border-radius: 6px;
           background: #bb86fc; color: #050505; cursor: pointer; }
</style>
</head>
<body>
  <h1>Dark Tetris</h1>
  <div id="hud">Score: <span id="score">0</span> · Lines: <span id="lines">0</span></div>
  <canvas id="tetris" width="220" height="484"></canvas>
  <p>← → Move, ↑ Rotate, ↓ Soft drop (dark theme)</p>
  <button id="restart">Restart</button>

<script>
  const COLS = 10, ROWS = 22, CELL = 22; // dark-themed tetris grid
  const canvas = document.getElementById('tetris');
  const ctx = canvas.getContext('2d');
  const scoreEl = document.getElementById('score');
  const linesEl = document.getElementById('lines');

  const SHAPES = {
    I: [[1, 1, 1, 1]],
    O: [[1, 1], [1, 1]],
    T: [[0, 1, 0], [1, 1, 1]],
    L: [[1, 0], [1, 0], [1, 1]],
    J: [[0, 1], [0, 1], [1, 1]],
    S: [[0, 1, 1], [1, 1, 0]],
    Z: [[1, 1, 0], [0, 1, 1]],
  };
  const COLORS = ['#bb86fc', '#03dac6', '#cf6679', '#ffb74d', '#4fc3f7', '#aed581', '#ff8a65'];
  const KEYS = Object.keys(SHAPES);

  let grid, piece, score, lines, gameOver, stepRemainder;
  let deterministicMode = false;

  function emptyGrid() {
    return Array.from({ length: ROWS }, () => Array(COLS).fill(0));
  }

  function newPiece() {
    const k = KEYS[Math.floor(Math.random() * KEYS.length)];
    const shape = SHAPES[k];
    return {
      shape,
      color: COLORS[KEYS.indexOf(k)],
      x: Math.floor((COLS - shape[0].length) / 2),
      y: 0,
    };
  }

  // rotate the current block clockwise
  function rotate(shape) {
    return shape[0].map((_, i) => shape.map(row => row[i]).reverse());
  }

  function collides(shape, ox, oy) {
    for (let r = 0; r < shape.length; r++) {
      for (let c = 0; c < shape[r].length; c++) {
        if (!shape[r][c]) continue;
        const nx = ox + c, ny = oy + r;
        if (nx < 0 || nx >= COLS || ny >= ROWS) return true;
        if (ny >= 0 && grid[ny][nx]) return true;
      }
    }
    return false;
  }

  function merge() {
    piece.shape.forEach((row, r) => row.forEach((v, c) => {
      if (v && piece.y + r >= 0) grid[piece.y + r][piece.x + c] = piece.color;
    }));
  }

  function clearLines() {
    let cleared = 0;
    for (let r = ROWS - 1; r >= 0; r--) {
      if (grid[r].every(v => v)) {
        grid.splice(r, 1);
        grid.unshift(Array(COLS).fill(0));
        cleared++;
        r++;
      }
    }
    if (cleared) {
      lines += cleared;
      score += [0, 100, 300, 500, 800][cleared];
      scoreEl.textContent = score;
      linesEl.textContent = lines;
    }
  }

  function drop() {
    if (gameOver) return;
    if (!collides(piece.shape, piece.x, piece.y + 1)) {
      piece.y++;
    } else {
      merge();
      clearLines();
      piece = newPiece();
      if (collides(piece.shape, piece.x, piece.y)) gameOver = true;
    }
  }

  function draw() {
    ctx.fillStyle = '#0d0d0d';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    for (let r = 0; r < ROWS; r++) {
      for (let c = 0; c < COLS; c++) {
        if (grid[r][c]) drawCell(c, r, typeof grid[r][c] === 'string' ? grid[r][c] : '#bb86fc');
      }
    }
    if (piece) {
      piece.shape.forEach((row, r) => row.forEach((v, c) => {
        if (v) drawCell(piece.x + c, piece.y + r, piece.color);
      }));
    }
    if (gameOver) {
      ctx.fillStyle = '#fff';
      ctx.font = '22px sans-serif';
      ctx.fillText('Game Over', 70, canvas.height / 2);
    }
  }

  function drawCell(x, y, color) {
    ctx.fillStyle = color;
    ctx.fillRect(x * CELL + 1, y * CELL + 1, CELL - 2, CELL - 2);
  }

  document.addEventListener('keydown', (e) => {
    if (gameOver) return;
    if (e.key === 'ArrowLeft' && !collides(piece.shape, piece.x - 1, piece.y)) piece.x--;
    else if (e.key === 'ArrowRight' && !collides(piece.shape, piece.x + 1, piece.y)) piece.x++;
    else if (e.key === 'ArrowDown') drop();
    else if (e.key === 'ArrowUp') {
      const r = rotate(piece.shape);
      if (!collides(r, piece.x, piece.y)) piece.shape = r;
    }
    draw();
  });

  function loop(time) {
    if (!gameOver && !deterministicMode) {
      if (!loop.last) loop.last = time;
      if (time - loop.last > 500) { drop(); loop.last = time; }
      draw();
    }
    requestAnimationFrame(loop);
  }

  function start(deterministic = false) {
    deterministicMode = deterministic;
    grid = emptyGrid();
    piece = newPiece();
    score = 0; lines = 0; gameOver = false;
    stepRemainder = 0;
    scoreEl.textContent = '0';
    linesEl.textContent = '0';
    loop.last = 0;
    draw();
  }

  document.getElementById('restart').addEventListener('click', () => start(false));
  start(false);
  requestAnimationFrame(loop);
  window.gameTest = {
    reset() { start(true); return this.state(); },
    setBoard(rows) { deterministicMode=true; if(rows.length!==ROWS||rows.some(r=>r.length!==COLS)) throw new Error('invalid board'); grid=rows.map(r=>[...r]); stepRemainder=0; draw(); },
    setPiece(next) { deterministicMode=true; piece={shape:next.shape.map(r=>[...r]),x:next.x,y:next.y,color:next.color||'#fff'}; stepRemainder=0; draw(); },
    move(dx) { if(!gameOver&&!collides(piece.shape,piece.x+dx,piece.y)) piece.x+=dx; draw(); return this.state(); },
    rotate() { const r=rotate(piece.shape); if(!gameOver&&!collides(r,piece.x,piece.y)) piece.shape=r; draw(); return this.state(); },
    drop() { drop(); draw(); return this.state(); },
    step(ms) { if(Number.isFinite(ms)&&ms>0){stepRemainder+=ms;while(stepRemainder>=500&&!gameOver){drop();stepRemainder-=500;}} draw(); return this.state(); },
    state() { return { rows:ROWS, cols:COLS, board:grid.map(r=>[...r]), piece:{...piece,shape:piece.shape.map(r=>[...r])}, score, lines, gameOver }; }
  };
</script>
</body>
</html>