Atomic Studio
/
2048パズル(テンプレートから)
0部品・0〜数百トークン
↩ 戻す
履歴
複製
💬 ことばで直す
▶ Run
🔗 共有
🚀 デプロイ
⏹
☰
▷ プレビュー
</> コード
🖥 PC
📱 スマホ
↻ 新タブ
atomic.studio/s/3704
📄 index.html
コードを直接編集して「適用」でプレビューに反映
適用 ▷
<!doctype html><html lang="ja"><head><meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"><title>2048パズル</title> <style> html,body{margin:0;height:100%;background:#0e1525;color:#f5f9fc;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif} .wrap{min-height:100%;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:12px;padding:16px;box-sizing:border-box} h1{font-size:18px;margin:0;font-weight:700} .hud{font-size:14px;color:#9da9bd} .hint{font-size:12px;color:#6b7689} canvas{background:var(--bg,#11131f);border-radius:14px;box-shadow:0 12px 48px #0008;max-width:96vw;height:auto;touch-action:none} .btn{background:#6366f1;color:#fff;border:0;border-radius:10px;padding:8px 16px;font-size:14px;font-weight:600;cursor:pointer} #board{display:grid;gap:6px} .cell{display:flex;align-items:center;justify-content:center;border-radius:8px;font-weight:700;user-select:none;cursor:pointer} </style></head><body><div class="wrap"> <h1 id="title">2048パズル</h1><div class="hud" id="hud">スコア <span id="sc">0</span></div> <div id="board" style="grid-template-columns:repeat(4,64px);grid-auto-rows:64px;background:#0b1020;padding:6px;border-radius:12px"></div> <div class="hint" id="hint">← ↑ → ↓ / スワイプ で寄せる</div><button class="btn" id="restart">もう一度</button> </div> <script id="game-data" type="application/json">{"title": "2048パズル", "bg": "#1c2333", "tile": "#6366f1", "size": 4}</script> <script> const DATA=JSON.parse(document.getElementById('game-data').textContent); document.getElementById('title').textContent=DATA.title||document.title; if(DATA.bg)document.documentElement.style.setProperty('--bg',DATA.bg); const N=DATA.size,bd=document.getElementById('board');let g,sc; function blank(){return Array.from({length:N*N},()=>0);} function add(){const e=[];g.forEach((v,i)=>{if(!v)e.push(i);});if(e.length)g[e[(Math.random()*e.length)|0]]=Math.random()<.9?2:4;} function reset(){g=blank();add();add();sc=0;render();} function line(a){let r=a.filter(v=>v);for(let i=0;i<r.length-1;i++)if(r[i]===r[i+1]){r[i]*=2;sc+=r[i];r[i+1]=0;}r=r.filter(v=>v);while(r.length<N)r.push(0);return r;} function move(dir){const o=g.slice();for(let i=0;i<N;i++){let a=[];for(let j=0;j<N;j++){const idx=dir==='L'||dir==='R'?i*N+j:j*N+i;a.push(g[idx]);} if(dir==='R'||dir==='D')a.reverse();a=line(a);if(dir==='R'||dir==='D')a.reverse(); for(let j=0;j<N;j++){const idx=dir==='L'||dir==='R'?i*N+j:j*N+i;g[idx]=a[j];}} if(g.some((v,i)=>v!==o[i])){add();}document.getElementById('sc').textContent=sc;render();} function render(){bd.innerHTML='';g.forEach(v=>{const d=document.createElement('div');d.className='cell'; d.style.background=v?DATA.tile:'#11182b';d.style.color='#fff';d.style.fontSize='22px'; d.textContent=v||'';d.style.opacity=v?(0.4+Math.min(1,Math.log2(v)/11)):1;bd.appendChild(d);});} window.addEventListener('keydown',e=>{const m={ArrowLeft:'L',ArrowRight:'R',ArrowUp:'U',ArrowDown:'D'}[e.key];if(m){e.preventDefault();move(m);}}); let tx,ty;document.addEventListener('touchstart',e=>{tx=e.touches[0].clientX;ty=e.touches[0].clientY;}); document.addEventListener('touchend',e=>{const dx=e.changedTouches[0].clientX-tx,dy=e.changedTouches[0].clientY-ty; if(Math.abs(dx)>Math.abs(dy))move(dx>0?'R':'L');else move(dy>0?'D':'U');}); document.getElementById('restart').onclick=reset;reset(); window.__state=()=>({sc,sum:g.reduce((a,b)=>a+b,0),tiles:g.filter(v=>v).length}); window.__input=()=>{move('L');move('U');move('R');move('D');}; </script></body></html>
▸ コンソール
0部品・0〜数百トークン
✕