Spaces:
Running

GAF1 / Tetris_Trial.html
IndividualGamer's picture
Upload Tetris_Trial.html
7203196 verified
Raw History Blame Contribute Delete
18.8 kB
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Tetris Mobile Portrait - TRIAL</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
}
body {
background-color: #121218;
color: #ffffff;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
height: 100dvh;
padding: 10px 12px 16px 12px;
overflow: hidden;
}
.header {
width: 100%;
max-width: 360px;
display: flex;
justify-content: space-between;
align-items: center;
background: #1e1e2a;
padding: 8px 12px;
border-radius: 12px;
border: 1px solid #2e2e3e;
}
.stat-box {
display: flex;
flex-direction: column;
align-items: center;
}
.stat-label {
font-size: 10px;
color: #9ca3af;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.stat-value {
font-size: 16px;
font-weight: 700;
color: #38bdf8;
}
.trial-badge {
background: rgba(245, 158, 11, 0.15);
border: 1px solid #f59e0b;
color: #fbbf24;
font-size: 10px;
font-weight: 800;
padding: 3px 7px;
border-radius: 6px;
letter-spacing: 0.8px;
text-transform: uppercase;
}
.next-container {
display: flex;
flex-direction: column;
align-items: center;
}
#nextCanvas {
background: #121218;
border-radius: 6px;
margin-top: 2px;
}
.save-btn {
background: #0f172a;
border: 1px solid #10b981;
color: #10b981;
padding: 8px 10px;
border-radius: 8px;
font-size: 11px;
font-weight: 700;
cursor: pointer;
text-transform: uppercase;
transition: all 0.15s ease;
}
.save-btn:active {
background: #10b981;
color: #0f172a;
transform: scale(0.95);
}
.game-wrapper {
position: relative;
display: flex;
justify-content: center;
align-items: center;
flex: 1;
width: 100%;
margin: 8px 0;
}
#board {
background-color: #181822;
border: 2px solid #334155;
border-radius: 8px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
max-height: 100%;
aspect-ratio: 1 / 2;
}
.board-trial-watermark {
position: absolute;
bottom: 10px;
font-size: 11px;
font-weight: 800;
letter-spacing: 2px;
color: rgba(251, 191, 36, 0.35);
pointer-events: none;
z-index: 5;
}
#toast {
position: absolute;
top: 16px;
background: rgba(16, 185, 129, 0.92);
color: #ffffff;
padding: 6px 14px;
border-radius: 999px;
font-size: 12px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transition: opacity 0.25s ease;
z-index: 20;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
#toast.show {
opacity: 1;
}
#overlay {
position: absolute;
inset: 0;
background: rgba(18, 18, 24, 0.92);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-radius: 8px;
gap: 12px;
z-index: 10;
padding: 16px;
text-align: center;
}
#overlay.hidden {
display: none;
}
.overlay-trial-tag {
background: #f59e0b;
color: #121218;
font-size: 11px;
font-weight: 900;
padding: 3px 10px;
border-radius: 999px;
letter-spacing: 1px;
}
#overlayTitle {
font-size: 26px;
font-weight: 800;
color: #f8fafc;
letter-spacing: 1px;
}
#overlayScore {
font-size: 14px;
color: #94a3b8;
margin-bottom: 4px;
line-height: 1.4;
}
.menu-btn {
width: 220px;
border: none;
padding: 12px 18px;
font-size: 14px;
font-weight: 700;
border-radius: 999px;
cursor: pointer;
transition: transform 0.1s ease;
text-decoration: none;
display: inline-block;
text-align: center;
}
.menu-btn:active {
transform: scale(0.96);
}
.hidden {
display: none !important;
}
#startBtn {
background: linear-gradient(135deg, #0ea5e9, #2563eb);
color: white;
box-shadow: 0 4px 12px rgba(14, 165, 233, 0.4);
}
#loadBtn {
background: linear-gradient(135deg, #10b981, #059669);
color: white;
box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
}
#downloadLink {
background: linear-gradient(135deg, #f59e0b, #ea580c);
color: white;
box-shadow: 0 4px 12px rgba(245, 158, 11, 0.4);
}
.controls {
width: 100%;
max-width: 360px;
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.ctrl-btn {
background: #1e1e2a;
border: 1px solid #334155;
color: #f8fafc;
height: 56px;
border-radius: 14px;
font-size: 22px;
font-weight: bold;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
box-shadow: 0 4px 0 #0f172a;
}
.ctrl-btn:active {
background: #2e2e3e;
transform: translateY(3px);
box-shadow: 0 1px 0 #0f172a;
}
.ctrl-btn.rotate {
background: #1e293b;
color: #38bdf8;
border-color: #0284c7;
}
</style>
</head>
<body>
<div class="header">
<div class="stat-box">
<span class="stat-label">Skor</span>
<span class="stat-value" id="score">0</span>
</div>
<div class="stat-box">
<span class="stat-label">Level</span>
<span class="stat-value" id="level">1</span>
</div>
<div class="trial-badge">TRIAL</div>
<div class="next-container">
<span class="stat-label">Berikutnya</span>
<canvas id="nextCanvas" width="56" height="42"></canvas>
</div>
<button class="save-btn" id="btnSave" title="Simpan Permainan">Save</button>
</div>
<div class="game-wrapper">
<div id="toast">Game Tersimpan!</div>
<canvas id="board" width="240" height="480"></canvas>
<div class="board-trial-watermark">TRIAL VERSION (MAX 1500)</div>
<div id="overlay">
<div class="overlay-trial-tag">TRIAL</div>
<div id="overlayTitle">TETRIS</div>
<div id="overlayScore">Maksimum skor trial: 1500</div>
<button class="menu-btn hidden" id="loadBtn">LANJUTKAN GAME</button>
<button class="menu-btn" id="startBtn">MULAI BARU</button>
<a class="menu-btn hidden" id="downloadLink" href="https://lynk.id/readthefuture" target="_blank" rel="noopener noreferrer">Unduh lagi untuk lanjut main</a>
</div>
</div>
<div class="controls">
<button class="ctrl-btn" id="btnLeft" aria-label="Kiri">&#9664;</button>
<button class="ctrl-btn" id="btnDown" aria-label="Turun">&#9660;</button>
<button class="ctrl-btn" id="btnRight" aria-label="Kanan">&#9654;</button>
<button class="ctrl-btn rotate" id="btnRotate" aria-label="Putar">&#8635;</button>
</div>
<script>
const canvas = document.getElementById("board");
const ctx = canvas.getContext("2d");
const nextCanvas = document.getElementById("nextCanvas");
const nextCtx = nextCanvas.getContext("2d");
const COLS = 10;
const ROWS = 20;
const BLOCK_SIZE = 24;
const SAVE_KEY = "tetris_mobile_save_v1";
const TRIAL_LOCK_KEY = "tetris_trial_locked_v1";
const MAX_TRIAL_SCORE = 1500;
const COLORS = [
null,
"#06b6d4", // I - Cyan
"#3b82f6", // J - Blue
"#f97316", // L - Orange
"#eab308", // O - Yellow
"#22c55e", // S - Green
"#a855f7", // T - Purple
"#ef4444" // Z - Red
];
const SHAPES = [
[],
[[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]],
[[2,0,0],[2,2,2],[0,0,0]],
[[0,0,3],[3,3,3],[0,0,0]],
[[4,4],[4,4]],
[[0,5,5],[5,5,0],[0,0,0]],
[[0,6,0],[6,6,6],[0,0,0]],
[[7,7,0],[0,7,7],[0,0,0]]
];
let board = [];
let score = 0;
let lines = 0;
let level = 1;
let dropCounter = 0;
let dropInterval = 800;
let lastTime = 0;
let isGameOver = false;
let isPaused = true;
let animationId = null;
let player = {
pos: {x: 0, y: 0},
matrix: null,
next: null
};
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function playTone(freq, type, duration) {
if (audioCtx.state === "suspended") audioCtx.resume();
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = type;
osc.frequency.setValueAtTime(freq, audioCtx.currentTime);
gain.gain.setValueAtTime(0.08, audioCtx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + duration);
osc.connect(gain);
gain.connect(audioCtx.destination);
osc.start();
osc.stop(audioCtx.currentTime + duration);
}
function showToast(msg) {
const toast = document.getElementById("toast");
toast.innerText = msg;
toast.classList.add("show");
setTimeout(() => {
toast.classList.remove("show");
}, 1600);
}
function checkTrialLimit(currentScore = 0) {
const isLocked = currentScore >= MAX_TRIAL_SCORE || localStorage.getItem(TRIAL_LOCK_KEY) === "true";
if (isLocked) {
localStorage.setItem(TRIAL_LOCK_KEY, "true");
lockTrialGame();
return true;
}
return false;
}
function lockTrialGame() {
isGameOver = true;
isPaused = true;
if (animationId) cancelAnimationFrame(animationId);
document.getElementById("overlayTitle").innerText = "TRIAL SELESAI";
document.getElementById("overlayScore").innerText = "Nilai maksimum trial (" + MAX_TRIAL_SCORE + ") telah tercapai.";
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("loadBtn").classList.add("hidden");
document.getElementById("downloadLink").classList.remove("hidden");
document.getElementById("overlay").classList.remove("hidden");
}
function saveGame() {
if (checkTrialLimit(score)) return;
if (isPaused || isGameOver || !player.matrix) {
showToast("Mainkan dulu untuk save!");
return;
}
const saveData = {
board: board,
score: score,
lines: lines,
level: level,
dropInterval: dropInterval,
player: {
pos: { x: player.pos.x, y: player.pos.y },
matrix: player.matrix,
next: player.next
}
};
localStorage.setItem(SAVE_KEY, JSON.stringify(saveData));
playTone(520, "sine", 0.12);
showToast("Permainan Tersimpan!");
checkSavedGame();
}
function loadGame() {
if (checkTrialLimit(score)) return;
const raw = localStorage.getItem(SAVE_KEY);
if (!raw) return;
try {
const data = JSON.parse(raw);
if (checkTrialLimit(data.score)) return;
board = data.board;
score = data.score;
lines = data.lines;
level = data.level;
dropInterval = data.dropInterval;
player = data.player;
isGameOver = false;
isPaused = false;
dropCounter = 0;
updateUI();
drawNext();
draw();
document.getElementById("overlay").classList.add("hidden");
lastTime = performance.now();
if (animationId) cancelAnimationFrame(animationId);
playTone(600, "triangle", 0.15);
showToast("Melanjutkan Save!");
update();
} catch (e) {
console.error("Gagal memuat data:", e);
showToast("Data save rusak");
}
}
function checkSavedGame() {
if (checkTrialLimit(score)) return;
const raw = localStorage.getItem(SAVE_KEY);
const loadBtn = document.getElementById("loadBtn");
if (raw) {
try {
const data = JSON.parse(raw);
if (data.score >= MAX_TRIAL_SCORE) {
checkTrialLimit(data.score);
return;
}
loadBtn.innerText = "LANJUT (Skor: " + data.score + ")";
loadBtn.classList.remove("hidden");
} catch {
loadBtn.classList.add("hidden");
}
} else {
loadBtn.classList.add("hidden");
}
}
function createMatrix(w, h) {
const matrix = [];
while (h--) matrix.push(new Array(w).fill(0));
return matrix;
}
function randomPiece() {
const id = Math.floor(Math.random() * 7) + 1;
return SHAPES[id].map(row => [...row]);
}
function drawBlock(context, x, y, colorId, size) {
context.fillStyle = COLORS[colorId];
context.fillRect(x * size, y * size, size, size);
context.strokeStyle = "rgba(0,0,0,0.35)";
context.lineWidth = 1.5;
context.strokeRect(x * size, y * size, size, size);
context.fillStyle = "rgba(255,255,255,0.2)";
context.fillRect(x * size + 2, y * size + 2, size - 4, 4);
}
function draw() {
ctx.fillStyle = "#181822";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#222230";
ctx.lineWidth = 1;
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
ctx.strokeRect(c * BLOCK_SIZE, r * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
drawMatrix(ctx, board, {x: 0, y: 0}, BLOCK_SIZE);
if (player.matrix) {
drawMatrix(ctx, player.matrix, player.pos, BLOCK_SIZE);
}
}
function drawMatrix(context, matrix, offset, size) {
matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
drawBlock(context, x + offset.x, y + offset.y, value, size);
}
});
});
}
function drawNext() {
nextCtx.fillStyle = "#121218";
nextCtx.fillRect(0, 0, nextCanvas.width, nextCanvas.height);
if (!player.next) return;
const size = 12;
const offsetX = (nextCanvas.width / size - player.next[0].length) / 2;
const offsetY = (nextCanvas.height / size - player.next.length) / 2;
drawMatrix(nextCtx, player.next, {x: offsetX, y: offsetY}, size);
}
function collide(board, player) {
const [m, o] = [player.matrix, player.pos];
for (let y = 0; y < m.length; ++y) {
for (let x = 0; x < m[y].length; ++x) {
if (m[y][x] !== 0 &&
(board[y + o.y] && board[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
}
function merge(board, player) {
player.matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
board[y + player.pos.y][x + player.pos.x] = value;
}
});
});
}
function rotateMatrix(matrix) {
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]];
}
}
matrix.forEach(row => row.reverse());
}
function playerRotate() {
if (isPaused || isGameOver) return;
const pos = player.pos.x;
let offset = 1;
rotateMatrix(player.matrix);
while (collide(board, player)) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.matrix[0].length) {
rotateMatrix(player.matrix);
rotateMatrix(player.matrix);
rotateMatrix(player.matrix);
player.pos.x = pos;
return;
}
}
playTone(440, "triangle", 0.06);
draw();
}
function playerMove(dir) {
if (isPaused || isGameOver) return;
player.pos.x += dir;
if (collide(board, player)) {
player.pos.x -= dir;
} else {
playTone(260, "sine", 0.04);
draw();
}
}
function playerDrop() {
if (isPaused || isGameOver) return;
player.pos.y++;
if (collide(board, player)) {
player.pos.y--;
merge(board, player);
playTone(180, "square", 0.08);
sweepLines();
if (!isGameOver) {
playerReset();
}
}
dropCounter = 0;
draw();
}
function sweepLines() {
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) {
const lineScores = [0, 100, 300, 500, 800];
score += lineScores[rowCount] * level;
lines += rowCount;
level = Math.floor(lines / 10) + 1;
dropInterval = Math.max(120, 800 - (level - 1) * 70);
updateUI();
playTone(620, "sine", 0.2);
if (checkTrialLimit(score)) {
playTone(130, "sawtooth", 0.4);
return;
}
}
}
function playerReset() {
if (checkTrialLimit(score)) return;
player.matrix = player.next || randomPiece();
player.next = randomPiece();
player.pos.y = 0;
player.pos.x = Math.floor((COLS - player.matrix[0].length) / 2);
drawNext();
if (collide(board, player)) {
isGameOver = true;
isPaused = true;
playTone(130, "sawtooth", 0.4);
checkSavedGame();
if (localStorage.getItem(TRIAL_LOCK_KEY) !== "true") {
document.getElementById("overlayTitle").innerText = "GAME OVER";
document.getElementById("overlayScore").innerText = "Skor Akhir: " + score;
document.getElementById("startBtn").innerText = "MAIN BARU";
document.getElementById("overlay").classList.remove("hidden");
}
}
}
function updateUI() {
document.getElementById("score").innerText = score;
document.getElementById("level").innerText = level;
}
function update(time = 0) {
if (isPaused || isGameOver) return;
if (checkTrialLimit(score)) return;
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter > dropInterval) {
playerDrop();
}
draw();
animationId = requestAnimationFrame(update);
}
function startGame() {
if (checkTrialLimit(0)) return;
board = createMatrix(COLS, ROWS);
score = 0;
lines = 0;
level = 1;
dropInterval = 800;
isGameOver = false;
isPaused = false;
player.next = null;
updateUI();
playerReset();
document.getElementById("overlay").classList.add("hidden");
lastTime = performance.now();
if (animationId) cancelAnimationFrame(animationId);
update();
}
document.getElementById("startBtn").addEventListener("click", startGame);
document.getElementById("loadBtn").addEventListener("click", loadGame);
document.getElementById("btnSave").addEventListener("click", saveGame);
document.getElementById("btnLeft").addEventListener("click", () => playerMove(-1));
document.getElementById("btnRight").addEventListener("click", () => playerMove(1));
document.getElementById("btnDown").addEventListener("click", () => playerDrop());
document.getElementById("btnRotate").addEventListener("click", () => playerRotate());
let touchStartX = 0;
let touchStartY = 0;
canvas.addEventListener("touchstart", e => {
touchStartX = e.changedTouches[0].screenX;
touchStartY = e.changedTouches[0].screenY;
}, {passive: true});
canvas.addEventListener("touchend", e => {
const dx = e.changedTouches[0].screenX - touchStartX;
const dy = e.changedTouches[0].screenY - touchStartY;
if (Math.abs(dx) < 15 && Math.abs(dy) < 15) {
playerRotate();
} else if (Math.abs(dx) > Math.abs(dy)) {
playerMove(dx > 0 ? 1 : -1);
} else if (dy > 25) {
playerDrop();
}
}, {passive: true});
document.addEventListener("keydown", e => {
if (e.key === "ArrowLeft") playerMove(-1);
else if (e.key === "ArrowRight") playerMove(1);
else if (e.key === "ArrowDown") playerDrop();
else if (e.key === "ArrowUp") playerRotate();
else if (e.key.toLowerCase() === "s") saveGame();
});
checkSavedGame();
draw();
</script>
</body>
</html>