Creating a complete Tetris game in a single HTML file is a fun challenge! Below is a simple implementation of Tetris using HTML, CSS, and JavaScript. This code includes everything you need to run the game in your browser. Just copy and paste it into an HTML file and open it in your web browser.
<!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 {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
color: #fff;
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid #fff;
background-color: #111;
}
#score {
position: absolute;
top: 20px;
left: 20px;
font-size: 24px;
}
</style>
</head>
<body>
<canvas id="tetris" width="240" height="400"></canvas>
<div id="score">Score: 0</div>
<script>
const canvas = document.getElementById('tetris');
const context = canvas.getContext('2d');
const scoreDisplay = document.getElementById('score');
context.scale(20, 20);
let board = Array.from({ length: 20 }, () => Array(12).fill(0));
let score = 0;
const tetrominoes = [
[[1, 1, 1, 1]], // I
[[1, 1], [1, 1]], // O
[[0, 1, 0], [1, 1, 1]], // T
[[1, 1, 0], [0, 1, 1]], // S
[[0, 1, 1], [1, 1, 0]], // Z
[[1, 0, 0], [1, 1, 1]], // L
[[0, 0, 1], [1, 1, 1]], // J
];
let currentTetromino = getRandomTetromino();
let currentPosition = { x: 4, y: 0 };
function getRandomTetromino() {
return tetrominoes[Math.floor(Math.random() * tetrominoes.length)];
}
function drawBoard() {
context.clearRect(0, 0, canvas.width, canvas.height);
board.forEach((row, y) => {
row.forEach((value, x) => {
if (value) {
context.fillStyle = 'white';
context.fillRect(x, y, 1, 1);
}
});
});
drawTetromino();
}
function drawTetromino() {
currentTetromino.forEach((row, y) => {
row.forEach((value, x) => {
if (value) {
context.fillStyle = 'white';
context.fillRect(currentPosition.x + x, currentPosition.y + y, 1, 1);
}
});
});
}
function collide() {
return currentTetromino.some((row, y) => {
return row.some((value, x) => {
if (value) {
const newX = currentPosition.x + x;
const newY = currentPosition.y + y;
return newX < 0 || newX >= 12 || newY >= 20 || board[newY] && board[newY][newX];
}
return false;
});
});
}
function merge() {
currentTetromino.forEach((row, y) => {
row.forEach((value, x) => {
if (value) {
board[currentPosition.y + y][currentPosition.x + x] = 1;
}
});
});
}
function rotate() {
const rotated = currentTetromino[0].map((_, index) => currentTetromino.map(row => row[index]).reverse());
const originalTetromino = currentTetromino;
currentTetromino = rotated;
if (collide()) {
currentTetromino = originalTetromino;
}
}
function clearLines() {
let linesCleared = 0;
for (let y = board.length - 1; y >= 0; y--) {
if (board[y].every(value => value)) {
board.splice(y, 1);
board.unshift(Array(12).fill(0));
linesCleared++;
}
}
score += linesCleared * 100;
scoreDisplay.innerText = `Score: ${score}`;
}
function drop() {
currentPosition.y++;
if (collide()) {
currentPosition.y--;
merge();
clearLines();
currentTetromino = getRandomTetromino();
currentPosition = { x: 4, y: 0 };
if (collide()) {
alert('Game Over!');
resetGame();
}
}
}
function resetGame() {
board = Array.from({ length: 20 }, () => Array(12).fill(0));
score = 0;
scoreDisplay.innerText = `Score: ${score}`;
currentTetromino = getRandomTetromino();
currentPosition = { x: 4, y: 0 };
}
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
currentPosition.x--;
if (collide()) currentPosition.x++;
} else if (event.key === 'ArrowRight') {
currentPosition.x++;
if (collide()) currentPosition.x--;
} else if (event.key === 'ArrowDown') {
drop();
} else if (event.key === 'ArrowUp') {
rotate();
}
});
function update() {
drop();
drawBoard();
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>
How to Run the Game
- Copy the code above.
- Open a text editor (like Notepad, VSCode, or any code editor).
- Paste the code into a new file.
- Save the file with a
.html extension, for example, tetris.html.
- Open the saved file in a web browser.
Controls
- Arrow Left: Move the tetromino left.
- Arrow Right: Move the tetromino right.
- Arrow Down: Drop the tetromino faster.
- Arrow Up: Rotate the tetromino.
Features
- Basic Tetris gameplay.
- Score tracking.
- Game over alert when the tetrominoes stack to the top.
Feel free to modify and enhance the game as you like! Enjoy coding!