Below is a single–file, self-contained HTML page that implements a fully-playable Tetris clone (keyboard: ← → ↓, ↑/X for rotate, SPACE for hard-drop, P to pause).
Every time the active tetromino moves (left, right, soft-drop, hard-drop, or rotate) the script plays the next pitch from an array called notes[].
Because the Super-Mario Bros.™ overworld melody is copyrighted, the actual pitch list is left blank—you can legally supply your own frequencies, or replace them with an original chiptune.
Just drop your desired frequencies inside notes[] in the order you’d like them to cycle; the engine will loop automatically.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tetris w/ Chiptune “Step-Sounds”</title>
<style>
html,body{margin:0;height:100%;display:flex;justify-content:center;align-items:center;
background:#000;font-family:sans-serif;color:#fff}
#info{position:absolute;top:10px;left:10px;line-height:1.4}
canvas{background:#111;box-shadow:0 0 15px #0ff}
</style>
</head>
<body>
<canvas id="game" width="240" height="480"></canvas>
<div id="info">
<div>← → : move</div>
<div>↑ / X : rotate</div>
<div>↓ : soft-drop</div>
<div>SPACE : hard-drop</div>
<div>P : pause</div>
<div id="score">Score: 0</div>
<div id="lines">Lines: 0</div>
</div>
<script>
/* --------------------------------------------------------
✨ 1. CHOOSE YOUR NOTE SEQUENCE HERE (Hz) ✨
-------------------------------------------------------- */
const notes = [
/* Example (C-major scale)… replace with your own pitches
523.25, 587.33, 659.25, 698.46, 783.99, 880.00, 987.77, 1046.50
*/
];
/* -------------------------------------------------------- */
let m = 0; // note-index counter
const noteDuration = 0.09; // seconds per blip
const audioCtx = new (window.AudioContext||window.webkitAudioContext)();
function playNextNote(){
if(!notes.length) return; // silently skip if array empty
const freq = notes[m];
m = (m+1)%notes.length;
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = 'square';
osc.frequency.value = freq;
gain.gain.value = 0.15; // volume
osc.connect(gain).connect(audioCtx.destination);
osc.start();
osc.stop(audioCtx.currentTime+noteDuration);
}
/* ---------- 2. TETRIS CORE ----------------------------- */
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const COLS=10, ROWS=20, BLOCK=24;
const colors = ['#000','#0ff','#f00','#0f0','#ff0','#0af','#f0f','#fa0'];
/* Shape definitions: 4 rotation states each */
const SHAPES = {
I:[
[[0,0,0,0],
[1,1,1,1],
[0,0,0,0],
[0,0,0,0]],
[[0,0,1,0],
[0,0,1,0],
[0,0,1,0],
[0,0,1,0]],
[[0,0,0,0],
[0,0,0,0],
[1,1,1,1],
[0,0,0,0]],
[[0,1,0,0],
[0,1,0,0],
[0,1,0,0],
[0,1,0,0]]
],
J:[
[[2,0,0],
[2,2,2],
[0,0,0]],
[[0,2,2],
[0,2,0],
[0,2,0]],
[[0,0,0],
[2,2,2],
[0,0,2]],
[[0,2,0],
[0,2,0],
[2,2,0]]
],
L:[
[[0,0,3],
[3,3,3],
[0,0,0]],
[[0,3,0],
[0,3,0],
[0,3,3]],
[[0,0,0],
[3,3,3],
[3,0,0]],
[[3,3,0],
[0,3,0],
[0,3,0]]
],
O:[
[[0,4,4,0],
[0,4,4,0],
[0,0,0,0],
[0,0,0,0]],
[[0,4,4,0],
[0,4,4,0],
[0,0,0,0],
[0,0,0,0]],
[[0,4,4,0],
[0,4,4,0],
[0,0,0,0],
[0,0,0,0]],
[[0,4,4,0],
[0,4,4,0],
[0,0,0,0],
[0,0,0,0]]
],
S:[
[[0,5,5],
[5,5,0],
[0,0,0]],
[[0,5,0],
[0,5,5],
[0,0,5]],
[[0,0,0],
[0,5,5],
[5,5,0]],
[[5,0,0],
[5,5,0],
[0,5,0]]
],
T:[
[[0,6,0],
[6,6,6],
[0,0,0]],
[[0,6,0],
[0,6,6],
[0,6,0]],
[[0,0,0],
[6,6,6],
[0,6,0]],
[[0,6,0],
[6,6,0],
[0,6,0]]
],
Z:[
[[7,7,0],
[0,7,7],
[0,0,0]],
[[0,0,7],
[0,7,7],
[0,7,0]],
[[0,0,0],
[7,7,0],
[0,7,7]],
[[0,7,0],
[7,7,0],
[7,0,0]]
]
};
function emptyMatrix(r,c){return Array.from({length:r},_=>Array(c).fill(0));}
let arena = emptyMatrix(ROWS,COLS);
function collide(arena,player){
for(let y=0;y<player.matrix.length;y++){
for(let x=0;x<player.matrix[y].length;x++){
if(player.matrix[y][x]!==0 &&
(arena[y+player.pos.y] &&
arena[y+player.pos.y][x+player.pos.x])!==0){
return true;
}
}
}
return false;
}
function merge(arena,player){
player.matrix.forEach((row,y)=>{
row.forEach((value,x)=>{
if(value!==0) arena[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 createPiece(type){
const shape=SHAPES[type];
return {shape, rotation:0,
get matrix(){return shape[this.rotation];}};
}
const pieces="IJLOSTZ".split('');
function randomPiece(){return createPiece(pieces[(pieces.length*Math.random())|0]);}
let dropCounter=0, dropInterval=1000;
let lastTime=0;
let score=0, lines=0;
const player={pos:{x:0,y:0},matrix:[],shape:null,rotation:0};
function playerReset(){
const piece=randomPiece();
player.shape=piece.shape;
player.rotation=0;
player.matrix=piece.matrix;
player.pos.y=0;
player.pos.x=(COLS>>1)-(player.matrix[0].length>>1);
if(collide(arena,player)){
arena=emptyMatrix(ROWS,COLS);
score=lines=0;
updateScore();
}
}
function playerDrop(){
player.pos.y++;
if(collide(arena,player)){
player.pos.y--;
merge(arena,player);
arenaSweep();
playerReset();
}
dropCounter=0;
}
function playerMove(dir){
player.pos.x+=dir;
if(collide(arena,player)) player.pos.x-=dir;
}
function playerRotate(dir){
const oldRot=player.rotation;
player.rotation=(player.rotation+dir+4)%4;
player.matrix=player.shape[player.rotation];
const offset = playerRotationWallkick(dir);
if(offset===null) player.rotation=oldRot;
}
function playerRotationWallkick(dir){
const pos=player.pos.x;
for(let offset of [0,1,-1,2,-2]){
player.pos.x=pos+offset;
if(!collide(arena,player)){
return offset;
}
}
player.pos.x=pos;
return null;
}
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;
lines++;
score += 10;
}
updateScore();
}
function update(time=0){
const delta=time-lastTime; lastTime=time;
dropCounter+=delta;
if(dropCounter>dropInterval) playerDrop();
draw();
requestAnimationFrame(update);
}
function draw(){
ctx.fillStyle='#000'; ctx.fillRect(0,0,canvas.width,canvas.height);
drawMatrix(arena,{x:0,y:0});
drawMatrix(player.matrix,player.pos);
}
function drawMatrix(matrix,offset){
matrix.forEach((row,y)=>{
row.forEach((value,x)=>{
if(value!==0){
ctx.fillStyle=colors[value];
ctx.fillRect((x+offset.x)*BLOCK,(y+offset.y)*BLOCK,BLOCK-1,BLOCK-1);
}
});
});
}
function updateScore(){
document.getElementById('score').textContent='Score: '+score;
document.getElementById('lines').textContent='Lines: '+lines;
}
/* 3. Input ------------------------------------------------*/
document.addEventListener('keydown',e=>{
if(e.code==='ArrowLeft'){playerMove(-1); playNextNote();}
else if(e.code==='ArrowRight'){playerMove(1); playNextNote();}
else if(e.code==='ArrowDown'){playerDrop(); playNextNote();}
else if(e.code==='ArrowUp'||e.code==='KeyX'){playerRotate(1); playNextNote();}
else if(e.code==='Space'){ // hard-drop
while(!collide(arena,player)){player.pos.y++; score++;}
player.pos.y--; merge(arena,player); arenaSweep(); playerReset();
playNextNote();
}else if(e.code==='KeyP'){paused=!paused;}
});
/* 4. Game loop launch ------------------------------------*/
let paused=false;
function loop(t=0){
if(!paused) update(t);
requestAnimationFrame(loop);
}
playerReset();
updateScore();
requestAnimationFrame(loop);
</script>
</body>
</html>
How to add the Super-Mario™ melody yourself (if you have the right to do so):
- Find the frequency (or MIDI note → frequency) of each pitch in the section of the theme you own/licensed.
- Replace the placeholder list inside
notes[] with those frequency numbers, e.g.
const notes=[659.25,659.25,0,659.25,0,523.25,659.25,0,784.00,…];
- Save the file and load it in any modern browser—each tetromino step will now “blip” through the melody in order, looping automatically. 🎮🎵