Remove misplaced 2048 file (moved to dedicated 2048-game repo)
This commit is contained in:
-544
@@ -1,544 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
|
||||
<title>2048 · 数字合并</title>
|
||||
<style>
|
||||
*{box-sizing:border-box;margin:0;padding:0;}
|
||||
html,body{height:100%;}
|
||||
body{
|
||||
background:radial-gradient(circle at 50% -20%, #23233a 0%, #12121c 45%, #0a0a10 100%);
|
||||
color:#e8ecf7;
|
||||
font-family:ui-sans-serif,system-ui,"Segoe UI",Roboto,"PingFang SC","Microsoft YaHei",sans-serif;
|
||||
display:flex;align-items:center;justify-content:center;
|
||||
min-height:100%;padding:18px;
|
||||
-webkit-tap-highlight-color:transparent;
|
||||
-webkit-user-select:none;user-select:none;
|
||||
overflow:hidden;
|
||||
}
|
||||
.app{display:flex;flex-direction:column;gap:14px;width:100%;max-width:440px;align-items:center;}
|
||||
|
||||
/* ---------- 顶部 ---------- */
|
||||
header{display:flex;align-items:flex-end;justify-content:space-between;width:100%;gap:12px;}
|
||||
.title{
|
||||
font-size:clamp(30px,8vw,46px);font-weight:900;letter-spacing:-.02em;line-height:1;
|
||||
background:linear-gradient(135deg,#ffe45c,#f5a742 55%,#e2543f);
|
||||
-webkit-background-clip:text;background-clip:text;color:transparent;
|
||||
filter:drop-shadow(0 3px 18px rgba(245,167,66,.35));
|
||||
}
|
||||
.scores{display:flex;gap:8px;}
|
||||
.score-box{
|
||||
background:rgba(255,255,255,.05);border:1px solid rgba(255,255,255,.08);
|
||||
border-radius:11px;padding:7px 14px;text-align:center;min-width:78px;
|
||||
}
|
||||
.score-box span{display:block;font-size:10px;letter-spacing:.14em;color:#8892ad;font-weight:700;}
|
||||
.score-box strong{
|
||||
display:block;font-size:19px;font-weight:800;color:#fff;margin-top:2px;
|
||||
font-variant-numeric:tabular-nums;
|
||||
}
|
||||
.score-box.best strong{color:#f5c451;}
|
||||
|
||||
/* ---------- 操作条 ---------- */
|
||||
.bar{display:flex;align-items:center;justify-content:space-between;width:100%;gap:10px;}
|
||||
.hint{font-size:12px;color:#7f8aa6;line-height:1.5;}
|
||||
.hint b{color:#f5c451;}
|
||||
.btns{display:flex;gap:8px;flex-shrink:0;}
|
||||
button{
|
||||
cursor:pointer;font-family:inherit;font-weight:700;letter-spacing:.02em;
|
||||
border-radius:10px;border:1px solid rgba(255,255,255,.1);
|
||||
background:rgba(255,255,255,.06);color:#c8d2e8;
|
||||
padding:9px 15px;font-size:13px;transition:all .15s ease;
|
||||
}
|
||||
button:hover{background:rgba(255,255,255,.13);color:#fff;border-color:rgba(255,255,255,.22);}
|
||||
button:active{transform:scale(.96);}
|
||||
button:disabled{opacity:.32;cursor:not-allowed;transform:none;}
|
||||
button.primary{
|
||||
background:linear-gradient(135deg,#f7c948,#ef8b3c);color:#2a1c00;border:0;
|
||||
box-shadow:0 4px 18px rgba(239,139,60,.35);
|
||||
}
|
||||
button.primary:hover{box-shadow:0 6px 26px rgba(239,139,60,.55);}
|
||||
|
||||
/* ---------- 棋盘 ---------- */
|
||||
#board{
|
||||
position:relative;width:min(100%,440px,58vh);min-width:220px;aspect-ratio:1/1;
|
||||
background:#16161f;border-radius:16px;
|
||||
box-shadow:0 0 0 1px rgba(255,255,255,.06), 0 24px 60px rgba(0,0,0,.55), inset 0 1px 0 rgba(255,255,255,.05);
|
||||
touch-action:none;overflow:hidden;
|
||||
}
|
||||
#cells{
|
||||
position:absolute;inset:var(--gap,8px);
|
||||
display:grid;grid-template-columns:repeat(4,1fr);grid-template-rows:repeat(4,1fr);
|
||||
gap:var(--gap,8px);
|
||||
}
|
||||
#cells div{background:rgba(255,255,255,.035);border-radius:8px;}
|
||||
|
||||
.tile{
|
||||
position:absolute;top:0;left:0;
|
||||
width:var(--cell,60px);height:var(--cell,60px);
|
||||
display:flex;align-items:center;justify-content:center;
|
||||
border-radius:8px;font-weight:800;letter-spacing:-.03em;
|
||||
font-size:calc(var(--cell,60px) * .42);
|
||||
font-variant-numeric:tabular-nums;
|
||||
transform:translate(var(--x,0px),var(--y,0px));
|
||||
transition:transform .11s cubic-bezier(.3,.8,.4,1);
|
||||
will-change:transform;
|
||||
}
|
||||
.tile.d3{font-size:calc(var(--cell,60px) * .34);}
|
||||
.tile.d4{font-size:calc(var(--cell,60px) * .27);}
|
||||
.tile.d5{font-size:calc(var(--cell,60px) * .21);}
|
||||
#board.no-anim .tile{transition:none !important;}
|
||||
|
||||
.tile[data-v="2"] {background:#32324a;color:#c9d2e8;}
|
||||
.tile[data-v="4"] {background:#3f3f5e;color:#dfe6f7;}
|
||||
.tile[data-v="8"] {background:#d98a4f;color:#fff;}
|
||||
.tile[data-v="16"] {background:#e0733a;color:#fff;}
|
||||
.tile[data-v="32"] {background:#e25a3c;color:#fff;}
|
||||
.tile[data-v="64"] {background:#e0432f;color:#fff;box-shadow:0 0 14px rgba(224,67,47,.45);}
|
||||
.tile[data-v="128"] {background:#cfae3e;color:#fff;box-shadow:0 0 18px rgba(207,174,62,.5);}
|
||||
.tile[data-v="256"] {background:#dfc44c;color:#fff;box-shadow:0 0 22px rgba(223,196,76,.6);}
|
||||
.tile[data-v="512"] {background:#e8d457;color:#fff;box-shadow:0 0 26px rgba(232,212,87,.68);}
|
||||
.tile[data-v="1024"]{background:#f2e46a;color:#2a1c00;box-shadow:0 0 30px rgba(242,228,106,.78);}
|
||||
.tile[data-v="2048"]{
|
||||
background:linear-gradient(135deg,#ffe45c,#f2a93b);color:#3a2600;
|
||||
box-shadow:0 0 40px rgba(255,228,92,.9);
|
||||
}
|
||||
.tile[data-v="4096"]{
|
||||
background:linear-gradient(135deg,#c084fc,#7c3aed);color:#fff;
|
||||
box-shadow:0 0 40px rgba(168,85,247,.85);
|
||||
}
|
||||
|
||||
.tile.appear{animation:appear .15s ease-out;}
|
||||
@keyframes appear{
|
||||
from{transform:translate(var(--x),var(--y)) scale(0);opacity:0;}
|
||||
to{transform:translate(var(--x),var(--y)) scale(1);opacity:1;}
|
||||
}
|
||||
.tile.pop{animation:popIt .17s ease;}
|
||||
@keyframes popIt{
|
||||
0%{transform:translate(var(--x),var(--y)) scale(1);}
|
||||
45%{transform:translate(var(--x),var(--y)) scale(1.16);}
|
||||
100%{transform:translate(var(--x),var(--y)) scale(1);}
|
||||
}
|
||||
.tile.dying{z-index:6;}
|
||||
|
||||
/* ---------- 覆盖层 ---------- */
|
||||
#overlay{
|
||||
position:absolute;inset:0;display:none;align-items:center;justify-content:center;
|
||||
background:rgba(12,12,20,.82);backdrop-filter:blur(5px);-webkit-backdrop-filter:blur(5px);
|
||||
border-radius:16px;padding:22px;text-align:center;z-index:20;
|
||||
}
|
||||
#overlay.show{display:flex;animation:fade .22s ease;}
|
||||
@keyframes fade{from{opacity:0;}to{opacity:1;}}
|
||||
.ov-card h2{font-size:clamp(24px,6vw,34px);font-weight:900;margin-bottom:8px;}
|
||||
.ov-card h2.win{
|
||||
background:linear-gradient(135deg,#ffe45c,#f2a93b);
|
||||
-webkit-background-clip:text;background-clip:text;color:transparent;
|
||||
filter:drop-shadow(0 0 20px rgba(242,169,59,.5));
|
||||
}
|
||||
.ov-card h2.lose{color:#e88; }
|
||||
.ov-card p{color:#8e99b5;font-size:14px;line-height:1.7;margin-bottom:20px;}
|
||||
.ov-card p b{color:#f5c451;}
|
||||
.ov-btns{display:flex;gap:10px;justify-content:center;flex-wrap:wrap;}
|
||||
|
||||
.foot{font-size:11.5px;color:#5f6982;letter-spacing:.02em;text-align:center;}
|
||||
.foot kbd{
|
||||
background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.12);
|
||||
border-radius:5px;padding:1px 6px;font-family:inherit;font-size:11px;color:#9aa5c0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="app">
|
||||
<header>
|
||||
<div class="title">2048</div>
|
||||
<div class="scores">
|
||||
<div class="score-box"><span>得分</span><strong id="score">0</strong></div>
|
||||
<div class="score-box best"><span>最高</span><strong id="best">0</strong></div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="bar">
|
||||
<p class="hint">合并相同数字,凑出 <b>2048</b>!</p>
|
||||
<div class="btns">
|
||||
<button id="btnUndo">↩ 撤销</button>
|
||||
<button id="btnNew">新游戏</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="board">
|
||||
<div id="cells"></div>
|
||||
<div id="tiles"></div>
|
||||
<div id="overlay">
|
||||
<div class="ov-card">
|
||||
<h2 id="ovTitle"></h2>
|
||||
<p id="ovText"></p>
|
||||
<div class="ov-btns">
|
||||
<button class="primary" id="ovPrimary"></button>
|
||||
<button id="ovSecondary"></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="foot"><kbd>↑</kbd><kbd>↓</kbd><kbd>←</kbd><kbd>→</kbd> 或 <kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd> 移动 · 手机滑动屏幕</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
/* ===================== 常量与元素 ===================== */
|
||||
const N = 4;
|
||||
const VEC = { up: [-1, 0], down: [1, 0], left: [0, -1], right: [0, 1] };
|
||||
const MERGE_MS = 110;
|
||||
const MAX_HISTORY = 20;
|
||||
|
||||
const boardEl = document.getElementById('board');
|
||||
const cellsEl = document.getElementById('cells');
|
||||
const tileLayer = document.getElementById('tiles');
|
||||
const scoreEl = document.getElementById('score');
|
||||
const bestEl = document.getElementById('best');
|
||||
const overlayEl = document.getElementById('overlay');
|
||||
const ovTitle = document.getElementById('ovTitle');
|
||||
const ovText = document.getElementById('ovText');
|
||||
const ovPrimary = document.getElementById('ovPrimary');
|
||||
const ovSecondary= document.getElementById('ovSecondary');
|
||||
const btnUndo = document.getElementById('btnUndo');
|
||||
const btnNew = document.getElementById('btnNew');
|
||||
|
||||
/* ===================== 状态 ===================== */
|
||||
let grid = emptyGrid();
|
||||
let score = 0;
|
||||
let best = 0;
|
||||
let won = false;
|
||||
let over = false;
|
||||
let nextId = 1;
|
||||
let history = [];
|
||||
let gap = 8, cell = 60;
|
||||
|
||||
try { const s = localStorage.getItem('best2048'); if (s) best = parseInt(s, 10) || 0; } catch (e) {}
|
||||
|
||||
/* ===================== 工具 ===================== */
|
||||
function emptyGrid() {
|
||||
return Array.from({ length: N }, () => Array(N).fill(null));
|
||||
}
|
||||
function allTiles() {
|
||||
const out = [];
|
||||
for (let r = 0; r < N; r++) for (let c = 0; c < N; c++) if (grid[r][c]) out.push(grid[r][c]);
|
||||
return out;
|
||||
}
|
||||
function inBounds(r, c) { return r >= 0 && r < N && c >= 0 && c < N; }
|
||||
|
||||
/* ===================== 尺寸与定位 ===================== */
|
||||
function layout() {
|
||||
const size = boardEl.clientWidth;
|
||||
if (!size) return;
|
||||
gap = size * 0.028;
|
||||
cell = (size - 5 * gap) / 4;
|
||||
boardEl.style.setProperty('--gap', gap + 'px');
|
||||
boardEl.style.setProperty('--cell', cell + 'px');
|
||||
|
||||
boardEl.classList.add('no-anim');
|
||||
for (const t of allTiles()) place(t, t.r, t.c, false);
|
||||
void boardEl.offsetWidth;
|
||||
boardEl.classList.remove('no-anim');
|
||||
}
|
||||
|
||||
function place(t, r, c) {
|
||||
t.r = r; t.c = c;
|
||||
t.el.style.setProperty('--x', (gap + c * (cell + gap)) + 'px');
|
||||
t.el.style.setProperty('--y', (gap + r * (cell + gap)) + 'px');
|
||||
}
|
||||
|
||||
/* ===================== 创建 / 绘制 ===================== */
|
||||
function paint(el, v) {
|
||||
el.textContent = v;
|
||||
el.dataset.v = v > 4096 ? '4096' : String(v);
|
||||
el.classList.toggle('d3', v >= 1000 && v < 10000);
|
||||
el.classList.toggle('d4', v >= 10000 && v < 100000);
|
||||
el.classList.toggle('d5', v >= 100000);
|
||||
}
|
||||
|
||||
function addTile(r, c, value, isNew) {
|
||||
const el = document.createElement('div');
|
||||
el.className = 'tile';
|
||||
const t = { id: nextId++, value, r, c, el };
|
||||
grid[r][c] = t;
|
||||
paint(el, value);
|
||||
tileLayer.appendChild(el);
|
||||
place(t, r, c);
|
||||
if (isNew) {
|
||||
el.classList.add('appear');
|
||||
setTimeout(() => el.classList.remove('appear'), 200);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
function pop(el) {
|
||||
el.classList.remove('pop');
|
||||
void el.offsetWidth;
|
||||
el.classList.add('pop');
|
||||
setTimeout(() => el.classList.remove('pop'), 200);
|
||||
}
|
||||
|
||||
function addRandom() {
|
||||
const empty = [];
|
||||
for (let r = 0; r < N; r++) for (let c = 0; c < N; c++) if (!grid[r][c]) empty.push([r, c]);
|
||||
if (!empty.length) return;
|
||||
const [r, c] = empty[(Math.random() * empty.length) | 0];
|
||||
addTile(r, c, Math.random() < 0.9 ? 2 : 4, true);
|
||||
}
|
||||
|
||||
/* ===================== 核心:移动 ===================== */
|
||||
function move(dir) {
|
||||
if (over || !VEC[dir]) return;
|
||||
|
||||
const snap = snapshot();
|
||||
const [dr, dc] = VEC[dir];
|
||||
const rOrd = dr > 0 ? [3, 2, 1, 0] : [0, 1, 2, 3];
|
||||
const cOrd = dc > 0 ? [3, 2, 1, 0] : [0, 1, 2, 3];
|
||||
|
||||
const mergedIds = new Set();
|
||||
const dying = [];
|
||||
const targets = [];
|
||||
let moved = false, gained = 0;
|
||||
|
||||
for (const r of rOrd) {
|
||||
for (const c of cOrd) {
|
||||
const t = grid[r][c];
|
||||
if (!t) continue;
|
||||
|
||||
// 滑到最远空格
|
||||
let nr = r, nc = c;
|
||||
while (true) {
|
||||
const tr = nr + dr, tc = nc + dc;
|
||||
if (!inBounds(tr, tc) || grid[tr][tc]) break;
|
||||
nr = tr; nc = tc;
|
||||
}
|
||||
|
||||
const tr = nr + dr, tc = nc + dc;
|
||||
const target = inBounds(tr, tc) ? grid[tr][tc] : null;
|
||||
|
||||
if (target && target.value === t.value && !mergedIds.has(target.id)) {
|
||||
// —— 合并 ——
|
||||
grid[r][c] = null;
|
||||
mergedIds.add(target.id);
|
||||
gained += target.value * 2;
|
||||
target.value *= 2; // 数据先更新,保证后续逻辑正确
|
||||
targets.push(target);
|
||||
|
||||
place(t, tr, tc); // 老块滑到目标格
|
||||
t.el.classList.add('dying');
|
||||
dying.push(t);
|
||||
moved = true;
|
||||
} else if (nr !== r || nc !== c) {
|
||||
// —— 平移 ——
|
||||
grid[r][c] = null;
|
||||
grid[nr][nc] = t;
|
||||
place(t, nr, nc);
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!moved) return;
|
||||
|
||||
history.push(snap);
|
||||
if (history.length > MAX_HISTORY) history.shift();
|
||||
btnUndo.disabled = false;
|
||||
|
||||
score += gained;
|
||||
updateScore();
|
||||
|
||||
// 动画结束后:移除被吞掉的老块 + 新数值弹出
|
||||
setTimeout(() => {
|
||||
for (const t of dying) t.el.remove();
|
||||
for (const tgt of targets) { paint(tgt.el, tgt.value); pop(tgt.el); }
|
||||
}, MERGE_MS);
|
||||
|
||||
addRandom();
|
||||
|
||||
if (!won && allTiles().some(t => t.value >= 2048)) {
|
||||
won = true;
|
||||
setTimeout(() => showWin(), MERGE_MS + 90);
|
||||
return;
|
||||
}
|
||||
if (isGameOver()) {
|
||||
over = true;
|
||||
setTimeout(() => showGameOver(), MERGE_MS + 90);
|
||||
}
|
||||
}
|
||||
|
||||
function isGameOver() {
|
||||
for (let r = 0; r < N; r++) {
|
||||
for (let c = 0; c < N; c++) {
|
||||
const t = grid[r][c];
|
||||
if (!t) return false;
|
||||
if (c + 1 < N && grid[r][c + 1] && grid[r][c + 1].value === t.value) return false;
|
||||
if (r + 1 < N && grid[r + 1][c] && grid[r + 1][c].value === t.value) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ===================== 分数 ===================== */
|
||||
function updateScore() {
|
||||
scoreEl.textContent = score;
|
||||
if (score > best) {
|
||||
best = score;
|
||||
try { localStorage.setItem('best2048', String(best)); } catch (e) {}
|
||||
}
|
||||
bestEl.textContent = best;
|
||||
}
|
||||
|
||||
/* ===================== 撤销 ===================== */
|
||||
function snapshot() {
|
||||
return {
|
||||
v: grid.map(row => row.map(t => (t ? t.value : 0))),
|
||||
score
|
||||
};
|
||||
}
|
||||
|
||||
function undo() {
|
||||
if (!history.length) return;
|
||||
const snap = history.pop();
|
||||
restore(snap);
|
||||
btnUndo.disabled = history.length === 0;
|
||||
}
|
||||
|
||||
function restore(snap) {
|
||||
tileLayer.innerHTML = '';
|
||||
grid = emptyGrid();
|
||||
for (let r = 0; r < N; r++) {
|
||||
for (let c = 0; c < N; c++) {
|
||||
const v = snap.v[r][c];
|
||||
if (v) addTile(r, c, v, false);
|
||||
}
|
||||
}
|
||||
score = snap.score;
|
||||
scoreEl.textContent = score;
|
||||
over = false;
|
||||
hideOverlay();
|
||||
}
|
||||
|
||||
/* ===================== 新游戏 ===================== */
|
||||
function newGame() {
|
||||
tileLayer.innerHTML = '';
|
||||
grid = emptyGrid();
|
||||
score = 0;
|
||||
won = false;
|
||||
over = false;
|
||||
history = [];
|
||||
btnUndo.disabled = true;
|
||||
hideOverlay();
|
||||
scoreEl.textContent = 0;
|
||||
bestEl.textContent = best;
|
||||
addRandom();
|
||||
addRandom();
|
||||
}
|
||||
|
||||
/* ===================== 覆盖层 ===================== */
|
||||
function showOverlay(cfg) {
|
||||
ovTitle.className = cfg.cls || '';
|
||||
ovTitle.textContent = cfg.title;
|
||||
ovText.innerHTML = cfg.text;
|
||||
ovPrimary.textContent = cfg.primary;
|
||||
ovPrimary.onclick = cfg.onPrimary;
|
||||
if (cfg.secondary) {
|
||||
ovSecondary.style.display = '';
|
||||
ovSecondary.textContent = cfg.secondary;
|
||||
ovSecondary.onclick = cfg.onSecondary;
|
||||
} else {
|
||||
ovSecondary.style.display = 'none';
|
||||
}
|
||||
overlayEl.classList.add('show');
|
||||
}
|
||||
function hideOverlay() { overlayEl.classList.remove('show'); }
|
||||
|
||||
function showWin() {
|
||||
showOverlay({
|
||||
cls: 'win',
|
||||
title: '🎉 2048 达成!',
|
||||
text: '得分 <b>' + score + '</b><br>还能走多远?',
|
||||
primary: '继续挑战',
|
||||
onPrimary: hideOverlay,
|
||||
secondary: '重新开始',
|
||||
onSecondary: newGame
|
||||
});
|
||||
}
|
||||
|
||||
function showGameOver() {
|
||||
showOverlay({
|
||||
cls: 'lose',
|
||||
title: '💀 无路可走',
|
||||
text: '本局得分 <b>' + score + '</b><br>最高纪录 <b>' + best + '</b>',
|
||||
primary: '再来一局',
|
||||
onPrimary: newGame,
|
||||
secondary: '撤销一步',
|
||||
onSecondary: () => { undo(); btnUndo.disabled = history.length === 0; }
|
||||
});
|
||||
}
|
||||
|
||||
/* ===================== 输入:键盘 ===================== */
|
||||
const KEYMAP = {
|
||||
ArrowUp: 'up', ArrowDown: 'down', ArrowLeft: 'left', ArrowRight: 'right',
|
||||
w: 'up', s: 'down', a: 'left', d: 'right',
|
||||
W: 'up', S: 'down', A: 'left', D: 'right'
|
||||
};
|
||||
|
||||
window.addEventListener('keydown', (e) => {
|
||||
const dir = KEYMAP[e.key];
|
||||
if (dir) {
|
||||
e.preventDefault();
|
||||
if (overlayEl.classList.contains('show')) return;
|
||||
move(dir);
|
||||
return;
|
||||
}
|
||||
if (e.key === 'z' || e.key === 'Z') { e.preventDefault(); undo(); }
|
||||
if (e.key === 'r' || e.key === 'R') { e.preventDefault(); newGame(); }
|
||||
});
|
||||
|
||||
/* ===================== 输入:触摸滑动 ===================== */
|
||||
let touchStart = null;
|
||||
boardEl.addEventListener('touchstart', (e) => {
|
||||
if (e.touches.length !== 1) { touchStart = null; return; }
|
||||
touchStart = { x: e.touches[0].clientX, y: e.touches[0].clientY };
|
||||
}, { passive: true });
|
||||
|
||||
boardEl.addEventListener('touchmove', (e) => { e.preventDefault(); }, { passive: false });
|
||||
|
||||
boardEl.addEventListener('touchend', (e) => {
|
||||
if (!touchStart) return;
|
||||
const t = e.changedTouches[0];
|
||||
const dx = t.clientX - touchStart.x;
|
||||
const dy = t.clientY - touchStart.y;
|
||||
touchStart = null;
|
||||
if (overlayEl.classList.contains('show')) return;
|
||||
if (Math.max(Math.abs(dx), Math.abs(dy)) < 24) return;
|
||||
if (Math.abs(dx) > Math.abs(dy)) move(dx > 0 ? 'right' : 'left');
|
||||
else move(dy > 0 ? 'down' : 'up');
|
||||
}, { passive: true });
|
||||
|
||||
/* ===================== 按钮 ===================== */
|
||||
btnNew.addEventListener('click', newGame);
|
||||
btnUndo.addEventListener('click', undo);
|
||||
|
||||
/* ===================== 初始化 ===================== */
|
||||
for (let i = 0; i < N * N; i++) cellsEl.appendChild(document.createElement('div'));
|
||||
|
||||
bestEl.textContent = best;
|
||||
btnUndo.disabled = true;
|
||||
|
||||
layout();
|
||||
newGame();
|
||||
|
||||
if (typeof ResizeObserver !== 'undefined') {
|
||||
new ResizeObserver(layout).observe(boardEl);
|
||||
} else {
|
||||
window.addEventListener('resize', layout);
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user