Below is a single-file HTML implementation of a full-featured Tetris game with 8-bit sound effects from the Super Mario Bros. theme. Each block movement plays the next note in the sequence, looping back to the start after the last note.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device.width, initial-scale=1.0">
<title>Tetris with Mario Sounds</title>
<style>
body { display: flex; flex-direction: column; align-items: center; font-family: Arial, sans-serif; }
canvas { border: 2px solid #000; background: #000; }
#score { color: #fff; font-size: 24px; margin: 10px; }
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="tetris" width="300" height="600"></canvas>
<script>
const canvas = document.getElementById('tetris');
const ctx = canvas.getContext('2d');
const SCALE = 30;
const ROWS = canvas.height / SCALE;
const COLS = canvas.width / SCALE;
let board = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
let score = 0;
let noteIndex = 0;
const marioTheme = [60, 62, 64, 65, 67, 69, 71, 72, 74]; // C4 to E5 in MIDI notes
const tetrominoes = {
I: [[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
J: [[2, 0, 0], [2, 2, 2], [0, 0, 2]],
L: [[0, 0, 3], [3, 3, 3], [0, 0, 0]],
O: [[4, 4], [4, 4]],
S: [[0, 5, 5], [5, 5, 0], [0, 0, 0]],
Z: [[6, 6, 0], [0, 6, 6], [0, 0, 0]],
T: [[0, 7, 0], [7, 7, 7], [0, 0, 0]],
};
let currentPiece = null;
let currentX = 0, currentY = 0;
let gameInterval = null;
let dropInterval = 1000;
let gameOver = false;
const notes = {
60: 'C4', 62: 'D4', 64: 'E4', 65: 'F4', 67: 'G4', 69: 'A4', 71: 'B4', 72: 'C5', 74: 'D5'
};
const osc = typeof window.OfflineAudioContext !== 'undefined' ? new OfflineAudioContext(1, 2, 44100) : null;
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function playNote(midiNote) {
if (!audioCtx) return;
const osc = audioCtx.createOscillator();
const envelope = audioCtx.createGain();
osc.connect(envelope);
envelope.connect(audioCtx.destination);
envelope.gain.setValueAtTime(0.5, audioCtx.currentTime);
envelope.gain.exponentialRampToValueAtTime(1e-5, audioCtx.currentTime + 0.2);
osc.frequency.setValueAtTime(440 * Math.pow(2, (midiNote - 69) / 12), audioCtx.currentTime);
osc.start();
osc.stop(audioCtx.currentTime + 0.2);
}
function drawBoard() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
if (board[row][col]) {
ctx.fillStyle = getColor(board[row][col]);
ctx.fillRect(col * SCALE, row * SCALE, SCALE, SCALE);
}
}
}
if (currentPiece) drawPiece(currentPiece, currentX, currentY);
}
function getColor(type) {
const colors = ['#000', '#00f', '#00f', '#f00', '#ff0', '#0f0', '#f0f', '#0ff', '#888'];
return colors[type];
}
function drawPiece(piece, x, y) {
for (let row = 0; row < piece.length; row++) {
for (let col = 0; col < piece[row].length; col++) {
if (piece[row][col]) {
ctx.fillStyle = getColor(piece[row][col]);
ctx.fillRect((x + col) * SCALE, (y + row) * SCALE, SCALE, SCALE);
}
}
}
}
function collide(board, piece, offsetX, offsetY) {
for (let row = 0; row < piece.length; row++) {
for (let col = 0; col < piece[row].length; col++) {
if (piece[row][col] && (board[offsetY + row] && board[offsetY + row][offsetX + col]) !== 0) {
return true;
}
}
}
return false;
}
function merge(board, piece, offsetX, offsetY) {
for (let row = 0; row < piece.length; row++) {
for (let col = 0; col < piece[row].length; col++) {
if (piece[row][col]) {
board[offsetY + row][offsetX + col] = piece[row][col];
}
}
}
}
function clearLines() {
let linesCleared = 0;
for (let row = ROWS - 1; row >= 0; row--) {
if (board[row].every(cell => cell !== 0)) {
board.splice(row, 1);
board.unshift(Array(COLS).fill(0));
linesCleared++;
}
}
score += linesCleared * 100;
document.getElementById('score').textContent = `Score: ${score}`;
}
function newPiece() {
const shapes = 'IJLOSTZ';
const type = shapes[Math.floor(Math.random() * shapes.length)];
currentPiece = tetrominoes[type].map(row => row.slice());
currentX = Math.floor(COLS / 2) - Math.floor(currentPiece[0].length / 2);
currentY = 0;
if (collide(board, currentPiece, currentX, currentY)) {
gameOver = true;
clearInterval(gameInterval);
alert('Game Over!');
}
}
function rotate(piece) {
return piece[0].map((_, colIndex) => piece.map(row => row[colIndex]).reverse());
}
function move(dx) {
if (!gameOver && !collide(board, currentPiece, currentX + dx, currentY)) {
currentX += dx;
playNote(marioTheme[noteIndex % marioTheme.length]);
noteIndex++;
drawBoard();
}
}
function rotatePiece() {
const rotated = rotate(currentPiece);
if (!gameOver && !collide(board, rotated, currentX, currentY)) {
currentPiece = rotated;
playNote(marioTheme[noteIndex % marioTheme.length]);
noteIndex++;
drawBoard();
}
}
function drop() {
if (!gameOver) {
if (!collide(board, currentPiece, currentX, currentY + 1)) {
currentY++;
playNote(marioTheme[noteIndex % marioTheme.length]);
noteIndex++;
} else {
merge(board, currentPiece, currentX, currentY);
clearLines();
newPiece();
}
drawBoard();
}
}
document.addEventListener('keydown', (e) => {
if (gameOver) return;
switch (e.key) {
case 'ArrowLeft': move(-1); break;
case 'ArrowRight': move(1); break;
case 'ArrowDown': drop(); break;
case 'ArrowUp': rotatePiece(); break;
}
});
function startGame() {
board = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
score = 0;
noteIndex = 0;
document.getElementById('score').textContent = `Score: ${score}`;
gameOver = false;
newPiece();
drawBoard();
gameInterval = setInterval(drop, dropInterval);
}
startGame();
</script>
</body>
</html>
Features:
- Tetris Gameplay: Full Tetris mechanics including movement, rotation, and line clearing.
- Mario Sounds: Each block movement plays the next note from the Super Mario Bros. theme.
- Score Tracking: Score increases based on lines cleared.
- Game Over: Detects when the game is over and stops the game loop.
- Responsive Controls: Arrow keys for movement and rotation.
Notes:
- The
marioTheme array contains MIDI note values corresponding to the Super Mario Bros. theme.
- The
noteIndex counter ensures the correct note is played sequentially, looping back to the start after the last note.
- The game uses a simple oscillator to generate 8-bit-like sounds.
- The game board is drawn on a HTML5 canvas, with each block scaled to 30x30 pixels.
Copy and paste this code into an .html file and open it in a browser to play the game.