Okay, here is a functional implementation of Tetris using HTML, CSS, and JavaScript. It includes the core mechanics, scoring, a next-piece preview, and a clean UI using the HTML Canvas.
1. index.html (The Structure)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Tetris</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>HTML Tetris</h1>
<div class="game-container">
<canvas id="tetris-board" width="300" height="600"></canvas>
<div class="side-panel">
<div>
<h2>Score</h2>
<p id="score">0</p>
</div>
<div>
<h2>Next</h2>
<canvas id="next-piece" width="120" height="120"></canvas>
</div>
<button id="start-button">Start Game</button>
<div id="game-over" class="hidden">
<h2>Game Over!</h2>
<p>Press Start to Play Again</p>
</div>
<div class="controls">
<h2>Controls</h2>
<p>←: Move Left</p>
<p>→: Move Right</p>
<p>↓: Move Down</p>
<p>↑: Rotate</p>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
2. style.css (The Look)
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column; /* Stack title above game */
align-items: center;
justify-content: center;
min-height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
h1 {
color: #333;
margin-bottom: 20px; /* Space between title and game */
}
.game-container {
display: flex;
align-items: flex-start; /* Align items to the top */
gap: 20px; /* Space between board and side panel */
background-color: #e0e0e0;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
#tetris-board {
border: 5px solid #333;
background-color: #000; /* Black background for the game area */
display: block; /* Remove extra space below canvas */
}
.side-panel {
display: flex;
flex-direction: column;
align-items: center;
width: 150px; /* Fixed width for the side panel */
gap: 15px;
text-align: center;
}
.side-panel div:not(.controls) { /* Style score and next box */
background-color: #fff;
padding: 10px;
border-radius: 5px;
width: 100%;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.side-panel h2 {
margin-top: 0;
margin-bottom: 5px;
font-size: 1.1em;
color: #555;
}
#score {
font-size: 1.5em;
font-weight: bold;
color: #333;
margin: 0;
}
#next-piece {
background-color: #f8f8f8; /* Light background for preview */
border: 1px solid #ccc;
display: block; /* Remove extra space */
margin: 0 auto; /* Center the canvas */
}
#start-button {
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
transition: background-color 0.3s ease;
width: 100%; /* Make button fill width */
box-sizing: border-box; /* Include padding in width */
}
#start-button:hover {
background-color: #45a049;
}
#game-over {
color: red;
font-weight: bold;
text-align: center;
background-color: rgba(255, 255, 255, 0.9);
padding: 15px;
border-radius: 5px;
border: 2px solid red;
width: calc(100% - 30px); /* Adjust width considering padding */
}
.hidden {
display: none;
}
.controls {
background-color: #fff;
padding: 10px;
border-radius: 5px;
width: 100%;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
margin-top: 10px; /* Add some space above controls */
box-sizing: border-box;
}
.controls p {
margin: 5px 0;
font-size: 0.9em;
color: #666;
}
3. script.js (The Logic)