o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write tetris in HTML, CSS, and JS (using div elements rather than canvas)
- User
Write tetris in HTML, CSS, and JS (using div elements rather than canvas)
- Assistant
Below is an implementation of Tetris using HTML, CSS, and JavaScript, utilizing
divelements instead of the canvas. The game grid is constructed using a grid ofdivelements, and JavaScript handles the game logic, including creating tetrominoes, moving them, rotating them, and detecting collisions. CSS is used to style the grid and the tetrominoes.HTML (
index.html)<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Tetris Game</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="grid"></div> <div class="score-display">Score: <span id="score">0</span></div> <button id="start-button">Start/Pause</button> <script src="app.js"></script> </body> </html>CSS (
style.css)body { display: flex; flex-direction: column; align-items: center; background: #000; color: #fff; font-family: Arial, sans-serif; } .grid { position: relative; width: 200px; height: 400px; background: #222; display: grid; grid-template-rows: repeat(20, 20px); grid-template-columns: repeat(10, 20px); border: 1px solid #555; } .grid div { box-sizing: border-box; border: 1px solid #222; } .tetromino { background: #fff; } .score-display { margin-top: 20px; } #start-button { margin-top: 10px; padding: 10px 20px; }JavaScript (
app.js)document.addEventListener('DOMContentLoaded', () => { const grid = document.querySelector('.grid'); let squares = Array.from(grid.querySelectorAll('div')); const scoreDisplay = document.querySelector('#score'); const startButton = document.querySelector('#start-button'); const width = 10; let nextRandom = 0; let timerId; let score = 0; // The Tetrominoes const lTetromino = [ [1, width+1, width*2+1, 2], [width, width+1, width+2, width*2+2], [1, width+1, width*2+1, width*2], [width, width*2, width*2+1, width*2+2] ]; const zTetromino = [ [0, width, width+1, width*2+1], [width+1, width+2, width*2, width*2+1], [0, width, width+1, width*2+1], [width+1, width+2, width*2, width*2+1] ]; const tTetromino = [ [1, width, width+1, width+2], [1, width+1, width+2, width*2+1], [width, width+1, width+2, width*2+1], [1, width, width+1, width*2+1] ]; const oTetromino = [ [0, 1, width, width+1], [0, 1, width, width+1], [0, 1, width, width+1], [0, 1, width, width+1] ]; const iTetromino = [ [1, width+1, width*2+1, width*3+1], [width, width+1, width+2, width+3], [1, width+1, width*2+1, width*3+1], [width, width+1, width+2, width+3] ]; const theTetrominoes = [lTetromino, zTetromino, tTetromino, oTetromino, iTetromino]; let currentPosition = 4; let currentRotation = 0; // Randomly select a Tetromino and its first rotation let random = Math.floor(Math.random()*theTetrominoes.length); let current = theTetrominoes[random][currentRotation]; // Draw the Tetromino function draw() { current.forEach(index => { squares[currentPosition + index].classList.add('tetromino'); }); } // Undraw the Tetromino function undraw() { current.forEach(index => { squares[currentPosition + index].classList.remove('tetromino'); }); } // Assign functions to keycodes function control(e) { if(e.keyCode === 37) { moveLeft(); } else if(e.keyCode === 38) { rotate(); } else if(e.keyCode === 39) { moveRight(); } else if(e.keyCode === 40) { moveDown(); } } document.addEventListener('keyup', control); // Move down function function moveDown() { undraw(); currentPosition += width; draw(); freeze(); } // Freeze function function freeze() { if(current.some(index => squares[currentPosition + index + width].classList.contains('taken'))) { current.forEach(index => squares[currentPosition + index].classList.add('taken')); // Start a new Tetromino falling random = nextRandom; nextRandom = Math.floor(Math.random()*theTetrominoes.length); current = theTetrominoes[random][currentRotation]; currentPosition = 4; draw(); addScore(); gameOver(); } } // Move the Tetromino left, unless it's at the edge or there's blockage function moveLeft() { undraw(); const isAtLeftEdge = current.some(index => (currentPosition + index) % width === 0); if(!isAtLeftEdge) currentPosition -=1; if(current.some(index => squares[currentPosition + index].classList.contains('taken'))) { currentPosition +=1; } draw(); } // Move the Tetromino right, unless it's at the edge or there's blockage function moveRight() { undraw(); const isAtRightEdge = current.some(index => (currentPosition + index) % width === width -1); if(!isAtRightEdge) currentPosition +=1; if(current.some(index => squares[currentPosition + index].classList.contains('taken'))) { currentPosition -=1; } draw(); } // Rotate the Tetromino function rotate() { undraw(); currentRotation++; if(currentRotation === current.length) { // if currentRotation gets to 4, make it go back to 0 currentRotation = 0; } current = theTetrominoes[random][currentRotation]; draw(); } // Start/Pause functionality startButton.addEventListener('click', () => { if(timerId) { clearInterval(timerId); timerId = null; } else { draw(); timerId = setInterval(moveDown, 1000); nextRandom = Math.floor(Math.random()*theTetrominoes.length); } }); // Add score function addScore() { for(let i = 0; i < 199; i += width) { const row = [i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9]; if(row.every(index => squares[index].classList.contains('taken'))) { score += 10; scoreDisplay.innerHTML = score; row.forEach(index => { squares[index].classList.remove('taken'); squares[index].classList.remove('tetromino'); }); const removedSquares = squares.splice(i, width); squares = removedSquares.concat(squares); squares.forEach(cell => grid.appendChild(cell)); } } } // Game over function gameOver() { if(current.some(index => squares[currentPosition + index].classList.contains('taken'))) { scoreDisplay.innerHTML = 'Game Over'; clearInterval(timerId); } } // Create grid squares function createGrid() { // The main grid for(let i = 0; i < 200; i++) { const square = document.createElement('div'); grid.appendChild(square); } // Set bottom squares as taken for(let i = 0; i < 10; i++) { const square = document.createElement('div'); square.classList.add('taken'); grid.appendChild(square); } squares = Array.from(document.querySelectorAll('.grid div')); } createGrid(); });Note: This is a basic implementation and can be further enhanced with additional features such as displaying the next tetromino, adding sound effects, and improving the overall styling and user experience.