build a full featured tetris game. each block movement should make a sound - 8-bit note from the super-mario bros classic theme . exactly one note from the sequence (maintain an internal counter m, inc+1 on each play such that every note pitch will be the m-th note from the start of the theme sequence. reset to 0 on finish - aka Loop). single file, html.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Below is a single-file HTML implementation of a full-featured Tetris game that plays a sound (a note from the Super Mario Bros. theme melody) each time a block moves. The notes are cycled through following the melody (looped infinitely), and every move plays the m-th note from the melody (with m incremented each time a block moves).
✅ Features:
Full Tetris logic (gravity, rotations, line clears, etc.)
Keyboard controls (←, →, ↓, ↑, Space to hard-drop)
8-bit style Mario melody notes playing upon each block movement
All in one HTML file, no external libraries or audio files needed
⚠️ Note: This uses the Web Audio API to synthesize 8-bit-style notes and uses a predefined melody similar to the Super Mario Bros. theme (simplified as a list of note frequencies).
✅ ONE-FILE TETRIS + AUDIO:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tetris with Super Mario Sound</title>
<style>
body {
background: black;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
canvas {
border: 2px solid white;
}
</style>
</head>
<body>
<canvas id="game" width="240" height="400"></canvas>
<script>
const canvas = document.getElementById('game');
const context = canvas.getContext('2d');
context.scale(20, 20);
// Tetris pieces
const pieces = 'TJLOSZI';
function createPiece(type) {
switch (type) {
case 'T': return [[0,0,0],[1,1,1],[0,1,0]];
case 'O': return [[2,2],[2,2]];
case 'L': return [[0,3,0],[0,3,0],[0,3,3]];
case 'J': return [[0,4,0],[0,4,0],[4,4,0]];
case 'I': return [[0,5,0,0],[0,5,0,0],[0,5,0,0],[0,5,0,0]];
case 'S': return [[0,6,6],[6,6,0],[0,0,0]];
case 'Z': return [[7,7,0],[0,7,7],[0,0,0]];
}
}
function createMatrix(w, h) {
const matrix = [];
while (h--) {
matrix.push(new Array(w).fill(0));
}
return matrix;
}
function collide(matrix, player) {
const m = player.matrix;
const o = player.pos;
for (let y=0; y<m.length; ++y) {
for (let x=0; x<m[y].length; ++x) {
if (m[y][x] !== 0 && (matrix[y + o.y] && matrix[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
}
function merge(matrix, player) {
player.matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
matrix[y + player.pos.y][x + player.pos.x] = value;
}
});
});
}
function rotate(matrix, dir) {
for (let y=0; y<matrix.length; ++y) {
for (let x=0; x<y; ++x) {
[matrix[x][y], matrix[y][x]] = [matrix[y][x], matrix[x][y]];
}
}
if (dir > 0) matrix.forEach(row => row.reverse());
else matrix.reverse();
}
function playerDrop() {
player.pos.y++;
if (collide(arena, player)) {
player.pos.y--;
merge(arena, player);
playerReset();
arenaSweep();
}
dropCounter = 0;
playNextNote();
}
function playerMove(dir) {
player.pos.x += dir;
if (collide(arena, player)) player.pos.x -= dir;
else playNextNote();
}
function playerReset() {
const piecesType = pieces[Math.floor(Math.random() * pieces.length)];
player.matrix = createPiece(piecesType);
player.pos.y = 0;
player.pos.x = (arena[0].length / 2 | 0) - (player.matrix[0].length / 2 | 0);
if (collide(arena, player)) {
arena.forEach(row => row.fill(0));
alert('Game Over');
}
playNextNote();
}
function playerRotate(dir) {
const pos = player.pos.x;
let offset = 1;
rotate(player.matrix, dir);
while (collide(arena, player)) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.matrix[0].length) {
rotate(player.matrix, -dir);
player.pos.x = pos;
return;
}
}
playNextNote();
}
function arenaSweep() {
outer: for (let y = arena.length - 1; y >= 0; --y) {
for (let x = 0; x < arena[y].length; ++x) {
if (arena[y][x] === 0) continue outer;
}
const row = arena.splice(y, 1)[0].fill(0);
arena.unshift(row);
++y;
}
}
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);
}
});
});
}
function draw() {
context.fillStyle = '#000';
context.fillRect(0, 0, canvas.width, canvas.height);
drawMatrix(arena, {x:0, y:0});
drawMatrix(player.matrix, player.pos);
}
let dropCounter = 0;
let dropInterval = 1000;
let lastTime = 0;
function update(time = 0) {
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter > dropInterval) {
playerDrop();
}
draw();
requestAnimationFrame(update);
}
document.addEventListener('keydown', event => {
if (event.key === 'ArrowLeft') playerMove(-1);
else if (event.key === 'ArrowRight') playerMove(1);
else if (event.key === 'ArrowDown') playerDrop();
else if (event.key === 'ArrowUp') playerRotate(1);
else if (event.key === ' ') { // Hard drop
while (!collide(arena, player)) {
player.pos.y++;
}
player.pos.y--;
merge(arena, player);
playerReset();
arenaSweep();
dropCounter = 0;
playNextNote();
}
});
// Colors
const colors = [
null,
'#FF0D72',
'#0DC2FF',
'#0DFF72',
'#F538FF',
'#FF8E0D',
'#FFE138',
'#3877FF'
];
// Arena and Player
const arena = createMatrix(12, 20);
const player = {
pos: {x:0, y:0},
matrix: null
};
playerReset();
update();
// -------------------- 8-BIT MARIO NOTE PLAYER --------------------
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();
// Simplified Mario melody (first 16 note pitches)
const marioMelody = [
659.25, 659.25, 0, 659.25, 0, 523.25, 659.25, 0, 784.00, 0, 392.00,
523.25, 0, 392.00, 440.00, 349.23, 392.00
];
let m = 0;
function playNextNote() {
const freq = marioMelody[m];
if (freq !== 0) {
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = 'square';
osc.frequency.value = freq;
gain.gain.value = 0.1;
osc.connect(gain);
gain.connect(audioCtx.destination);
osc.start();
osc.stop(audioCtx.currentTime + 0.2);
}
m = (m + 1) % marioMelody.length;
}
</script>
</body>
</html>
✅ Controls:
Left/Right Arrow to move
Down Arrow to soft drop
Up Arrow to rotate
Space to hard drop
🔊 Sound:
Each movement or drop plays the next note from the simplified Super Mario Bros. melody using a square wave oscillator – mimicking 8-bit sound style.
Let me know if you'd like to add full Mario theme or enhance with better pixel styling & music syncing!