Atomic Studio
/
スネーク(テンプレートから)
0部品・0〜数百トークン
↩ 戻す
履歴
複製
💬 ことばで直す
▶ Run
🔗 共有
🚀 デプロイ
⏹
☰
▷ プレビュー
</> コード
🖥 PC
📱 スマホ
↻ 新タブ
atomic.studio/s/3725
📄 index.html
コードを直接編集して「適用」でプレビューに反映
適用 ▷
<!doctype html><html lang="ja"><head><meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"><title>スネークゲーム</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">スネークゲーム</h1><div class="hud" id="hud">スコア <span id="sc">0</span></div> <canvas id="cv" width="400" height="400"></canvas> <div class="hint" id="hint">← ↑ → ↓ / スワイプ で操作</div><button class="btn" id="restart">もう一度</button> </div> <script id="game-data" type="application/json">{"title": "スネークゲーム", "bg": "#0b1020", "snake": "#34d399", "food": "#f59e0b", "speed": 8, "grid": 20}</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 cv=document.getElementById('cv'),x=cv.getContext('2d');const N=DATA.grid,S=cv.width/N; let snake,dir,nx,food,sc,alive,acc=0; function reset(){snake=[{x:10,y:10}];dir={x:1,y:0};nx=dir;food=rf();sc=0;alive=1;document.getElementById('sc').textContent=0;} function rf(){return{x:(Math.random()*N)|0,y:(Math.random()*N)|0};} function step(){dir=nx;const h={x:(snake[0].x+dir.x+N)%N,y:(snake[0].y+dir.y+N)%N}; if(snake.some(s=>s.x===h.x&&s.y===h.y)){alive=0;return;}snake.unshift(h); if(h.x===food.x&&h.y===food.y){sc++;document.getElementById('sc').textContent=sc;food=rf();}else snake.pop();} function draw(){x.fillStyle=DATA.bg;x.fillRect(0,0,cv.width,cv.height); x.fillStyle=DATA.food;x.fillRect(food.x*S,food.y*S,S-1,S-1); x.fillStyle=DATA.snake;snake.forEach(s=>x.fillRect(s.x*S,s.y*S,S-1,S-1)); if(!alive){x.fillStyle='#fff';x.font='24px sans-serif';x.textAlign='center';x.fillText('GAME OVER',cv.width/2,cv.height/2);}} function loop(t){acc++;if(acc>=(60/DATA.speed|0)){acc=0;if(alive)step();}draw();requestAnimationFrame(loop);} function set(dx,dy){if(dx===-dir.x&&dy===-dir.y)return;nx={x:dx,y:dy};} window.addEventListener('keydown',e=>{({ArrowLeft:[-1,0],ArrowRight:[1,0],ArrowUp:[0,-1],ArrowDown:[0,1]}[e.key]||[]).length&&(e.preventDefault(),set(...{ArrowLeft:[-1,0],ArrowRight:[1,0],ArrowUp:[0,-1],ArrowDown:[0,1]}[e.key]));}); let tx,ty;cv.addEventListener('touchstart',e=>{tx=e.touches[0].clientX;ty=e.touches[0].clientY;}); cv.addEventListener('touchend',e=>{const dx=e.changedTouches[0].clientX-tx,dy=e.changedTouches[0].clientY-ty; if(Math.abs(dx)>Math.abs(dy))set(dx>0?1:-1,0);else set(0,dy>0?1:-1);}); document.getElementById('restart').onclick=reset;reset();requestAnimationFrame(loop); window.__state=()=>({sc,alive,len:snake.length,head:snake[0]});window.__input=()=>{set(0,-1);step();}; </script></body></html>
▸ コンソール
0部品・0〜数百トークン
✕