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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tetris Game</title>
    <style>
        body {
            margin: 0;
            font-family: 'Arial', sans-serif;
            background-color: #222;
            color: white;
            text-align: center;
        }
        canvas {
            background-color: black;
            display: block;
            margin: 0 auto;
        }
        #gameCanvas {
            border: 1px solid #444;
        }
        #welcomeScreen, #gameOverScreen {
            display: none;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(0, 0, 0, 0.8);
            padding: 20px;
            border-radius: 10px;
            width: 300px;
            text-align: center;
        }
        #welcomeScreen {
            background-image: url('tetris7.png');
            background-size: cover;
            background-position: center;
        }
        #welcomeScreen input, #welcomeScreen button {
            margin-top: 10px;
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
        }
        #gameOverScreen button {
            margin-top: 10px;
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #ff5555;
            color: white;
            font-size: 16px;
        }
        #homeButton {
            margin-top: 10px;
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #007BFF;
            color: white;
            font-size: 16px;
        }

        /* Style for control buttons */
        .control-buttons {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            justify-content: center;
            gap: 10px;
        }
        .control-buttons button {
            width: 50px;
            height: 50px;
            font-size: 24px;
            background-color: #444;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .control-buttons button:hover {
            background-color: #666;
        }
        .control-buttons button:active {
            background-color: #333;
        }
    </style>
</head>
<body>
    <h1>Tetris Game</h1>
    <canvas id="gameCanvas" width="200" height="400"></canvas>

    <div id="welcomeScreen">
        <h2>Welcome to Tetris!</h2>
        <label for="usernameInput">Enter your name:</label>
        <input type="text" id="usernameInput" placeholder="Your Name">
        <button onclick="startGame()">Start Game</button>
    </div>

    <div id="gameOverScreen">
        <h2 id="gameOverMessage">Game Over!</h2>
        <p id="finalScore">Score: 0</p>
        <button onclick="restartGame()">Restart</button>
        <button id="homeButton" onclick="goHome()">Home</button>
    </div>

    <!-- On-screen control buttons -->
    <div class="control-buttons">
        <button id="rotateBtn">🔄</button>
        <button id="leftBtn">⬅️</button>
        <button id="downBtn">⬇️</button>
        <button id="rightBtn">➡️</button>
    </div>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');

        const gridSize = { width: 10, height: 20 };
        const zoom = 20;
        const fps = 5; // Slower falling speed

        const colors = [
            'none', // Placeholder for index 0 (empty cell)
            '#7800FF', // Purple
            '#00FFFF', // Cyan
            '#FFA500', // Orange
            '#FFFF00', // Yellow
            '#00FF00', // Green
            '#FF0000', // Red
            '#0000FF'  // Blue
        ];

        let game = null;
        let username = '';
        let music = new Audio('music.mp3'); // Create a global audio object

        class Figure {
            constructor() {
                const figures = [
                    [[1, 5, 9, 13], [4, 5, 6, 7]],  // I
                    [[1, 2, 5, 6]],  // O
                    [[1, 4, 5, 6], [0, 4, 5, 8], [4, 5, 6, 9], [2, 5, 6, 10]],  // T
                    [[0, 1, 5, 6], [2, 4, 5, 7]],  // S
                    [[1, 2, 4, 5], [1, 5, 6, 10]], // Z
                    [[0, 4, 5, 9], [2, 4, 5, 6], [4, 5, 6, 8], [0, 1, 5, 9]], // J
                    [[1, 5, 6, 9], [2, 4, 5, 6], [1, 5, 6, 10], [0, 4, 5, 7]]  // L
                ];
                this.type = Math.floor(Math.random() * figures.length);
                this.color = colors[this.type + 1]; // Assign different colors to each figure
                this.rotation = 0;
                this.x = 3;
                this.y = 0;
                this.figures = figures;
            }

            getCurrentFigure() {
                return this.figures[this.type][this.rotation];
            }

            rotate() {
                this.rotation = (this.rotation + 1) % this.figures[this.type].length;
            }
        }

        class Tetris {
            constructor() {
                this.height = gridSize.height;
                this.width = gridSize.width;
                this.field = Array.from({ length: this.height }, () => Array(this.width).fill(0));
                this.score = 0;
                this.state = 'welcome';
                this.figure = null;
                this.level = 2;
                this.username = '';
            }

            newFigure() {
                this.figure = new Figure();
            }

            intersects() {
                for (let i = 0; i < 4; i++) {
                    for (let j = 0; j < 4; j++) {
                        if (this.figure.getCurrentFigure().includes(i * 4 + j)) {
                            const x = j + this.figure.x;
                            const y = i + this.figure.y;
                            if (x < 0 || x >= this.width || y >= this.height || this.field[y][x]) {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }

            breakLines() {
                let lines = 0;
                for (let i = 0; i < this.height; i++) {
                    if (this.field[i].every(cell => cell !== 0)) {
                        lines++;
                        for (let k = i; k > 0; k--) {
                            this.field[k] = [...this.field[k - 1]];
                        }
                        this.field[0] = Array(this.width).fill(0);
                    }
                }
                this.score += lines * lines;
            }

            goDown() {
                this.figure.y += 1;
                if (this.intersects()) {
                    this.figure.y -= 1;
                    this.freeze();
                }
            }

            goSide(dx) {
                this.figure.x += dx;
                if (this.intersects()) {
                    this.figure.x -= dx;
                }
            }

            rotateFigure() {
                const oldRotation = this.figure.rotation;
                this.figure.rotate();
                if (this.intersects()) {
                    this.figure.rotation = oldRotation;
                }
            }

            freeze() {
                for (let i = 0; i < 4; i++) {
                    for (let j = 0; j < 4; j++) {
                        if (this.figure.getCurrentFigure().includes(i * 4 + j)) {
                            this.field[i + this.figure.y][j + this.figure.x] = this.figure.color;
                        }
                    }
                }
                this.breakLines();
                this.newFigure();
                if (this.intersects()) {
                    this.state = 'gameover';
                    document.getElementById('gameOverMessage').textContent = `Game Over, ${this.username}!`;
                    document.getElementById('finalScore').textContent = `Score: ${this.score}`;
                    document.getElementById('gameOverScreen').style.display = 'block';
                    stopMusic(); // Stop music on game over
                }
            }
        }

        function drawGrid() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            for (let i = 0; i < game.height; i++) {
                for (let j = 0; j < game.width; j++) {
                    ctx.strokeStyle = 'gray';
                    ctx.strokeRect(j * zoom, i * zoom, zoom, zoom);
                    if (game.field[i][j] !== 0) {
                        ctx.fillStyle = game.field[i][j];
                        ctx.fillRect(j * zoom + 1, i * zoom + 1, zoom - 2, zoom - 2);
                    }
                }
            }
        }

        function drawFigure() {
            if (game.figure !== null) {
                const figure = game.figure.getCurrentFigure();
                for (let i = 0; i < 4; i++) {
                    for (let j = 0; j < 4; j++) {
                        if (figure.includes(i * 4 + j)) {
                            ctx.fillStyle = game.figure.color;
                            ctx.fillRect((j + game.figure.x) * zoom + 1, (i + game.figure.y) * zoom + 1, zoom - 2, zoom - 2);
                        }
                    }
                }
            }
        }

        function drawText() {
            ctx.fillStyle = 'white';
            ctx.font = '14px Arial';
            ctx.fillText(`Score: ${game.score}`, 5, 15);
        }

        function updateGame() {
            if (game.state === 'start') {
                game.goDown();
                drawGrid();
                drawFigure();
                drawText();
            }
        }

        function startGame() {
            username = document.getElementById('usernameInput').value;
            if (!username) {
                alert('Please enter your name.');
                return;
            }
            game = new Tetris();
            game.newFigure();
            game.username = username;
            game.state = 'start';
            document.getElementById('welcomeScreen').style.display = 'none';
            startMusic(); // Start music when the game starts
            gameLoop();
        }

        function gameLoop() {
            if (game.state !== 'gameover') {
                updateGame();
                setTimeout(gameLoop, 1000 / fps);
            }
        }

        function restartGame() {
            document.getElementById('gameOverScreen').style.display = 'none';
            startGame();
        }

        function goHome() {
            document.getElementById('gameOverScreen').style.display = 'none';
            document.getElementById('welcomeScreen').style.display = 'block';
        }

        function startMusic() {
            music.loop = true;
            music.volume = 0.5;
            music.play();
        }

        function stopMusic() {
            music.pause();
            music.currentTime = 0; // Reset to start
        }

        // Handle keyboard controls
        document.addEventListener('keydown', (event) => {
            if (game && game.state === 'start') {
                switch (event.key) {
                    case 'ArrowUp':
                        game.rotateFigure();
                        break;
                    case 'ArrowDown':
                        game.goDown();
                        break;
                    case 'ArrowLeft':
                        game.goSide(-1);
                        break;
                    case 'ArrowRight':
                        game.goSide(1);
                        break;
                }
                drawGrid();
                drawFigure();
                drawText();
            }
        });

        // Handle touch controls
        let touchStartX = 0;
        let touchStartY = 0;

        canvas.addEventListener('touchstart', (event) => {
            event.preventDefault();
            touchStartX = event.touches[0].clientX;
            touchStartY = event.touches[0].clientY;
        });

        canvas.addEventListener('touchend', (event) => {
            event.preventDefault();
            const touchEndX = event.changedTouches[0].clientX;
            const touchEndY = event.changedTouches[0].clientY;
            const dx = touchEndX - touchStartX;
            const dy = touchEndY - touchStartY;

            if (Math.abs(dx) > Math.abs(dy)) {
                if (dx > 30) { // Swipe right
                    game.goSide(1);
                } else if (dx < -30) { // Swipe left
                    game.goSide(-1);
                }
            } else {
                if (dy > 30) { // Swipe down
                    game.goDown();
                }
            }
            drawGrid();
            drawFigure();
            drawText();
        });

        canvas.addEventListener('touchmove', (event) => {
            event.preventDefault();
        });

        // On-screen button event handlers
        document.getElementById('rotateBtn').addEventListener('click', () => {
            if (game && game.state === 'start') {
                game.rotateFigure();
                drawGrid();
                drawFigure();
                drawText();
            }
        });

        document.getElementById('leftBtn').addEventListener('click', () => {
            if (game && game.state === 'start') {
                game.goSide(-1);
                drawGrid();
                drawFigure();
                drawText();
            }
        });

        document.getElementById('downBtn').addEventListener('click', () => {
            if (game && game.state === 'start') {
                game.goDown();
                drawGrid();
                drawFigure();
                drawText();
            }
        });

        document.getElementById('rightBtn').addEventListener('click', () => {
            if (game && game.state === 'start') {
                game.goSide(1);
                drawGrid();
                drawFigure();
                drawText();
            }
        });

        window.onload = () => {
            document.getElementById('welcomeScreen').style.display = 'block';
            // No need to play music on window load
        };
    </script>
</body>
</html>