NES TETRIS
by PixelStar14783 lines26.4 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NES TETRIS</title>
<!-- Fallback stack ensures crisp rendering even if remote retro font fails to load -->
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Courier New', Courier, monospace, sans-serif;
font-weight: bold;
image-rendering: pixelated;
user-select: none;
-webkit-user-select: none;
}
body {
background-color: #000000;
color: #ffffff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
overflow: hidden;
touch-action: manipulation;
}
.arcade-cabinet {
background: #101010;
border: 4px solid #525252;
box-shadow: 0 0 0 4px #000, 0 0 15px rgba(255, 255, 255, 0.1);
padding: 16px;
display: flex;
flex-direction: column;
align-items: center;
max-width: 460px;
width: 100%;
}
.header-box {
display: flex;
justify-content: space-between;
width: 100%;
margin-bottom: 10px;
font-size: 0.75rem;
color: #c0c0c0;
letter-spacing: 1px;
}
.game-container {
display: flex;
gap: 12px;
position: relative;
background-color: #000;
border: 4px solid #3c3c3c;
padding: 6px;
}
canvas {
background-color: #000;
display: block;
}
.side-panel {
display: flex;
flex-direction: column;
justify-content: flex-start;
gap: 12px;
width: 130px;
}
.stats-box {
background: #000;
border: 2px solid #3c3c3c;
padding: 8px;
text-align: left;
}
.stats-box h3 {
font-size: 0.6rem;
color: #787878;
margin-bottom: 4px;
}
.stats-box p {
font-size: 0.85rem;
color: #fcbc00;
text-align: right;
letter-spacing: 2px;
text-shadow: 2px 2px #3c1400;
}
.preview-container {
background: #000;
border: 2px solid #3c3c3c;
padding: 8px;
display: flex;
flex-direction: column;
align-items: center;
height: 104px;
}
.preview-container h3 {
font-size: 0.6rem;
color: #787878;
margin-bottom: 4px;
align-self: flex-start;
}
#nextCanvas {
background-color: #000;
}
.controls-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 14px;
width: 100%;
}
.ctrl-btn {
background: #202020;
border: 2px solid #525252;
color: #fff;
padding: 12px 0;
font-size: 0.75rem;
font-weight: bold;
text-align: center;
}
.ctrl-btn.active-touch {
background: #fcbc00;
color: #000;
border-color: #fcbc00;
}
.ctrl-btn.wide {
grid-column: span 3;
background: #0078f8;
border-color: #00fcfc;
}
.ctrl-btn.wide.active-touch {
background: #00fcfc;
color: #000;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.95);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
z-index: 10;
}
.overlay h2 {
font-size: 1rem;
color: #00fcfc;
margin-bottom: 16px;
line-height: 1.6;
text-shadow: 3px 3px #000088;
}
.overlay p {
font-size: 0.65rem;
color: #c0c0c0;
margin-bottom: 16px;
padding: 0 10px;
line-height: 1.8;
}
.menu-options {
display: flex;
flex-direction: column;
gap: 12px;
margin-bottom: 20px;
width: 85%;
}
.menu-row {
background: #000;
color: #787878;
border: 2px solid #3c3c3c;
padding: 10px;
font-size: 0.65rem;
text-align: left;
display: flex;
justify-content: space-between;
}
.menu-row.selected {
border-color: #fcbc00;
color: #fcbc00;
background: #1a1a00;
}
.footer-credits {
margin-top: 12px;
font-size: 0.7rem;
color: #fcbc00;
text-align: center;
letter-spacing: 1px;
text-shadow: 2px 2px #521800;
}
.hidden {
display: none !important;
}
</style>
</head>
<body>
<div class="arcade-cabinet">
<div class="header-box">
<span>NES TETRIS</span>
<span>© 1989 NINTENDO</span>
</div>
<div class="game-container">
<div style="position: relative;">
<canvas id="tetris" width="200" height="400"></canvas>
<!-- Main Menu / Overlays -->
<div id="menuOverlay" class="overlay">
<h2 style="color: #fcbc00;">NES TETRIS</h2>
<div class="menu-options">
<div class="menu-row selected" id="rowMode">
<span>MODE:</span> <span id="valMode" style="color:#00fcfc;">TYPE-A</span>
</div>
<div class="menu-row" id="rowMusic">
<span>AUDIO/MUSIC:</span> <span id="valMusic" style="color:#00f800;">ON</span>
</div>
</div>
<p>PRESS [↑/↓] TO SELECT<br>PRESS [ENTER / SPACE] TO START</p>
</div>
<div id="gameOverOverlay" class="overlay hidden">
<h2 style="color: #f83800;">GAME OVER</h2>
<p id="finalScoreText">SCORE: 000000</p>
<p style="color: #fcbc00; font-size: 0.55rem;">PRESS START TO RETURN</p>
</div>
</div>
<div class="side-panel">
<div class="stats-box">
<h3>SCORE</h3>
<p id="scoreVal">000000</p>
</div>
<div class="stats-box">
<h3>LINES</h3>
<p id="linesVal">000</p>
</div>
<div class="stats-box">
<h3>LEVEL</h3>
<p id="levelVal">01</p>
</div>
<div class="preview-container">
<h3>NEXT</h3>
<canvas id="nextCanvas" width="80" height="64"></canvas>
</div>
</div>
</div>
<!-- Onscreen Touch Controls -->
<div class="controls-grid">
<div class="ctrl-btn" id="btnLeft">←</div>
<div class="ctrl-btn" id="btnRotate">↻ (SPACE)</div>
<div class="ctrl-btn" id="btnRight">→</div>
<div class="ctrl-btn" id="btnDown">↓ SOFT</div>
<div class="ctrl-btn wide" id="btnRotateWide">ROTATE PIECE [ SPACE ]</div>
</div>
<div class="footer-credits">
PORT BY BLUEDEV - NOV 4, 2026
</div>
</div>
<script>
// Web Audio API Sound Synthesizer & Music Engine
let audioCtx = null;
let audioEnabled = true;
let musicInterval = null;
function initAudio() {
if (!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
}
// Sound effects boosted by +60% loudness factor (matching the requested ~80% loud profile)
function playSound(type) {
if (!audioEnabled || !audioCtx) return;
try {
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.connect(gain);
gain.connect(audioCtx.destination);
const now = audioCtx.currentTime;
if (type === 'move') {
osc.type = 'square';
osc.frequency.setValueAtTime(150, now);
osc.frequency.exponentialRampToValueAtTime(100, now + 0.05);
gain.gain.setValueAtTime(0.22, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.05);
osc.start(now);
osc.stop(now + 0.05);
} else if (type === 'rotate') {
osc.type = 'triangle';
osc.frequency.setValueAtTime(300, now);
osc.frequency.exponentialRampToValueAtTime(500, now + 0.08);
gain.gain.setValueAtTime(0.25, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.08);
osc.start(now);
osc.stop(now + 0.08);
} else if (type === 'drop') {
osc.type = 'square';
osc.frequency.setValueAtTime(140, now);
osc.frequency.exponentialRampToValueAtTime(30, now + 0.1);
gain.gain.setValueAtTime(0.32, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.1);
osc.start(now);
osc.stop(now + 0.1);
} else if (type === 'clear') {
osc.type = 'square';
osc.frequency.setValueAtTime(523.25, now);
osc.frequency.setValueAtTime(659.25, now + 0.08);
osc.frequency.setValueAtTime(783.99, now + 0.16);
gain.gain.setValueAtTime(0.30, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.35);
osc.start(now);
osc.stop(now + 0.35);
} else if (type === 'gameover') {
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(220, now);
osc.frequency.linearRampToValueAtTime(70, now + 0.6);
gain.gain.setValueAtTime(0.32, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.6);
osc.start(now);
osc.stop(now + 0.6);
} else if (type === 'menu') {
osc.type = 'sine';
osc.frequency.setValueAtTime(440, now);
osc.frequency.exponentialRampToValueAtTime(660, now + 0.06);
gain.gain.setValueAtTime(0.25, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.06);
osc.start(now);
osc.stop(now + 0.06);
}
} catch(e) {}
}
// Varied multi-phrase background music track (Tetris Theme Variant) to avoid monotony
const melody = [
// A Phrase
{f: 659.25, d: 240}, {f: 493.88, d: 120}, {f: 523.25, d: 120}, {f: 587.33, d: 240}, {f: 523.25, d: 120}, {f: 493.88, d: 120},
{f: 440.00, d: 240}, {f: 440.00, d: 120}, {f: 523.25, d: 120}, {f: 659.25, d: 240}, {f: 587.33, d: 120}, {f: 523.25, d: 120},
{f: 493.88, d: 360}, {f: 523.25, d: 120}, {f: 587.33, d: 240}, {f: 659.25, d: 240},
{f: 523.25, d: 240}, {f: 440.00, d: 240}, {f: 440.00, d: 240}, {f: 0, d: 180},
// B Phrase (Dynamic Counter-Melody Variation)
{f: 587.33, d: 320}, {f: 698.46, d: 120}, {f: 880.00, d: 240}, {f: 783.99, d: 120}, {f: 659.25, d: 120},
{f: 523.25, d: 320}, {f: 587.33, d: 120}, {f: 659.25, d: 240}, {f: 523.25, d: 240},
{f: 493.88, d: 240}, {f: 587.33, d: 240}, {f: 440.00, d: 360}, {f: 0, d: 240}
];
let noteIndex = 0;
function startBackgroundMusic() {
if (!audioEnabled || musicInterval) return;
musicInterval = setInterval(() => {
if (!audioEnabled || !gameStarted || gameOver || !audioCtx) return;
const note = melody[noteIndex];
noteIndex = (noteIndex + 1) % melody.length;
if (note.f > 0) {
try {
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = 'triangle';
osc.frequency.setValueAtTime(note.f, audioCtx.currentTime);
gain.gain.setValueAtTime(0.08, audioCtx.currentTime); // Louder background accompaniment
gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + (note.d / 1000) * 0.9);
osc.connect(gain);
gain.connect(audioCtx.destination);
osc.start();
osc.stop(audioCtx.currentTime + note.d / 1000);
} catch(e) {}
}
}, 175);
}
function stopBackgroundMusic() {
if (musicInterval) {
clearInterval(musicInterval);
musicInterval = null;
}
noteIndex = 0;
}
const canvas = document.getElementById('tetris');
const ctx = canvas.getContext('2d');
const nextCanvas = document.getElementById('nextCanvas');
const nextCtx = nextCanvas.getContext('2d');
const BLOCK_SIZE = 20;
const COLS = 10;
const ROWS = 20;
ctx.scale(BLOCK_SIZE, BLOCK_SIZE);
nextCtx.scale(BLOCK_SIZE, BLOCK_SIZE);
const COLORS = [
null,
'#00fcfc', // Cyan (I)
'#0000f8', // Blue (J)
'#f83800', // Orange/Red (L)
'#fcbc00', // Yellow (O)
'#00f800', // Green (S)
'#7c08f8', // Purple (T)
'#f80000' // Red (Z)
];
const SHAPES = [
null,
[[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]], // I
[[2, 0, 0], [2, 2, 2], [0, 0, 0]], // J
[[0, 0, 3], [3, 3, 3], [0, 0, 0]], // L
[[4, 4], [4, 4]], // O
[[0, 5, 5], [5, 5, 0], [0, 0, 0]], // S
[[0, 6, 0], [6, 6, 6], [0, 0, 0]], // T
[[7, 7, 0], [0, 7, 7], [0, 0, 0]] // Z
];
const arena = createMatrix(COLS, ROWS);
const player = {
pos: {x: 0, y: 0},
matrix: null,
score: 0,
lines: 0,
level: 1
};
let nextPieceType = 1;
let dropCounter = 0;
let dropInterval = 1000;
let normalDropInterval = 1000;
let softDropping = false;
let lastTime = 0;
let gameOver = false;
let gameStarted = false;
let menuSelection = 0;
let selectedMode = 'A';
function updateMenuDisplay() {
const rowMode = document.getElementById('rowMode');
const rowMusic = document.getElementById('rowMusic');
const valMode = document.getElementById('valMode');
const valMusic = document.getElementById('valMusic');
valMode.innerText = selectedMode === 'A' ? 'TYPE-A' : 'TYPE-B';
valMusic.innerText = audioEnabled ? 'ON' : 'OFF';
if (menuSelection === 0) {
rowMode.classList.add('selected');
rowMusic.classList.remove('selected');
} else {
rowMode.classList.remove('selected');
rowMusic.classList.add('selected');
}
}
function createMatrix(w, h) {
const matrix = [];
while (h--) {
matrix.push(new Array(w).fill(0));
}
return matrix;
}
function drawMatrix(matrix, offset, context = ctx) {
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);
context.fillStyle = 'rgba(255,255,255,0.3)';
context.fillRect(x + offset.x, y + offset.y, 1, 0.15);
context.fillRect(x + offset.x, y + offset.y, 0.15, 1);
context.fillStyle = 'rgba(0,0,0,0.4)';
context.fillRect(x + offset.x + 0.85, y + offset.y, 0.15, 1);
context.fillRect(x + offset.x, y + offset.y + 0.85, 1, 0.15);
}
});
});
}
function draw() {
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawMatrix(arena, {x: 0, y: 0});
drawMatrix(player.matrix, player.pos);
// Perfectly centered unclipped Next Piece preview canvas rendering
nextCtx.fillStyle = '#000000';
nextCtx.fillRect(0, 0, nextCanvas.width, nextCanvas.height);
const nextMatrix = SHAPES[nextPieceType];
const offsetX = (4 - nextMatrix[0].length) / 2;
const offsetY = (3.2 - nextMatrix.length) / 2;
drawMatrix(nextMatrix, {x: offsetX, y: offsetY}, nextCtx);
}
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) {
const len = matrix.length;
for (let y = 0; y < len; ++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 playerRotate(dir) {
const posX = 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 = posX;
return;
}
}
playSound('rotate');
}
function collide(arena, 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 &&
(arena[y + o.y] && arena[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
}
function playerDrop() {
player.pos.y++;
if (collide(arena, player)) {
player.pos.y--;
merge(arena, player);
playerReset();
arenaSweep();
updateScore();
playSound('drop');
}
dropCounter = 0;
}
function playerMove(dir) {
player.pos.x += dir;
if (collide(arena, player)) {
player.pos.x -= dir;
} else {
playSound('move');
}
}
function playerReset() {
player.matrix = SHAPES[nextPieceType];
nextPieceType = Math.floor(Math.random() * (SHAPES.length - 1)) + 1;
player.pos.y = 0;
player.pos.x = Math.floor((arena[0].length - player.matrix[0].length) / 2);
if (collide(arena, player)) {
gameOver = true;
stopBackgroundMusic();
playSound('gameover');
showGameOver();
}
}
function arenaSweep() {
let rowCount = 0;
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;
rowCount++;
}
if (rowCount > 0) {
const baseScores = [0, 40, 100, 300, 1200];
player.score += baseScores[rowCount] * player.level;
player.lines += rowCount;
playSound('clear');
}
player.level = Math.floor(player.lines / 10) + (selectedMode === 'B' ? 5 : 1);
normalDropInterval = Math.max(50, 1000 - (player.level - 1) * 85);
}
function updateScore() {
document.getElementById('scoreVal').innerText = String(player.score).padStart(6, '0');
document.getElementById('linesVal').innerText = String(player.lines).padStart(3, '0');
document.getElementById('levelVal').innerText = String(player.level).padStart(2, '0');
}
function resetGame() {
arena.forEach(row => row.fill(0));
player.score = 0;
player.lines = 0;
player.level = selectedMode === 'B' ? 5 : 1;
normalDropInterval = Math.max(50, 1000 - (player.level - 1) * 85);
dropInterval = normalDropInterval;
nextPieceType = Math.floor(Math.random() * (SHAPES.length - 1)) + 1;
updateScore();
playerReset();
gameOver = false;
}
function showGameOver() {
document.getElementById('finalScoreText').innerText = `SCORE: ${String(player.score).padStart(6, '0')}`;
document.getElementById('gameOverOverlay').classList.remove('hidden');
}
function update(time = 0) {
if (!gameStarted || gameOver) return;
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter > dropInterval) {
playerDrop();
}
draw();
requestAnimationFrame(update);
}
// Keyboard Controls
document.addEventListener('keydown', event => {
initAudio();
const menuOverlay = document.getElementById('menuOverlay');
const gameOverOverlay = document.getElementById('gameOverOverlay');
const isMenuVisible = !menuOverlay.classList.contains('hidden');
const isGameOverVisible = !gameOverOverlay.classList.contains('hidden');
if (isMenuVisible) {
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
menuSelection = menuSelection === 0 ? 1 : 0;
playSound('menu');
updateMenuDisplay();
} else if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
if (menuSelection === 0) {
selectedMode = selectedMode === 'A' ? 'B' : 'A';
} else {
audioEnabled = !audioEnabled;
}
playSound('menu');
updateMenuDisplay();
} else if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
playSound('rotate');
menuOverlay.classList.add('hidden');
resetGame();
gameStarted = true;
lastTime = performance.now();
requestAnimationFrame(update);
if (audioEnabled) startBackgroundMusic();
}
return;
}
if (isGameOverVisible) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
gameOverOverlay.classList.add('hidden');
menuOverlay.classList.remove('hidden');
gameStarted = false;
}
return;
}
if (!gameStarted || gameOver) return;
if (event.key === 'ArrowLeft') {
playerMove(-1);
} else if (event.key === 'ArrowRight') {
playerMove(1);
} else if (event.key === 'ArrowDown') {
if (!softDropping) {
softDropping = true;
dropInterval = 40;
}
} else if (event.key === 'ArrowUp' || event.key === 'x' || event.key === 'z') {
playerRotate(1);
} else if (event.key === ' ') {
event.preventDefault();
playerRotate(1);
}
});
document.addEventListener('keyup', event => {
if (event.key === 'ArrowDown') {
softDropping = false;
dropInterval = normalDropInterval;
}
});
// Touch Controls
function bindTouchButton(id, actionPress, actionRelease) {
const btn = document.getElementById(id);
if (!btn) return;
btn.addEventListener('touchstart', (e) => {
e.preventDefault();
initAudio();
btn.classList.add('active-touch');
if (actionPress) actionPress();
}, {passive: false});
btn.addEventListener('touchend', (e) => {
e.preventDefault();
btn.classList.remove('active-touch');
if (actionRelease) actionRelease();
}, {passive: false});
btn.addEventListener('pointerdown', (e) => {
initAudio();
btn.classList.add('active-touch');
if (actionPress) actionPress();
});
btn.addEventListener('pointerup', (e) => {
btn.classList.remove('active-touch');
if (actionRelease) actionRelease();
});
}
bindTouchButton('btnLeft', () => { if(gameStarted && !gameOver) playerMove(-1); });
bindTouchButton('btnRight', () => { if(gameStarted && !gameOver) playerMove(1); });
bindTouchButton('btnRotate', () => { if(gameStarted && !gameOver) playerRotate(1); });
bindTouchButton('btnRotateWide', () => { if(gameStarted && !gameOver) playerRotate(1); });
bindTouchButton('btnDown',
() => { if(gameStarted && !gameOver) { softDropping = true; dropInterval = 40; } },
() => { softDropping = false; dropInterval = normalDropInterval; }
);
// Initialize Menu State
updateMenuDisplay();
</script>
</body>
</html>Game Source: NES TETRIS
Creator: PixelStar14
Libraries: none
Complexity: complex (783 lines, 26.4 KB)
The full source code is displayed above on this page.
Remix Instructions
To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: tetris-arcadelab-pixelstar14" to link back to the original. Then publish at arcadelab.ai/publish.