Gra zręcznościowa Tetris
Kompletny Kod HTML, CSS i JavaScript dla Tetrisa
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 | <!DOCTYPE html> <html lang="pl"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tetris - JavaScript</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; font-family: Arial, sans-serif; } #game-container { display: flex; } #tetris { width: 200px; height: 400px; display: grid; grid-template-columns: repeat(10, 20px); grid-template-rows: repeat(20, 20px); border: 2px solid #333; margin-right: 20px; } .cell { width: 20px; height: 20px; background-color: #f0f0f0; border: 1px solid #ccc; } .fixed { background-color: #333; } .active { background-color: #e74c3c; } #instructions { max-width: 200px; } button { margin-top: 10px; padding: 10px; font-size: 16px; cursor: pointer; } #score-board { margin-top: 20px; } </style> </head> <body> <div id="game-container"> <div id="tetris"></div> <div id="instructions"> <h2>Instrukcja Gry Tetris</h2> <p>◄ - Przesuń w lewo</p> <p>► - Przesuń w prawo</p> <p>▼ - Przyspiesz opadanie</p> <p>▲ - Obrót klocka</p> <button id="startButton">Start Gry</button> <button id="pauseButton">Pauza/Wznów</button> <div id="score-board"> <h3>Wynik</h3> <p>Aktualny: <span id="currentScore">0</span></p> <p>Najlepszy: <span id="highScore">0</span></p> </div> </div> </div> <script src="tetris.js"></script> </body> </html> |
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 | const COLS = 10, ROWS = 20; let board = Array.from({ length: ROWS }, () => Array(COLS).fill(0)); let currentPiece, gameInterval, isPaused = false, gameOver = false; let currentScore = 0; let highScore = localStorage.getItem('highScore') || 0; document.getElementById('currentScore').textContent = currentScore; document.getElementById('highScore').textContent = highScore; const tetris = document.getElementById('tetris'); const startButton = document.getElementById('startButton'); const pauseButton = document.getElementById('pauseButton'); // Klasy i logika gry class Piece { constructor(shape) { this.shape = shape; this.x = 4; this.y = 0; } move(dx, dy) { this.x += dx; this.y += dy; } rotate() { this.shape = this.shape[0].map((_, i) => this.shape.map(row => row[i]).reverse()); } draw() { this.shape.forEach((row, y) => { row.forEach((cell, x) => { if (cell) { const cellElement = document.createElement('div'); cellElement.classList.add('cell', 'active'); cellElement.style.gridRowStart = this.y + y + 1; cellElement.style.gridColumnStart = this.x + x + 1; tetris.appendChild(cellElement); } }); }); } collides() { return this.shape.some((row, y) => row.some((cell, x) => cell && ( this.y + y >= ROWS || this.x + x < 0 || this.x + x >= COLS || board[this.y + y][this.x + x] )) ); } lock() { this.shape.forEach((row, y) => { row.forEach((cell, x) => { if (cell) board[this.y + y][this.x + x] = 1; }); }); clearLines(); } } function spawnPiece() { const shapes = [ [[1, 1, 1, 1]], [[1, 1], [1, 1]], [[0, 1, 1], [1, 1, 0]], [[1, 1, 0], [0, 1, 1]], [[1, 0, 0], [1, 1, 1]] ]; currentPiece = new Piece(shapes[Math.floor(Math.random() * shapes.length)]); } function clearLines() { board = board.filter(row => !row.every(cell => cell)); while (board.length < ROWS) board.unshift(Array(COLS).fill(0)); currentScore += 100; document.getElementById('currentScore').textContent = currentScore; } function updateHighScore() { if (currentScore > highScore) { highScore = currentScore; localStorage.setItem('highScore', highScore); document.getElementById('highScore').textContent = highScore; } } function draw() { tetris.innerHTML = ''; board.forEach((row, y) => { row.forEach((cell, x) => { if (cell) { const cellElement = document.createElement('div'); cellElement.classList.add('cell', 'fixed'); cellElement.style.gridRowStart = y + 1; cellElement.style.gridColumnStart = x + 1; tetris.appendChild(cellElement); } }); }); currentPiece.draw(); } function update() { if (gameOver || isPaused) return; currentPiece.move(0, 1); if (currentPiece.collides()) { currentPiece.move(0, -1); currentPiece.lock(); spawnPiece(); } draw(); } function startGame() { board = Array.from({ length: ROWS }, () => Array(COLS).fill(0)); currentScore = 0; gameOver = false; document.getElementById('currentScore').textContent = currentScore; spawnPiece(); gameInterval = setInterval(update, 500); } function pauseGame() { isPaused = !isPaused; pauseButton.textContent = isPaused ? 'Wznów' : 'Pauza'; } // Zdarzenia document.addEventListener('keydown', e => { if (e.key === 'ArrowLeft') currentPiece.move(-1, 0); if (e.key === 'ArrowRight') currentPiece.move(1, 0); if (e.key === 'ArrowDown') currentPiece.move(0, 1); if (e.key === 'ArrowUp') currentPiece.rotate(); draw(); }); startButton.addEventListener('click', startGame); pauseButton.addEventListener('click', pauseGame); |


