File size: 8,555 Bytes
5802f4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
document.addEventListener('DOMContentLoaded', () => {
    const canvas = document.getElementById('tetris');
    const ctx = canvas.getContext('2d');
    
    // Scale canvas
    const scale = window.devicePixelRatio;
    canvas.width = Math.floor(canvas.clientWidth * scale);
    canvas.height = Math.floor(canvas.clientHeight * scale);
    ctx.scale(scale, scale);
    
    // Game state
    const COLS = 10;
    const ROWS = 20;
    const BLOCK_SIZE = canvas.width / 10 / scale;
    const COLORS = [
        null,
        '#3B82F6', // I
        '#8B5CF6', // J
        '#F59E0B', // L
        '#10B981', // O
        '#EF4444', // S
        '#EC4899', // T
        '#84CC16'  // Z
    ];
    
    let board = createBoard();
    let piece = null;
    let score = 0;
    let gameOver = false;
    let dropCounter = 0;
    let lastTime = 0;
    let dropInterval = 1000;
    
    // Pieces
    const PIECES = [
        null,
        { shape: [[0,0,0,0], [1,1,1,1], [0,0,0,0], [0,0,0,0]], color: 'I' },
        { shape: [[2,0,0], [2,2,2], [0,0,0]], color: 'J' },
        { shape: [[0,0,3], [3,3,3], [0,0,0]], color: 'L' },
        { shape: [[0,4,4], [0,4,4], [0,0,0]], color: 'O' },
        { shape: [[0,5,5], [5,5,0], [0,0,0]], color: 'S' },
        { shape: [[0,6,0], [6,6,6], [0,0,0]], color: 'T' },
        { shape: [[7,7,0], [0,7,7], [0,0,0]], color: 'Z' }
    ];
    
    // Game functions
    function createBoard() {
        return Array.from({length: ROWS}, () => Array(COLS).fill(0));
    }
    
    function createPiece() {
        const randomIndex = Math.floor(Math.random() * 7) + 1;
        return {
            position: {x: Math.floor(COLS / 2) - 1, y: 0},
            shape: PIECES[randomIndex].shape,
            color: PIECES[randomIndex].color
        };
    }
    
    function draw() {
        // Clear canvas
        ctx.fillStyle = '#111827';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        
        // Draw board
        board.forEach((row, y) => {
            row.forEach((value, x) => {
                if (value !== 0) {
                    ctx.fillStyle = COLORS[value];
                    ctx.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
                    ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
                    ctx.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
                }
            });
        });
        
        // Draw current piece
        if (piece) {
            piece.shape.forEach((row, y) => {
                row.forEach((value, x) => {
                    if (value !== 0) {
                        ctx.fillStyle = COLORS[value];
                        ctx.fillRect(
                            (piece.position.x + x) * BLOCK_SIZE,
                            (piece.position.y + y) * BLOCK_SIZE,
                            BLOCK_SIZE,
                            BLOCK_SIZE
                        );
                        ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
                        ctx.strokeRect(
                            (piece.position.x + x) * BLOCK_SIZE,
                            (piece.position.y + y) * BLOCK_SIZE,
                            BLOCK_SIZE,
                            BLOCK_SIZE
                        );
                    }
                });
            });
        }
    }
    
    function collide() {
        for (let y = 0; y < piece.shape.length; y++) {
            for (let x = 0; x < piece.shape[y].length; x++) {
                if (piece.shape[y][x] !== 0 &&
                    (board[y + piece.position.y] === undefined ||
                     board[y + piece.position.y][x + piece.position.x] === undefined ||
                     board[y + piece.position.y][x + piece.position.x] !== 0)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    function rotate() {
        const rotated = [];
        for (let y = 0; y < piece.shape[0].length; y++) {
            const row = [];
            for (let x = piece.shape.length - 1; x >= 0; x--) {
                row.push(piece.shape[x][y]);
            }
            rotated.push(row);
        }
        
        const previousShape = piece.shape;
        piece.shape = rotated;
        
        if (collide()) {
            piece.shape = previousShape;
        }
    }
    
    function movePiece(direction) {
        piece.position.x += direction;
        if (collide()) {
            piece.position.x -= direction;
        }
    }
    
    function hardDrop() {
        while (!collide()) {
            piece.position.y++;
        }
        piece.position.y--;
        merge();
        removeRows();
        piece = createPiece();
        
        if (collide()) {
            gameOver = true;
            document.querySelector('custom-game-over').setAttribute('visible', 'true');
        }
    }
    
    function merge() {
        piece.shape.forEach((row, y) => {
            row.forEach((value, x) => {
                if (value !== 0) {
                    board[y + piece.position.y][x + piece.position.x] = value;
                }
            });
        });
    }
    
    function removeRows() {
        let linesCleared = 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++;
            
            linesCleared++;
        }
        
        if (linesCleared > 0) {
            score += calculateScore(linesCleared);
            updateScoreDisplay();
        }
    }
    
    function calculateScore(lines) {
        switch(lines) {
            case 1: return 100;
            case 2: return 300;
            case 3: return 500;
            case 4: return 800;
            default: return 0;
        }
    }
    
    function updateScoreDisplay() {
        document.querySelector('custom-score-display').setAttribute('score', score.toString());
    }
    
    function resetGame() {
        board = createBoard();
        piece = createPiece();
        score = 0;
        gameOver = false;
        dropInterval = 1000;
        updateScoreDisplay();
        document.querySelector('custom-game-over').setAttribute('visible', 'false');
    }
    
    // Game loop
    function update(time = 0) {
        if (gameOver) return;
        
        const deltaTime = time - lastTime;
        lastTime = time;
        
        dropCounter += deltaTime;
        if (dropCounter > dropInterval) {
            piece.position.y++;
            if (collide()) {
                piece.position.y--;
                merge();
                removeRows();
                piece = createPiece();
                
                if (collide()) {
                    gameOver = true;
                    document.querySelector('custom-game-over').setAttribute('visible', 'true');
                }
            }
            dropCounter = 0;
        }
        
        draw();
        requestAnimationFrame(update);
    }
    
    // Controls
    document.addEventListener('keydown', event => {
        if (gameOver) return;
        
        switch(event.keyCode) {
            case 37: // Left
                movePiece(-1);
                break;
            case 39: // Right
                movePiece(1);
                break;
            case 40: // Down
                piece.position.y++;
                if (collide()) {
                    piece.position.y--;
                    merge();
                    removeRows();
                    piece = createPiece();
                    
                    if (collide()) {
                        gameOver = true;
                        document.querySelector('custom-game-over').setAttribute('visible', 'true');
                    }
                }
                dropCounter = 0;
                break;
            case 38: // Up
                rotate();
                break;
            case 32: // Space
                hardDrop();
                break;
            case 80: // P
                // Pause logic would go here
                break;
        }
    });
    
    // Initialize game
    piece = createPiece();
    update();
    
    // Custom element event listeners
    document.addEventListener('start-game', resetGame);
    document.addEventListener('reset-game', resetGame);
});