HTML Tetris Game
Here's a complete HTML Tetris game with the controls you requested:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Tetris</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
#game-container {
display: flex;
justify-content: center;
margin-top: 20px;
}
#tetris {
border: 2px solid #333;
background-color: #111;
}
#score-display {
margin-top: 20px;
font-size: 24px;
font-weight: bold;
}
.instructions {
margin-top: 20px;
padding: 10px;
background-color: #ddd;
border-radius: 5px;
display: inline-block;
}
</style>
</head>
<body>
<h1>JavaScript Tetris</h1>
<div id="game-container">
<canvas id="tetris" width="300" height="600"></canvas>
</div>
<div id="score-display">Score: 0</div>
<div class="instructions">
<p>Controls:</p>
<p>← → : Move left/right</p>
<p>↓ : Accelerate descent</p>
<p>Space : Rotate 90°</p>
</div>
<script>
const canvas = document.getElementById('tetris');
const context = canvas.getContext('2d');
const scoreDisplay = document.getElementById('score-display');
// Scale up the drawing
context.scale(30, 30);
// Game state
let score = 0;
let dropCounter = 0;
let dropInterval = 1000;
let lastTime = 0;
let gameOver = false;
// Game board (10x20)
const arena = createMatrix(12, 20);
// Player piece
const player = {
pos: {x: 0, y: 0},
matrix: null,
score: 0
};
// Tetromino shapes
const colors = [
null,
'#FF0D72', // I
'#0DC2FF', // J
'#0DFF72', // L
'#F538FF', // O
'#FF8E0D', // S
'#FFE138', // T
'#3877FF' // Z
];
// Create matrix helper function
function createMatrix(w, h) {
const matrix = [];
while (h--) {
matrix.push(new Array(w).fill(0));
}
return matrix;
}
// Create tetromino shapes
function createPiece(type) {
if (type === 'I') {
return [
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
];
} else if (type === 'L') {
return [
[0, 2, 0],
[0, 2, 0],
[0, 2, 2],
];
} else if (type === 'J') {
return [
[0, 3, 0],
[0, 3, 0],
[3, 3, 0],
];
} else if (type === 'O') {
return [
[4, 4],
[4, 4],
];
} else if (type === 'Z') {
return [
[5, 5, 0],
[0, 5, 5],
[0, 0, 0],
];
} else if (type === 'S') {
return [
[0, 6, 6],
[6, 6, 0],
[0, 0, 0],
];
} else if (type === 'T') {
return [
[0, 7, 0],
[7, 7, 7],
[0, 0, 0],
];
}
}
// Draw the matrix
function drawMatrix(matrix, offset) {
matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
context.fillStyle = colors[value];
context.fillRect(x + offset.x, y + offset.y, 1, 1);
context.strokeStyle = '#000';
context.lineWidth = 0.05;
context.strokeRect(x + offset.x, y + offset.y, 1, 1);
}
});
});
}
// Draw the game state
function draw() {
// Clear the canvas
context.fillStyle = '#111';
context.fillRect(0, 0, canvas.width, canvas.height);
// Draw the arena
drawMatrix(arena, {x: 1, y: 0});
// Draw the player piece
drawMatrix(player.matrix, player.pos);
}
// Merge the player piece into the arena
function merge() {
player.matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
arena[y + player.pos.y][x + player.pos.x] = value;
}
});
});
}
// Rotate the player piece
function rotate() {
const matrix = player.matrix;
const N = matrix.length;
// Transpose the matrix
for (let y = 0; y < N; y++) {
for (let x = 0; x < y; x++) {
[matrix[x][y], matrix[y][x]] = [matrix[y][x], matrix[x][y]];
}
}
// Reverse each row
for (let y = 0; y < N; y++) {
matrix[y].reverse();
}
}
// Check for collisions
function collide() {
const [m, o] = [player.matrix, player.pos];
for (let y = 0; y < m.length; y++) {
for (let x = 0; x < m[y].length; x++) {
if (m[y][x] !== 0 &&
(arena[y + o.y] === undefined ||
arena[y + o.y][x + o.x] === undefined ||
arena[y + o.y][x + o.x] !== 0)) {
return true;
}
}
}
return false;
}
// Move the player piece
function playerMove(dir) {
player.pos.x += dir;
if (collide()) {
player.pos.x -= dir;
}
}
// Reset the player piece
function playerReset() {
const pieces = 'ILJOTSZ';
player.matrix = createPiece(pieces[Math.floor(Math.random() * pieces.length)]);
player.pos.y = 0;
player.pos.x = (arena[0].length / 2 | 0) - (player.matrix[0].length / 2 | 0);
// Game over if collision on reset
if (collide()) {
gameOver = true;
alert('Game Over! Your score: ' + score);
arena.forEach(row => row.fill(0));
score = 0;
scoreDisplay.textContent = 'Score: 0';
}
}
// Drop the player piece
function playerDrop() {
player.pos.y++;
if (collide()) {
player.pos.y--;
merge();
playerReset();
arenaSweep();
}
dropCounter = 0;
}
// Rotate the player piece with collision check
function playerRotate() {
const pos = player.pos.x;
let offset = 1;
rotate();
while (collide()) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.matrix[0].length) {
rotate();
player.pos.x = pos;
return;
}
}
}
// Clear completed lines
function arenaSweep() {
let rowCount = 1;
outer: for (let y = arena.length - 1; y > 0; y--) {
for (let x = 1; x < arena[y].length - 1; x++) {
if (arena[y][x] === 0) {
continue outer;
}
}
const row = arena.splice(y, 1)[0].fill(0);
arena.unshift(row);
y++;
score += rowCount * 10;
rowCount *= 2;
}
scoreDisplay.textContent = 'Score: ' + score;
}
// Update the game state
function update(time = 0) {
if (gameOver) return;
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter