907 lines
27 KiB
HTML
907 lines
27 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="fr" data-theme="dark">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
<title>Tetris - AP Manageur Arcade</title>
|
|
<style>
|
|
:root, html[data-theme="dark"] {
|
|
--bg: #071326;
|
|
--card-bg: #0d1d36;
|
|
--panel-box: #132746;
|
|
--border: #223c68;
|
|
--text: #eef4ff;
|
|
--muted: #9aa8c5;
|
|
--blue: #5ca7ff;
|
|
--blue-btn-text: #071326;
|
|
--board-bg: #050d1a;
|
|
--grid-line: #0e203c;
|
|
}
|
|
html[data-theme="light"] {
|
|
--bg: #f2f5fa;
|
|
--card-bg: #ffffff;
|
|
--panel-box: #e9eef7;
|
|
--border: #d2dcec;
|
|
--text: #15203a;
|
|
--muted: #5b6b8c;
|
|
--blue: #1e6ef0;
|
|
--blue-btn-text: #ffffff;
|
|
--board-bg: #f8fafc;
|
|
--grid-line: #e2e8f0;
|
|
}
|
|
html[data-theme="winxp"] {
|
|
--bg: #ece9d8;
|
|
--card-bg: #ece9d8;
|
|
--panel-box: #ffffff;
|
|
--border: #7f9db9;
|
|
--text: #000000;
|
|
--muted: #444444;
|
|
--blue: #0055ea;
|
|
--blue-btn-text: #ffffff;
|
|
--board-bg: #ffffff;
|
|
--grid-line: #dcdcdc;
|
|
font-family: 'Tahoma', sans-serif !important;
|
|
}
|
|
html[data-theme="macos"][data-macos-mode="light"],
|
|
html[data-theme="macos"]:not([data-macos-mode="dark"]) {
|
|
--bg: #f5f5f7;
|
|
--card-bg: #ffffff;
|
|
--panel-box: #f2f2f7;
|
|
--border: rgba(0, 0, 0, 0.12);
|
|
--text: #1d1d1f;
|
|
--muted: #86868b;
|
|
--blue: #0071e3;
|
|
--blue-btn-text: #ffffff;
|
|
--board-bg: #ffffff;
|
|
--grid-line: #f0f0f2;
|
|
}
|
|
html[data-theme="macos"][data-macos-mode="dark"] {
|
|
--bg: #1e1e1e;
|
|
--card-bg: #2d2d2d;
|
|
--panel-box: #3a3a3c;
|
|
--border: rgba(255, 255, 255, 0.15);
|
|
--text: #f5f5f7;
|
|
--muted: #98989d;
|
|
--blue: #0a84ff;
|
|
--blue-btn-text: #ffffff;
|
|
--board-bg: #1c1c1e;
|
|
--grid-line: #2c2c2e;
|
|
}
|
|
html[data-theme="futuristic"] {
|
|
--bg: #050811;
|
|
--card-bg: #091124;
|
|
--panel-box: #0f1c3a;
|
|
--border: #00f0ff;
|
|
--text: #e6f6ff;
|
|
--muted: #64b5f6;
|
|
--blue: #00f0ff;
|
|
--blue-btn-text: #050811;
|
|
--board-bg: #030712;
|
|
--grid-line: #0d2948;
|
|
}
|
|
html[data-theme="hacker"] {
|
|
--bg: #000000;
|
|
--card-bg: #051206;
|
|
--panel-box: #0a1f0c;
|
|
--border: #00ff41;
|
|
--text: #00ff41;
|
|
--muted: #008f11;
|
|
--blue: #00ff41;
|
|
--blue-btn-text: #000000;
|
|
--board-bg: #000000;
|
|
--grid-line: #00330d;
|
|
font-family: 'Courier New', monospace !important;
|
|
}
|
|
|
|
* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; }
|
|
body {
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
min-height: 100vh;
|
|
overflow: hidden;
|
|
padding: 10px;
|
|
transition: background 0.3s ease, color 0.3s ease;
|
|
}
|
|
.arcade-wrap {
|
|
display: flex;
|
|
gap: 16px;
|
|
align-items: flex-start;
|
|
background: var(--card-bg);
|
|
border: 1px solid var(--border);
|
|
border-radius: 14px;
|
|
padding: 16px;
|
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4), 0 0 20px rgba(92, 167, 255, 0.1);
|
|
position: relative;
|
|
transition: all 0.3s ease;
|
|
}
|
|
.canvas-container {
|
|
position: relative;
|
|
background: var(--board-bg);
|
|
border: 2px solid var(--border);
|
|
border-radius: 8px;
|
|
overflow: hidden;
|
|
box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.5);
|
|
}
|
|
canvas {
|
|
display: block;
|
|
}
|
|
.sidebar {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 12px;
|
|
width: 140px;
|
|
}
|
|
.panel-box {
|
|
background: var(--panel-box);
|
|
border: 1px solid var(--border);
|
|
border-radius: 8px;
|
|
padding: 10px;
|
|
text-align: center;
|
|
}
|
|
.panel-box h3 {
|
|
font-size: 11px;
|
|
text-transform: uppercase;
|
|
letter-spacing: 1px;
|
|
color: var(--muted);
|
|
margin-bottom: 6px;
|
|
}
|
|
.panel-box .value {
|
|
font-size: 20px;
|
|
font-weight: 700;
|
|
color: var(--blue);
|
|
font-family: "Courier New", Courier, monospace;
|
|
}
|
|
.preview-canvas {
|
|
background: var(--board-bg);
|
|
border-radius: 6px;
|
|
border: 1px solid var(--border);
|
|
margin: 0 auto;
|
|
display: block;
|
|
}
|
|
.btn {
|
|
background: var(--blue);
|
|
color: var(--blue-btn-text);
|
|
border: 0;
|
|
border-radius: 6px;
|
|
padding: 8px 12px;
|
|
font-weight: 700;
|
|
font-size: 13px;
|
|
cursor: pointer;
|
|
transition: all .15s ease;
|
|
width: 100%;
|
|
text-align: center;
|
|
}
|
|
.btn:hover { opacity: 0.9; transform: translateY(-1px); }
|
|
.btn.secondary {
|
|
background: var(--panel-box);
|
|
color: var(--text);
|
|
border: 1px solid var(--border);
|
|
}
|
|
.btn.secondary:hover { opacity: 0.85; }
|
|
.controls-help {
|
|
font-size: 11px;
|
|
color: var(--muted);
|
|
line-height: 1.5;
|
|
background: rgba(0,0,0,0.12);
|
|
border-radius: 6px;
|
|
padding: 8px;
|
|
border: 1px solid var(--border);
|
|
}
|
|
.controls-help kbd {
|
|
background: var(--panel-box);
|
|
color: var(--text);
|
|
border: 1px solid var(--border);
|
|
padding: 1px 4px;
|
|
border-radius: 3px;
|
|
font-size: 10px;
|
|
font-family: monospace;
|
|
}
|
|
.touch-controls {
|
|
display: none;
|
|
grid-template-columns: repeat(3, 1fr);
|
|
gap: 8px;
|
|
margin-top: 10px;
|
|
width: 100%;
|
|
max-width: 320px;
|
|
}
|
|
.touch-btn {
|
|
background: var(--panel-box);
|
|
border: 1px solid var(--border);
|
|
color: var(--text);
|
|
padding: 12px;
|
|
border-radius: 8px;
|
|
font-size: 18px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
cursor: pointer;
|
|
touch-action: manipulation;
|
|
}
|
|
.touch-btn:active { background: var(--blue); color: var(--blue-btn-text); }
|
|
@media (max-width: 600px) {
|
|
.arcade-wrap { flex-direction: column; align-items: center; padding: 10px; }
|
|
.sidebar { width: 100%; flex-direction: row; flex-wrap: wrap; justify-content: space-between; }
|
|
.sidebar .panel-box { flex: 1; min-width: 80px; }
|
|
.touch-controls { display: grid; }
|
|
}
|
|
.overlay-msg {
|
|
position: absolute;
|
|
inset: 0;
|
|
background: rgba(7, 19, 38, 0.88);
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 12px;
|
|
z-index: 10;
|
|
backdrop-filter: blur(3px);
|
|
}
|
|
.overlay-msg h2 {
|
|
font-size: 24px;
|
|
color: #ff6b6b;
|
|
letter-spacing: 1px;
|
|
text-transform: uppercase;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="arcade-wrap">
|
|
<div class="canvas-container">
|
|
<canvas id="tetris" width="240" height="480"></canvas>
|
|
<div id="gameOverlay" class="overlay-msg" style="display:none">
|
|
<h2 id="overlayTitle">GAME OVER</h2>
|
|
<div id="overlayScore" style="color:var(--blue);font-size:16px;font-weight:700">Score: 0</div>
|
|
<button class="btn" style="width:auto;padding:8px 24px" onclick="startGame()">REJOUER</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="sidebar">
|
|
<div class="panel-box">
|
|
<h3>Prochaine</h3>
|
|
<canvas id="next" class="preview-canvas" width="80" height="80"></canvas>
|
|
</div>
|
|
<div class="panel-box">
|
|
<h3>Score</h3>
|
|
<div id="score" class="value">0</div>
|
|
</div>
|
|
<div class="panel-box">
|
|
<h3>Lignes</h3>
|
|
<div id="lines" class="value">0</div>
|
|
</div>
|
|
<div class="panel-box">
|
|
<h3>Niveau</h3>
|
|
<div id="level" class="value">1</div>
|
|
</div>
|
|
<button id="pauseBtn" class="btn secondary" onclick="togglePause()">PAUSE (P)</button>
|
|
<button id="musicBtn" class="btn secondary" onclick="toggleMusic()">🎵 Musique : ON</button>
|
|
<button class="btn" onclick="startGame()">NOUVEAU</button>
|
|
|
|
<div class="controls-help">
|
|
<div><kbd>⬅️</kbd> <kbd>➡️</kbd> : Gauche / Droite</div>
|
|
<div><kbd>⬆️</kbd> : Pivoter</div>
|
|
<div><kbd>⬇️</kbd> : Chute rapide</div>
|
|
<div><kbd>Espace</kbd> : Drop direct</div>
|
|
<div><kbd>P</kbd> : Mettre en pause</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="touch-controls">
|
|
<div class="touch-btn" onclick="moveLeft()">◀</div>
|
|
<div class="touch-btn" onclick="rotate()">🔄</div>
|
|
<div class="touch-btn" onclick="moveRight()">▶</div>
|
|
<div class="touch-btn" onclick="softDrop()">🔽</div>
|
|
<div class="touch-btn" onclick="hardDrop()" style="grid-column:span 2;background:var(--blue);color:var(--blue-btn-text);font-weight:700">⬇️ DROP</div>
|
|
</div>
|
|
|
|
<script>
|
|
// Theme Management
|
|
function setTheme(theme, macosMode) {
|
|
if (!theme) theme = 'dark';
|
|
document.documentElement.dataset.theme = theme;
|
|
if (theme === 'macos' && macosMode) {
|
|
document.documentElement.dataset.macosMode = macosMode;
|
|
} else {
|
|
delete document.documentElement.dataset.macosMode;
|
|
}
|
|
if (typeof ctx !== 'undefined' && ctx && typeof board !== 'undefined' && board) {
|
|
draw();
|
|
drawNext();
|
|
}
|
|
}
|
|
|
|
window.addEventListener('message', e => {
|
|
if (e.data && e.data.type === 'themeChange') {
|
|
setTheme(e.data.theme, e.data.macosMode);
|
|
}
|
|
});
|
|
|
|
// Audio synthesizer
|
|
let audioCtx = null;
|
|
function playSound(type) {
|
|
try {
|
|
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
if (audioCtx.state === 'suspended') audioCtx.resume();
|
|
const osc = audioCtx.createOscillator();
|
|
const gain = audioCtx.createGain();
|
|
osc.connect(gain);
|
|
gain.connect(audioCtx.destination);
|
|
const now = audioCtx.currentTime;
|
|
|
|
if (type === 'move') {
|
|
osc.type = 'triangle';
|
|
osc.frequency.setValueAtTime(220, now);
|
|
gain.gain.setValueAtTime(0.04, now);
|
|
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.05);
|
|
osc.start(now); osc.stop(now + 0.05);
|
|
} else if (type === 'rotate') {
|
|
osc.type = 'sine';
|
|
osc.frequency.setValueAtTime(350, now);
|
|
osc.frequency.exponentialRampToValueAtTime(500, now + 0.08);
|
|
gain.gain.setValueAtTime(0.06, now);
|
|
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.08);
|
|
osc.start(now); osc.stop(now + 0.08);
|
|
} else if (type === 'drop') {
|
|
osc.type = 'sine';
|
|
osc.frequency.setValueAtTime(150, now);
|
|
gain.gain.setValueAtTime(0.08, 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.08, now);
|
|
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.28);
|
|
osc.start(now); osc.stop(now + 0.28);
|
|
} else if (type === 'gameover') {
|
|
osc.type = 'sawtooth';
|
|
osc.frequency.setValueAtTime(300, now);
|
|
osc.frequency.linearRampToValueAtTime(100, now + 0.4);
|
|
gain.gain.setValueAtTime(0.12, now);
|
|
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.4);
|
|
osc.start(now); osc.stop(now + 0.4);
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
// Tetris Theme Music (Korobeiniki 8-bit Chiptune)
|
|
let isMusicEnabled = true;
|
|
let musicTimeout = null;
|
|
let musicStep = 0;
|
|
const T_TEMPO = 146;
|
|
const T_BEAT = 60 / T_TEMPO; // ~0.41s per quarter note
|
|
|
|
const KOROBEINIKI = [
|
|
// Part A
|
|
{ f: 659.25, d: 4 }, // E5
|
|
{ f: 493.88, d: 2 }, // B4
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 587.33, d: 4 }, // D5
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 493.88, d: 2 }, // B4
|
|
{ f: 440.00, d: 4 }, // A4
|
|
{ f: 440.00, d: 2 }, // A4
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 659.25, d: 4 }, // E5
|
|
{ f: 587.33, d: 2 }, // D5
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 493.88, d: 6 }, // B4
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 587.33, d: 4 }, // D5
|
|
{ f: 659.25, d: 4 }, // E5
|
|
{ f: 523.25, d: 4 }, // C5
|
|
{ f: 440.00, d: 4 }, // A4
|
|
{ f: 440.00, d: 4 }, // A4
|
|
{ f: 0, d: 4 }, // Pause
|
|
|
|
// Part B
|
|
{ f: 587.33, d: 6 }, // D5
|
|
{ f: 698.46, d: 2 }, // F5
|
|
{ f: 880.00, d: 4 }, // A5
|
|
{ f: 783.99, d: 2 }, // G5
|
|
{ f: 698.46, d: 2 }, // F5
|
|
{ f: 659.25, d: 6 }, // E5
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 659.25, d: 4 }, // E5
|
|
{ f: 587.33, d: 2 }, // D5
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 493.88, d: 4 }, // B4
|
|
{ f: 493.88, d: 2 }, // B4
|
|
{ f: 523.25, d: 2 }, // C5
|
|
{ f: 587.33, d: 4 }, // D5
|
|
{ f: 659.25, d: 4 }, // E5
|
|
{ f: 523.25, d: 4 }, // C5
|
|
{ f: 440.00, d: 4 }, // A4
|
|
{ f: 440.00, d: 4 }, // A4
|
|
{ f: 0, d: 4 } // Pause
|
|
];
|
|
|
|
function playMusicNote(freq, durSec) {
|
|
if (!isMusicEnabled || isPaused || isGameOver || freq <= 0) return;
|
|
try {
|
|
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
if (audioCtx.state === 'suspended') audioCtx.resume();
|
|
const osc = audioCtx.createOscillator();
|
|
const gain = audioCtx.createGain();
|
|
osc.type = 'square';
|
|
osc.frequency.setValueAtTime(freq, audioCtx.currentTime);
|
|
const vol = 0.035;
|
|
gain.gain.setValueAtTime(vol, audioCtx.currentTime);
|
|
gain.gain.setValueAtTime(vol * 0.85, audioCtx.currentTime + durSec * 0.7);
|
|
gain.gain.linearRampToValueAtTime(0.0001, audioCtx.currentTime + durSec * 0.95);
|
|
osc.connect(gain);
|
|
gain.connect(audioCtx.destination);
|
|
osc.start();
|
|
osc.stop(audioCtx.currentTime + durSec * 0.95);
|
|
} catch (e) {}
|
|
}
|
|
|
|
function scheduleMusic() {
|
|
if (musicTimeout) clearTimeout(musicTimeout);
|
|
if (!isMusicEnabled || isPaused || isGameOver) return;
|
|
const item = KOROBEINIKI[musicStep];
|
|
const durSec = item.d * (T_BEAT / 4);
|
|
if (item.f > 0) {
|
|
playMusicNote(item.f, durSec);
|
|
}
|
|
musicStep = (musicStep + 1) % KOROBEINIKI.length;
|
|
musicTimeout = setTimeout(scheduleMusic, durSec * 1000);
|
|
}
|
|
|
|
function startMusic() {
|
|
if (musicTimeout) clearTimeout(musicTimeout);
|
|
musicStep = 0;
|
|
if (isMusicEnabled && !isPaused && !isGameOver) {
|
|
scheduleMusic();
|
|
}
|
|
}
|
|
|
|
function stopMusic() {
|
|
if (musicTimeout) {
|
|
clearTimeout(musicTimeout);
|
|
musicTimeout = null;
|
|
}
|
|
}
|
|
|
|
function toggleMusic() {
|
|
isMusicEnabled = !isMusicEnabled;
|
|
const btn = document.getElementById('musicBtn');
|
|
if (btn) btn.textContent = isMusicEnabled ? '🎵 Musique : ON' : '🔇 Musique : OFF';
|
|
if (isMusicEnabled) {
|
|
if (!isPaused && !isGameOver) scheduleMusic();
|
|
} else {
|
|
stopMusic();
|
|
}
|
|
}
|
|
|
|
function resumeAudioOnGesture() {
|
|
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
if (audioCtx.state === 'suspended') audioCtx.resume();
|
|
if (isMusicEnabled && !musicTimeout && !isPaused && !isGameOver) {
|
|
scheduleMusic();
|
|
}
|
|
}
|
|
window.addEventListener('keydown', resumeAudioOnGesture);
|
|
window.addEventListener('pointerdown', resumeAudioOnGesture);
|
|
|
|
|
|
const canvas = document.getElementById('tetris');
|
|
const ctx = canvas.getContext('2d');
|
|
const nextCanvas = document.getElementById('next');
|
|
const nextCtx = nextCanvas.getContext('2d');
|
|
|
|
const COLS = 10;
|
|
const ROWS = 20;
|
|
const BLOCK = 24;
|
|
|
|
const COLORS = [
|
|
null,
|
|
'#00f0ff', // I - Cyan
|
|
'#2979ff', // J - Blue
|
|
'#ff9100', // L - Orange
|
|
'#ffd600', // O - Yellow
|
|
'#00e676', // S - Green
|
|
'#d500f9', // T - Purple
|
|
'#ff1744' // Z - Red
|
|
];
|
|
|
|
const PIECES = 'ILJOTSZ';
|
|
function createPiece(type) {
|
|
if (type === 'I') {
|
|
return [
|
|
[0, 1, 0, 0],
|
|
[0, 1, 0, 0],
|
|
[0, 1, 0, 0],
|
|
[0, 1, 0, 0],
|
|
];
|
|
} else if (type === 'J') {
|
|
return [
|
|
[0, 2, 0],
|
|
[0, 2, 0],
|
|
[2, 2, 0],
|
|
];
|
|
} else if (type === 'L') {
|
|
return [
|
|
[0, 3, 0],
|
|
[0, 3, 0],
|
|
[0, 3, 3],
|
|
];
|
|
} else if (type === 'O') {
|
|
return [
|
|
[4, 4],
|
|
[4, 4],
|
|
];
|
|
} else if (type === 'S') {
|
|
return [
|
|
[0, 5, 5],
|
|
[5, 5, 0],
|
|
[0, 0, 0],
|
|
];
|
|
} else if (type === 'T') {
|
|
return [
|
|
[0, 6, 0],
|
|
[6, 6, 6],
|
|
[0, 0, 0],
|
|
];
|
|
} else if (type === 'Z') {
|
|
return [
|
|
[7, 7, 0],
|
|
[0, 7, 7],
|
|
[0, 0, 0],
|
|
];
|
|
}
|
|
}
|
|
|
|
let board = createMatrix(COLS, ROWS);
|
|
let player = {
|
|
pos: { x: 0, y: 0 },
|
|
matrix: null,
|
|
score: 0,
|
|
lines: 0,
|
|
level: 1
|
|
};
|
|
let nextPiece = null;
|
|
let isPaused = false;
|
|
let isGameOver = false;
|
|
let dropCounter = 0;
|
|
let dropInterval = 1000;
|
|
let lastTime = 0;
|
|
|
|
function createMatrix(w, h) {
|
|
const m = [];
|
|
while (h--) m.push(new Array(w).fill(0));
|
|
return m;
|
|
}
|
|
|
|
function collide(b, p) {
|
|
const [m, o] = [p.matrix, p.pos];
|
|
for (let y = 0; y < m.length; ++y) {
|
|
for (let x = 0; x < m[y].length; ++x) {
|
|
if (m[y][x] !== 0 && (b[y + o.y] && b[y + o.y][x + o.x]) !== 0) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function merge(b, p) {
|
|
p.matrix.forEach((row, y) => {
|
|
row.forEach((value, x) => {
|
|
if (value !== 0) {
|
|
b[y + p.pos.y][x + p.pos.x] = value;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function rotateMatrix(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 playerRotate(dir) {
|
|
const pos = player.pos.x;
|
|
let offset = 1;
|
|
rotateMatrix(player.matrix, dir);
|
|
while (collide(board, player)) {
|
|
player.pos.x += offset;
|
|
offset = -(offset + (offset > 0 ? 1 : -1));
|
|
if (offset > player.matrix[0].length) {
|
|
rotateMatrix(player.matrix, -dir);
|
|
player.pos.x = pos;
|
|
return;
|
|
}
|
|
}
|
|
playSound('rotate');
|
|
}
|
|
|
|
function playerReset() {
|
|
if (!nextPiece) nextPiece = createPiece(PIECES[Math.floor(Math.random() * PIECES.length)]);
|
|
player.matrix = nextPiece;
|
|
nextPiece = createPiece(PIECES[Math.floor(Math.random() * PIECES.length)]);
|
|
player.pos.y = 0;
|
|
player.pos.x = (board[0].length / 2 | 0) - (player.matrix[0].length / 2 | 0);
|
|
drawNext();
|
|
|
|
if (collide(board, player)) {
|
|
isGameOver = true;
|
|
stopMusic();
|
|
playSound('gameover');
|
|
document.getElementById('gameOverlay').style.display = 'flex';
|
|
document.getElementById('overlayTitle').textContent = 'GAME OVER';
|
|
document.getElementById('overlayScore').textContent = 'Score final : ' + player.score;
|
|
}
|
|
}
|
|
|
|
function arenaSweep() {
|
|
let rowCount = 0;
|
|
outer: for (let y = board.length - 1; y >= 0; --y) {
|
|
for (let x = 0; x < board[y].length; ++x) {
|
|
if (board[y][x] === 0) continue outer;
|
|
}
|
|
const row = board.splice(y, 1)[0].fill(0);
|
|
board.unshift(row);
|
|
++y;
|
|
rowCount++;
|
|
}
|
|
|
|
if (rowCount > 0) {
|
|
playSound('clear');
|
|
const points = [0, 100, 300, 500, 800];
|
|
player.score += (points[rowCount] || 100) * player.level;
|
|
player.lines += rowCount;
|
|
player.level = Math.floor(player.lines / 10) + 1;
|
|
dropInterval = Math.max(120, 1000 - (player.level - 1) * 90);
|
|
updateScore();
|
|
}
|
|
}
|
|
|
|
function updateScore() {
|
|
document.getElementById('score').textContent = player.score;
|
|
document.getElementById('lines').textContent = player.lines;
|
|
document.getElementById('level').textContent = player.level;
|
|
}
|
|
|
|
function playerDrop() {
|
|
player.pos.y++;
|
|
if (collide(board, player)) {
|
|
player.pos.y--;
|
|
merge(board, player);
|
|
arenaSweep();
|
|
playerReset();
|
|
}
|
|
dropCounter = 0;
|
|
}
|
|
|
|
function hardDrop() {
|
|
if (isPaused || isGameOver) return;
|
|
while (!collide(board, player)) {
|
|
player.pos.y++;
|
|
}
|
|
player.pos.y--;
|
|
merge(board, player);
|
|
playSound('drop');
|
|
arenaSweep();
|
|
playerReset();
|
|
dropCounter = 0;
|
|
}
|
|
|
|
function moveLeft() {
|
|
if (isPaused || isGameOver) return;
|
|
player.pos.x--;
|
|
if (collide(board, player)) player.pos.x++;
|
|
else playSound('move');
|
|
}
|
|
|
|
function moveRight() {
|
|
if (isPaused || isGameOver) return;
|
|
player.pos.x++;
|
|
if (collide(board, player)) player.pos.x--;
|
|
else playSound('move');
|
|
}
|
|
|
|
function softDrop() {
|
|
if (isPaused || isGameOver) return;
|
|
playerDrop();
|
|
}
|
|
|
|
function rotate() {
|
|
if (isPaused || isGameOver) return;
|
|
playerRotate(1);
|
|
}
|
|
|
|
function togglePause() {
|
|
if (isGameOver) return;
|
|
isPaused = !isPaused;
|
|
document.getElementById('pauseBtn').textContent = isPaused ? 'REPRENDRE' : 'PAUSE (P)';
|
|
const ov = document.getElementById('gameOverlay');
|
|
if (isPaused) {
|
|
stopMusic();
|
|
ov.style.display = 'flex';
|
|
document.getElementById('overlayTitle').textContent = 'PAUSE';
|
|
document.getElementById('overlayScore').textContent = 'Score: ' + player.score;
|
|
} else {
|
|
ov.style.display = 'none';
|
|
lastTime = performance.now();
|
|
if (isMusicEnabled) scheduleMusic();
|
|
}
|
|
}
|
|
|
|
function getThemeVar(name, fallback) {
|
|
return getComputedStyle(document.documentElement).getPropertyValue(name).trim() || fallback;
|
|
}
|
|
|
|
function drawBlock(c, x, y, colorId, isGhost = false) {
|
|
const color = COLORS[colorId];
|
|
if (!color) return;
|
|
const px = x * BLOCK;
|
|
const py = y * BLOCK;
|
|
|
|
if (isGhost) {
|
|
c.strokeStyle = color;
|
|
c.lineWidth = 1.5;
|
|
c.strokeRect(px + 1, py + 1, BLOCK - 2, BLOCK - 2);
|
|
c.fillStyle = color + '22';
|
|
c.fillRect(px + 1, py + 1, BLOCK - 2, BLOCK - 2);
|
|
return;
|
|
}
|
|
|
|
c.fillStyle = color;
|
|
c.fillRect(px, py, BLOCK, BLOCK);
|
|
|
|
c.fillStyle = 'rgba(255, 255, 255, 0.3)';
|
|
c.fillRect(px, py, BLOCK, 3);
|
|
c.fillRect(px, py, 3, BLOCK);
|
|
|
|
c.fillStyle = 'rgba(0, 0, 0, 0.32)';
|
|
c.fillRect(px + BLOCK - 3, py, 3, BLOCK);
|
|
c.fillRect(px, py + BLOCK - 3, BLOCK, 3);
|
|
|
|
c.strokeStyle = 'rgba(0,0,0,0.35)';
|
|
c.strokeRect(px, py, BLOCK, BLOCK);
|
|
}
|
|
|
|
function drawGhost() {
|
|
let ghost = {
|
|
pos: { x: player.pos.x, y: player.pos.y },
|
|
matrix: player.matrix
|
|
};
|
|
while (!collide(board, ghost)) {
|
|
ghost.pos.y++;
|
|
}
|
|
ghost.pos.y--;
|
|
ghost.matrix.forEach((row, y) => {
|
|
row.forEach((value, x) => {
|
|
if (value !== 0) {
|
|
drawBlock(ctx, x + ghost.pos.x, y + ghost.pos.y, value, true);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function draw() {
|
|
const boardBg = getThemeVar('--board-bg', '#050d1a');
|
|
const gridLine = getThemeVar('--grid-line', '#0e203c');
|
|
|
|
ctx.fillStyle = boardBg;
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
|
ctx.strokeStyle = gridLine;
|
|
ctx.lineWidth = 0.5;
|
|
for (let x = 0; x <= COLS; x++) {
|
|
ctx.beginPath(); ctx.moveTo(x * BLOCK, 0); ctx.lineTo(x * BLOCK, ROWS * BLOCK); ctx.stroke();
|
|
}
|
|
for (let y = 0; y <= ROWS; y++) {
|
|
ctx.beginPath(); ctx.moveTo(0, y * BLOCK); ctx.lineTo(COLS * BLOCK, y * BLOCK); ctx.stroke();
|
|
}
|
|
|
|
board.forEach((row, y) => {
|
|
row.forEach((value, x) => {
|
|
if (value !== 0) drawBlock(ctx, x, y, value);
|
|
});
|
|
});
|
|
|
|
if (player.matrix) {
|
|
drawGhost();
|
|
player.matrix.forEach((row, y) => {
|
|
row.forEach((value, x) => {
|
|
if (value !== 0) drawBlock(ctx, x + player.pos.x, y + player.pos.y, value);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
function drawNext() {
|
|
const boardBg = getThemeVar('--board-bg', '#050d1a');
|
|
nextCtx.fillStyle = boardBg;
|
|
nextCtx.fillRect(0, 0, nextCanvas.width, nextCanvas.height);
|
|
if (!nextPiece) return;
|
|
const bSize = 16;
|
|
const offX = (nextCanvas.width - nextPiece[0].length * bSize) / 2;
|
|
const offY = (nextCanvas.height - nextPiece.length * bSize) / 2;
|
|
|
|
nextPiece.forEach((row, y) => {
|
|
row.forEach((value, x) => {
|
|
if (value !== 0) {
|
|
const px = offX + x * bSize;
|
|
const py = offY + y * bSize;
|
|
nextCtx.fillStyle = COLORS[value];
|
|
nextCtx.fillRect(px, py, bSize, bSize);
|
|
nextCtx.strokeStyle = 'rgba(0,0,0,0.35)';
|
|
nextCtx.strokeRect(px, py, bSize, bSize);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function update(time = 0) {
|
|
if (!isPaused && !isGameOver) {
|
|
const deltaTime = time - lastTime;
|
|
lastTime = time;
|
|
dropCounter += deltaTime;
|
|
if (dropCounter > dropInterval) {
|
|
playerDrop();
|
|
}
|
|
draw();
|
|
}
|
|
requestAnimationFrame(update);
|
|
}
|
|
|
|
function startGame() {
|
|
board = createMatrix(COLS, ROWS);
|
|
player.score = 0;
|
|
player.lines = 0;
|
|
player.level = 1;
|
|
dropInterval = 1000;
|
|
isGameOver = false;
|
|
isPaused = false;
|
|
document.getElementById('gameOverlay').style.display = 'none';
|
|
document.getElementById('pauseBtn').textContent = 'PAUSE (P)';
|
|
updateScore();
|
|
nextPiece = null;
|
|
playerReset();
|
|
lastTime = performance.now();
|
|
startMusic();
|
|
}
|
|
|
|
window.addEventListener('keydown', e => {
|
|
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(e.key)) {
|
|
e.preventDefault();
|
|
}
|
|
if (e.key === 'p' || e.key === 'P') {
|
|
togglePause();
|
|
return;
|
|
}
|
|
if (isPaused || isGameOver) return;
|
|
|
|
if (e.key === 'ArrowLeft') moveLeft();
|
|
else if (e.key === 'ArrowRight') moveRight();
|
|
else if (e.key === 'ArrowDown') softDrop();
|
|
else if (e.key === 'ArrowUp' || e.key === 'x' || e.key === 'X') rotate();
|
|
else if (e.key === ' ') hardDrop();
|
|
});
|
|
|
|
// Initialize theme from URL query param safely after all variables exist
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
setTheme(urlParams.get('theme') || 'dark', urlParams.get('macosMode'));
|
|
|
|
startGame();
|
|
update();
|
|
</script>
|
|
</body>
|
|
</html>
|