Let's create a web-based Tetris game using HTML, CSS, and JavaScript that can be opened directly in any browser. This version will have all the same features but work entirely in a web environment.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tetris</title>
<style>
body {
background-color: #1a1a1a;
color: white;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
overflow: hidden;
}
h1 {
font-family: 'Impact', sans-serif;
font-size: 3rem;
margin-bottom: 10px;
letter-spacing: 5px;
}
.game-container {
display: flex;
gap: 30px;
margin-top: 20px;
}
#game-board {
border: 5px solid #ccc;
background-color: #000;
box-shadow: 0 0 20px rgba(255, 255, 255, 0.1);
}
.side-panel {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 200px;
height: 600px;
}
.next-piece-container {
background-color: #222;
border: 3px solid #444;
padding: 10px;
text-align: center;
height: 150px;
}
.next-piece-container h3 {
margin-top: 0;
margin-bottom: 10px;
}
#next-piece-canvas {
background-color: #000;
margin: 0 auto;
}
.score-container {
background-color: #222;
border: 3px solid #444;
padding: 20px;
text-align: center;
}
.score-container h3 {
margin-top: 0;
margin-bottom: 15px;
}
.score-value {
font-size: 1.8rem;
font-weight: bold;
margin-bottom: 15px;
}
.level-value {
font-size: 1.5rem;
font-weight: bold;
}
.controls-container {
background-color: #222;
border: 3px solid #444;
padding: 15px;
}
.controls-container h3 {
margin-top: 0;
margin-bottom: 10px;
text-align: center;
}
.controls-list {
list-style-type: none;
padding: 0;
margin: 0;
}
.controls-list li {
margin-bottom: 8px;
display: flex;
justify-content: space-between;
}
.key {
background-color: #444;
padding: 2px 8px;
border-radius: 4px;
font-family: monospace;
}
#game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
padding: 30px 50px;
border-radius: 10px;
text-align: center;
display: none;
}
#game-over h2 {
font-size: 3rem;
margin-bottom: 20px;
color: #ff5555;
}
#restart-btn {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px 2px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s;
}
#restart-btn:hover {
background-color: #45a049;
}
#start-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #1a1a1a;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 10;
}
#start-screen h1 {
font-size: 5rem;
margin-bottom: 30px;
}
#start-btn {
background-color: #4CAF50;
border: none;
color: white;
padding: 15px 30px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
margin: 20px 2px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s;
}
#start-btn:hover {
background-color: #45a049;
}
.start-controls {
margin-top: 30px;
background-color: #222;
padding: 20px;
border-radius: 10px;
max-width: 400px;
}
.start-controls h3 {
text-align: center;
margin-top: 0;
}
.start-controls ul {
list-style-type: none;
padding: 0;
}
.start-controls li {
margin-bottom: 10px;
display: flex;
justify-content: space-between;
}
</style>
</head>
<body>
<div id="start-screen">
<h1>T E T R I S</h1>
<button id="start-btn">START GAME</button>
<div class="start-controls">
<h3>Controls</h3>
<ul>
<li><span class="key">←</span> <span>Move Left</span></li>
<li><span class="key">→</span> <span>Move Right</span></li>
<li><span class="key">↓</span> <span>Soft Drop</span></li>
<li><span class="key">↑</span> <span>Rotate</span></li>
<li><span class="key">Space</span> <span>Hard Drop</span></li>
</ul>
</div>
</div>
<h1>T E T R I S</h1>
<div class="game-container">
<canvas id="game-board" width="300" height="600"></canvas>
<div class="side-panel">
<div class="next-piece-container">
<h3>NEXT PIECE</h3>
<canvas id="next-piece-canvas" width="120" height="90"></canvas>
</div>
<div class="score-container">
<h3>SCORE</h3>
<div id="score" class="score-value">0</div>
<h3>LEVEL</h3>
<div id="level" class="level-value">1</div>
</div>
<div class="controls-container">
<h3>CONTROLS</h3>
<ul class="controls-list">
<li><span>Left</span> <span class="key">←</span></li>
<li><span>Right</span> <span class="key">→</span></li>
<li><span>Down</span> <span class="key">↓</span></li>
<li><span>Rotate</span> <span class="key">↑</span></li>
<li><span>Hard Drop</span> <span class="key">Space</span></li>
</ul>
</div>
</div>
</div>
<div id="game-over">
<h2>GAME OVER</h2>
<div id="final-score"></div>
<button id="restart-btn">Play Again</button>
</div>
<script>
// Game constants
const COLS = 10;
const ROWS = 20;
const BLOCK_SIZE = 30;
const EMPTY = 'black';
// Tetromino colors
const COLORS = [
null,
'#00FF00', // S - Green
'#FF0000', // Z - Red
'#00FFFF', // I - Cyan
'#FFFF00', // O - Yellow
'#0000FF', // J - Blue
'#FFA500', // L - Orange
'#800080' // T - Purple
];
// Tetromino shapes
const SHAPES = [
null,
[
[0, 0, 0, 0],
[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 0]
], // S
[
[0, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]
], // Z
[
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
], // I
[
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]
], // O
[
[0, 0, 0, 0],
[1, 0, 0, 0],
[1, 1, 1, 0],
[0, 0, 0, 0]
], // J
[
[0, 0, 0, 0],
[0, 0, 1, 0],
[1, 1, 1, 0],
[0, 0, 0, 0]
], // L
[
[0, 0, 0, 0],
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 0, 0, 0]
] // T
];
// Game variables
let canvas, ctx;
let nextCanvas, nextCtx;
let board = [];
let currentPiece, nextPiece;
let dropCounter = 0;
let dropInterval = 1000; // Initial drop speed (ms)
let lastTime = 0;
let score = 0;
let level = 1;
let linesCleared = 0;
let gameOver = false;
let gameStarted = false;
let animationId;
// DOM elements
const scoreElement = document.getElementById('score');
const levelElement = document.getElementById('level');
const gameOverElement = document.getElementById('game-over');
const finalScoreElement = document.getElementById('final-score');
const restartButton = document.getElementById('restart-btn');
const startButton = document.getElementById('start-btn');
const startScreen = document.getElementById('start-screen');
// Initialize the game
function init() {
canvas = document.getElementById('game-board');
ctx = canvas.getContext('2d');
nextCanvas = document.getElementById('next-piece-canvas');
nextCtx = nextCanvas.getContext('2d');
// Create the board
createBoard();
// Event listeners
document.addEventListener('keydown', handleKeyPress);
restartButton.addEventListener('click', restart);
startButton.addEventListener('click', startGame);
// Draw the empty board
drawBoard();
}
// Create the game board
function createBoard() {
board = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
}
// Draw the board
function drawBoard() {
ctx.fillStyle = EMPTY;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw the grid
ctx.strokeStyle = '#333';
ctx.lineWidth = 1;
// Draw horizontal lines
for (let y = 0; y < ROWS; y++) {
ctx.beginPath();
ctx.moveTo(0, y * BLOCK_SIZE);
ctx.lineTo(canvas.width, y * BLOCK_SIZE);
ctx.stroke();
}
// Draw vertical lines
for (let x = 0; x < COLS; x++) {
ctx.beginPath();
ctx.moveTo(x * BLOCK_SIZE, 0);
ctx.lineTo(x * BLOCK_SIZE, canvas.height);
ctx.stroke();
}
// Draw the blocks
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
if (board[y][x]) {
drawBlock(x, y, board[y][x]);
}
}
}
}
// Draw a single block
function drawBlock(x, y, colorIndex) {
const color = COLORS[colorIndex];
// Main block
ctx.fillStyle = color;
ctx.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
// Block border
ctx.strokeStyle = '#333';
ctx.lineWidth = 2;
ctx.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
// Highlight (3D effect)
ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
ctx.beginPath();
ctx.moveTo(x * BLOCK_SIZE, y * BLOCK_SIZE);
ctx.lineTo((x + 1) * BLOCK_SIZE, y * BLOCK_SIZE);
ctx.lineTo(x * BLOCK_SIZE, (y + 1) * BLOCK_SIZE);
ctx.fill();
// Shadow (3D effect)
ctx.fillStyle = 'rgba(0, 0, 0, 0.4)';
ctx.beginPath();
ctx.moveTo((x + 1) * BLOCK_SIZE, y * BLOCK_SIZE);
ctx.lineTo((x + 1) * BLOCK_SIZE, (y + 1) * BLOCK_SIZE);
ctx.lineTo(x * BLOCK_SIZE, (y + 1) * BLOCK_SIZE);
ctx.fill();
}
// Create a new piece
function createPiece(type) {
return {
type: type,
shape: SHAPES[type],
x: Math.floor(COLS / 2) - 2,
y: 0
};
}
// Get a random piece
function getRandomPiece() {
const type = Math.floor(Math.random() * 7) + 1;
return create