HTMLで簡単なスネークゲームを作成してください。 ただしUIには最もこだわり、そしてバグはマイナスの評価になります。…
- User
HTMLで簡単なスネークゲームを作成してください。 ただしUIには最もこだわり、そしてバグはマイナスの評価になります。 あなたはデザイナーとして最もプレイしやすく、最も魅了されるようなデザインを作成します。 例えばクリア報酬でスキンが解放されるなどはプレイヤーが長期的にプレイする魅力になります。 他にもどの環境からでもプレイできるようにレスポンシブなデザインを意識することも非常に大切でしょう。 他にも獲得したポイントの表示を一定ポイントごとに見た目を変更したりすることで高揚心を煽るのもいいかもしれません。 一つのコードブロックにまとめて提出してください。
Assistant
はい、承知いたしました。UIデザインとプレイ体験にこだわり、レスポンシブで魅力的なスネークゲームをHTML、CSS、JavaScriptで実装します。クリア報酬(スキン解放)やスコアに応じた演出も盛り込みました。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Snake Game Deluxe</title> <style> /* 基本スタイル */ body { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; background-color: #1a1a2e; /* 深い青紫 */ color: #e0e0e0; font-family: 'Poppins', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* モダンなフォント */ margin: 0; overflow: hidden; /* スクロールバー防止 */ box-sizing: border-box; padding: 20px; } /* Google Fonts読み込み */ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600;700&family=Press+Start+2P&display=swap'); /* ゲームタイトル */ h1 { font-family: 'Press Start 2P', cursive; /* レトロ風 */ color: #00f0ff; /* ネオンブルー */ text-shadow: 0 0 10px #00f0ff, 0 0 20px #00f0ff, 0 0 30px #00ffff; margin-bottom: 30px; font-size: clamp(2rem, 6vw, 3.5rem); /* レスポンシブフォント */ letter-spacing: 2px; } /* ゲームコンテナ */ .game-container { display: flex; flex-direction: column; align-items: center; width: 95%; max-width: 800px; /* 最大幅 */ background-color: #16213e; /* ネイビー */ border-radius: 15px; padding: 25px; box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5), 0 0 60px rgba(0, 150, 255, 0.3); box-sizing: border-box; border: 3px solid #00f0ff; } /* キャンバス */ canvas { display: block; background-color: #0f0f1e; /* ダークネイビー */ border: 4px solid #ffffff; border-radius: 10px; box-shadow: inset 0 0 20px rgba(0, 240, 255, 0.4), 0 0 10px rgba(255, 255, 255, 0.3); /* canvasサイズはJSで設定 */ max-width: 100%; /* コンテナに収まるように */ aspect-ratio: 1 / 1; /* 正方形を維持 (JSで調整されるが保険) */ } /* UIセクション (スコアとボタン) */ .ui-section { display: flex; justify-content: space-between; align-items: center; width: 100%; margin-top: 20px; flex-wrap: wrap; /* 必要に応じて折り返し */ gap: 15px; box-sizing: border-box; } /* スコア表示 */ #score-display { font-family: 'Poppins', sans-serif; font-size: clamp(1.2rem, 4vw, 1.8rem); font-weight: 700; color: #ffffff; padding: 12px 25px; background-color: rgba(255, 255, 255, 0.15); border-radius: 12px; transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1); /* スムーズな変化 */ min-width: 180px; /* 最低幅 */ text-align: center; border: 2px solid transparent; } /* スコアに応じたスタイル変更クラス */ .score-high { background-color: rgba(255, 200, 0, 0.75); /* ゴールド */ color: #1a1a2e; /* ダーク背景 */ box-shadow: 0 0 20px rgba(255, 200, 0, 0.7); transform: scale(1.05) translateY(-3px); border-color: #ffd700; } .score-very-high { background-color: rgba(255, 100, 100, 0.85); /* レッド */ color: #fff; box-shadow: 0 0 25px rgba(255, 100, 100, 0.85); animation: pulse-red 1.5s infinite ease-in-out; transform: scale(1.1) translateY(-4px) rotate(-2deg); border-color: #ff4444; } .score-extreme { background-color: rgba(150, 50, 255, 0.9); /* パープル */ color: #fff; box-shadow: 0 0 30px rgba(150, 50, 255, 0.9); animation: vibe-purple 1.2s infinite alternate ease-in-out; transform: scale(1.15) translateY(-5px) rotate(3deg); border-color: #9c27b0; } @keyframes pulse-red { 0% { transform: scale(1.1) translateY(-4px) rotate(-2deg); box-shadow: 0 0 25px rgba(255, 100, 100, 0.85);} 50% { transform: scale(1.15) translateY(-6px) rotate(-2deg); box-shadow: 0 0 35px rgba(255, 80, 80, 0.9);} 100% { transform: scale(1.1) translateY(-4px) rotate(-2deg); box-shadow: 0 0 25px rgba(255, 100, 100, 0.85);} } @keyframes vibe-purple { 0% { transform: scale(1.15) translateY(-5px) rotate(3deg); box-shadow: 0 0 30px rgba(150, 50, 255, 0.9);} 100% { transform: scale(1.15) translateY(-7px) rotate(-3deg); box-shadow: 0 0 40px rgba(180, 70, 255, 1);} } /* ボタン */ button { font-family: 'Poppins', sans-serif; font-size: clamp(0.9rem, 3.5vw, 1.2rem); padding: 10px 30px; background-image: linear-gradient(45deg, #00f0ff, #00aaff); /* グラデーション */ color: #16213e; /* ネイビー */ border: none; border-radius: 10px; cursor: pointer; font-weight: 700; transition: all 0.3s ease; box-shadow: 0 5px 20px rgba(0, 240, 255, 0.5), 0 3px 8px rgba(0, 160, 255, 0.3); transform: translateY(-2px); } button:hover { box-shadow: 0 8px 25px rgba(0, 240, 255, 0.7), 0 5px 12px rgba(0, 160, 255, 0.4); transform: translateY(-5px) scale(1.03); filter: brightness(1.1); } button:active { transform: translateY(-1px) scale(1); /* クリック時の押し込み効果 */ box-shadow: 0 3px 10px rgba(0, 240, 255, 0.4); } /* スキン選択エリア */ .skin-selector { margin-top: 30px; text-align: center; width: 100%; background-color: rgba(22, 33, 62, 0.5); /* 背景に少し馴染ませる */ padding: 15px; border-radius: 10px; border: 1px solid rgba(0, 240, 255, 0.2); } .skin-selector h3 { font-family: 'Poppins', sans-serif; margin-bottom: 15px; color: #00f0ff; font-size: 1.3rem; text-shadow: 0 0 8px #00f0ff; } .skins { display: flex; justify-content: center; gap: 15px; flex-wrap: wrap; } .skin-item { cursor: pointer; border: 3px solid transparent; border-radius: 12px; padding: 6px; transition: all 0.3s ease-out; background-color: #1f2a4a; /* 少し明るいネイビー */ opacity: 0.75; display: flex; flex-direction: column; align-items: center; width: 80px; /* サイズ固定 */ } .skin-item.locked { opacity: 0.3; cursor: not-allowed; border-color: #888; filter: grayscale(1) brightness(0.5); } .skin-item.selected { border-color: #00ff88; /* 明るい緑 */ transform: scale(1.15) translateY(-5px); opacity: 1; background-color: #00ff88; /* 選んだスキンは背景色を変える */ box-shadow: 0 0 15px rgba(0, 255, 136, 0.8); } .skin-item div.color-sample { /* 代表的な色 */ width: 45px; height: 45px; border-radius: 8px; margin-bottom: 8px; border: 2px solid rgba(255, 255, 255, 0.5); } .skin-item span.skin-name { font-size: 0.85rem; font-weight: 600; color: #ccc; } .skin-item.selected span.skin-name { color: #1a1a2e; /* 選んだスキンに合わせて文字色変更 */ } /* メッセージ表示用アニメーション */ @keyframes fadeOut { from { opacity: 1; transform: translate(-50%, -50%) scale(1.5); } to { opacity: 0; transform: translate(-50%, -50%) scale(1.8); } } /* レスポンシブ調整 */ @media (max-width: 600px) { h1 { text-align: center; margin-bottom: 20px; } .ui-section { flex-direction: column; /* 縦積み */ align-items: center; } #score-display { margin-bottom: 15px; min-width: auto; /* 幅を固定しない */ width: 80%; } button { width: 80%; margin-top: 10px; } .skin-selector h3 { font-size: 1.1rem; } .skin-item { width: 70px; } .skin-item div.color-sample { width: 40px; height: 40px; } .skin-item span.skin-name { font-size: 0.75rem; } } </style> </head> <body> <h1>Snake Game Deluxe</h1> <div class="game-container"> <canvas id="gameCanvas"></canvas> <div class="ui-section"> <div id="score-display">Score: 0</div> <button id="startButton">Start Game</button> </div> <div class="skin-selector"> <h3>Choose Your Snake</h3> <div id="skins-container" class="skins"> <!-- スキンはJSで生成 --> </div> </div> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const scoreDisplay = document.getElementById('score-display'); const startButton = document.getElementById('startButton'); const skinsContainer = document.getElementById('skins-container'); // --- ゲーム設定 --- let boxSize; // マスサイズ let canvasGridSize = {cols: 0, rows: 0}; // キャンバスのマス数 let gameLoopInterval; let snake; // [ {x: 10, y: 10}, ... ] let direction; // 'up', 'down', 'left', 'right' let food; let score; let isGameOver; let isPaused; // --- スキンデータ --- const skins = [ { id: 'classic', name: 'Classic', snakeColor: '#2ecc71', headColor: '#27ae60', foodColor: '#e74c3c', borderColor: '#2ecc71' }, // 緑 { id: 'retro', name: 'Retro', snakeColor: '#f1c40f', headColor: '#d4ac0d', foodColor: '#e67e22', borderColor: '#f39c12' }, // 黄色 { id: 'galaxy', name: 'Galaxy', snakeColor: '#9b59b6', headColor: '#8e44ad', foodColor: '#3498db', borderColor: '#9c27b0' }, // 紫 { id: 'ocean', name: 'Ocean', snakeColor: '#3498db', headColor: '#2980b9', foodColor: '#1abc9c', borderColor: '#4db6ac' }, // 青 { id: 'fire', name: 'Fire', snakeColor: '#e74c3c', headColor: '#c0392b', foodColor: '#f39c12', borderColor: '#e67e22' } // 赤 ]; // スキンアンロック条件 (スコア) const unlockScores = { 'retro': 50, 'galaxy': 150, 'ocean': 300, 'fire': 600 }; let currentSkinId = 'classic'; let unlockedSkinIds = ['classic']; // 初期状態 // --- 初期化 --- function initGame() { resizeCanvasAndCalculateBoxSize(); // キャンバスサイズ設定 // Snake初期化 snake = []; const startX = Math.floor(canvasGridSize.cols / 2); const startY = Math.floor(canvasGridSize.rows / 2); for (let i = 0; i < 3; i++) { // 初期長さ3 snake.push({ x: startX - i, y: startY }); } direction = 'right'; // 初期方向 spawnFood(); score = 0; isGameOver = false; isPaused = false; scoreDisplay.textContent = `Score: ${score}`; updateScoreDisplayClass(); // スコア表示のクラス更新 startButton.textContent = 'Restart Game'; startButton.style.display = 'block'; // 開始/再開ボタンを常に表示 // イベントリスナー再設定 document.removeEventListener('keydown', handleInput); document.addEventListener('keydown', handleInput); setupTouchControls(); // タッチコントロール設定 // キャンバスが初期化されたらループ開始 if (gameLoopInterval) cancelAnimationFrame(gameLoopInterval); gameLoop(); } // --- キャンバスサイズとマスサイズ計算 --- function resizeCanvasAndCalculateBoxSize() { const containerPadding = 50; // コンテナ内の余白 const uiHeight = 150; // UI要素の高さを考慮 const availableWidth = window.innerWidth * 0.95 - containerPadding * 2; const availableHeight = window.innerHeight * 0.7 - uiHeight; // canvasの高さを制限 const maxSize = Math.min(availableWidth, availableHeight); const preferredBox = 20; // 目標とするマスサイズ // マス数を計算 (最大マス数制限も考慮) const maxCols = Math.floor(maxSize / preferredBox); const maxRows = Math.floor(maxSize / preferredBox); // ちょうど良いマスサイズを探す let calculatedBoxSize = preferredBox; canvasGridSize.cols = Math.floor(maxSize / calculatedBoxSize); canvasGridSize.rows = Math.floor(maxSize / calculatedBoxSize); // 実際のキャンバスサイズを決定 (正方形にする) const actualCanvasSize = calculatedBoxSize * Math.min(canvasGridSize.cols, canvasGridSize.rows); canvas.width = actualCanvasSize; canvas.height = actualCanvasSize; boxSize = actualCanvasSize / Math.min(canvasGridSize.cols, canvasGridSize.rows); // boxSizeを再計算 // gridサイズを更新 canvasGridSize.cols = Math.floor(canvas.width / boxSize); canvasGridSize.rows = Math.floor(canvas.height / boxSize); // CSS変数でキャンバスサイズを反映 (スキン選択の幅などにも影響させるため) document.documentElement.style.setProperty('--canvas-width', `${canvas.width}px`); } // --- ゲームループ --- function gameLoop() { if (isGameOver || isPaused) return; updateGame(); drawGame(); gameLoopInterval = requestAnimationFrame(gameLoop); } // --- ゲーム状態更新 --- function updateGame() { let head = { ...snake[0] }; // 現在の頭の位置 // 方向に基づいて頭を移動 switch (direction) { case 'up': head.y--; break; case 'down': head.y++; break; case 'left': head.x--; break; case 'right': head.x++; break; } // 壁衝突判定 if (head.x < 0 || head.x >= canvasGridSize.cols || head.y < 0 || head.y >= canvasGridSize.rows) { gameOver(); return; } // 自分自身への衝突判定 for (let i = 1; i < snake.length; i++) { // 頭自身(i=0)は除く if (head.x === snake[i].x && head.y === snake[i].y) { gameOver(); return; } } // 蛇の本体を更新 (新しい頭を追加) snake.unshift(head); // 餌を食べたか判定 if (head.x === food.x && head.y === food.y) { score++; updateScoreDisplay(); spawnFood(); // 新しい餌を生成 // 蛇は伸びるので、尻尾削除はしない } else { // 餌を食べなかったら、尻尾を削除して移動を実現 snake.pop(); } } // --- ゲーム描画 --- function drawGame() { ctx.clearRect(0, 0, canvas.width, canvas.height); // キャンバス背景とグリッド線 ctx.fillStyle = '#0f0f1e'; ctx.fillRect(0, 0, canvas.width, canvas.height); const currentSkin = skins.find(s => s.id === currentSkinId); // グリッド線描画 ctx.strokeStyle = 'rgba(0, 240, 255, 0.1)'; ctx.lineWidth = 1; for (let i = 0; i <= canvasGridSize.cols; i++) { ctx.beginPath(); ctx.moveTo(i * boxSize, 0); ctx.lineTo(i * boxSize, canvas.height); ctx.stroke(); } for (let i = 0; i <= canvasGridSize.rows; i++) { ctx.beginPath(); ctx.moveTo(0, i * boxSize); ctx.lineTo(canvas.width, i * boxSize); ctx.stroke(); } // 餌描画 ctx.fillStyle = currentSkin.foodColor; ctx.shadowColor = currentSkin.foodColor; ctx.shadowBlur = 15; // 餌を光らせる ctx.fillRect(food.x * boxSize, food.y * boxSize, boxSize, boxSize); ctx.shadowBlur = 0; // シャドウをリセット // 蛇描画 ctx.shadowBlur = 10; // 蛇の影 snake.forEach((segment, index) => { ctx.fillStyle = (index === 0) ? currentSkin.headColor : currentSkin.snakeColor; ctx.shadowColor = ctx.fillStyle; // 体の色と同じ影 ctx.fillRect(segment.x * boxSize, segment.y * boxSize, boxSize, boxSize); }); ctx.shadowBlur = 0; // シャドウリセット // 蛇の縁取り (オプション) ctx.strokeStyle = currentSkin.borderColor; ctx.lineWidth = boxSize / 10; // マスサイズの1/10の太さ snake.forEach((segment, index) => { ctx.strokeRect(segment.x * boxSize + ctx.lineWidth / 2, segment.y * boxSize + ctx.lineWidth / 2, boxSize - ctx.lineWidth, boxSize - ctx.lineWidth); }); } // --- 餌の生成 --- function spawnFood() { let newFoodPosition; let collision; do { newFoodPosition = { x: Math.floor(Math.random() * canvasGridSize.cols), y: Math.floor(Math.random() * canvasGridSize.rows) }; collision = snake.some(segment => segment.x === newFoodPosition.x && segment.y === newFoodPosition.y); } while (collision); food = newFoodPosition; } // --- スコア表示更新とクラス適用 --- function updateScoreDisplay() { scoreDisplay.textContent = `Score: ${score}`; updateScoreDisplayClass(); // スコアに応じてクラスを更新 // スキンアンロックチェック for (const skinId in unlockScores) { if (score >= unlockScores[skinId] && !unlockedSkinIds.includes(skinId)) { unlockedSkinIds.push(skinId); renderSkins(); // スキンリストを再描画 displayTemporaryMessage(`"${skins.find(s => s.id === skinId).name}" Skin Unlocked! ✨`, '#00ff88'); } } } function updateScoreDisplayClass() { // 既存のスコアクラスを全て削除 scoreDisplay.classList.remove('score-high', 'score-very-high', 'score-extreme'); // スコアに応じて適切なクラスを追加 if (score >= 600) scoreDisplay.classList.add('score-extreme'); else if (score >= 300) scoreDisplay.classList.add('score-very-high'); else if (score >= 50) scoreDisplay.classList.add('score-high'); } // --- 一時的なメッセージ表示 --- function displayTemporaryMessage(message, color) { const msgDiv = document.createElement('div'); msgDiv.textContent = message; msgDiv.style.cssText = ` position: fixed; /* 画面中央に固定 */ top: 50%; left: 50%; transform: translate(-50%, -50%) scale(1.3); background-color: rgba(0, 0, 0, 0.8); color: ${color || '#fff'}; padding: 15px 30px; border-radius: 15px; font-size: 1.1rem; font-weight: bold; z-index: 1000; animation: fadeOut 2s ease-out forwards, scaleUp 0.4s ease-out forwards; text-align: center; border: 2px solid ${color || '#fff'}; box-shadow: 0 0 20px ${color || '#fff'}50; `; document.body.appendChild(msgDiv); setTimeout(() => { msgDiv.remove(); }, 2000); // 2秒後に削除 } // --- ゲームオーバー処理 --- function gameOver() { isGameOver = true; cancelAnimationFrame(gameLoopInterval); startButton.textContent = 'Play Again?'; startButton.style.display = 'block'; // ゲームオーバー表示 ctx.fillStyle = 'rgba(26, 26, 46, 0.8)'; // ダーク背景 ctx.fillRect(canvas.width * 0.15, canvas.height * 0.3, canvas.width * 0.7, canvas.height * 0.4); ctx.font = `bold ${Math.floor(boxSize * 1.2)}px 'Press Start 2P', cursive`; ctx.fillStyle = '#ff4444'; // 赤 ctx.textAlign = 'center'; ctx.fillText('GAME OVER', canvas.width / 2, canvas.height / 2 - boxSize * 0.5); ctx.font = `${Math.floor(boxSize)}px 'Poppins', sans-serif`; ctx.fillStyle = '#fff'; ctx.fillText(`Final Score: ${score}`, canvas.width / 2, canvas.height / 2 + boxSize * 0.5); // 操作解除 document.removeEventListener('keydown', handleInput); canvas.removeEventListener('touchstart', handleTouchStart); canvas.removeEventListener('touchend', handleTouchEnd); } // --- 入力処理 --- function handleInput(event) { const key = event.key.toLowerCase(); const newDirection = (key => { if (key === 'arrowup' || key === 'w') return 'up'; if (key === 'arrowdown' || key === 's') return 'down'; if (key === 'arrowleft' || key === 'a') return 'left'; if (key === 'arrowright' || key === 'd') return 'right'; if (key === 'p' || key === ' ') return 'pause'; // P または Space で一時停止 return null; })(key); if (newDirection === 'pause') { togglePause(); return; } // 方向転換時の無効な入力(逆方向)を防ぐ if (newDirection) { if (newDirection === 'up' && direction !== 'down') direction = 'up'; else if (newDirection === 'down' && direction !== 'up') direction = 'down'; else if (newDirection === 'left' && direction !== 'right') direction = 'left'; else if (newDirection === 'right' && direction !== 'left') direction = 'right'; } } // --- 一時停止処理 --- function togglePause() { if (isGameOver) return; isPaused = !isPaused; if (isPaused) { cancelAnimationFrame(gameLoopInterval); // 一時停止表示 ctx.fillStyle = 'rgba(0, 0, 0, 0.7)'; ctx.fillRect(canvas.width * 0.25, canvas.height * 0.3, canvas.width * 0.5, canvas.height * 0.4); ctx.font = `bold ${Math.floor(boxSize * 1.5)}px 'Press Start 2P', cursive`; ctx.fillStyle = '#f0f000'; // 黄色 ctx.fillText('PAUSED', canvas.width / 2, canvas.height / 2); startButton.textContent = 'Resume'; startButton.style.display = 'block'; // タッチイベントリスナーを一時停止状態にする(または無効化) } else { startButton.style.display = 'none'; // 再開時は非表示 gameLoop(); // ループ再開 // タッチイベントリスナーを有効化 } } // --- スキン選択UI描画 --- function renderSkins() { skinsContainer.innerHTML = ''; // クリーンアップ skins.forEach(skin => { const skinItem = document.createElement('div'); skinItem.classList.add('skin-item'); skinItem.dataset.skinId = skin.id; const colorSample = document.createElement('div'); colorSample.classList.add('color-sample'); colorSample.style.backgroundColor = skin.snakeColor; skinItem.appendChild(colorSample); const skinName = document.createElement('span'); skinName.classList.add('skin-name'); skinName.textContent = skin.name; skinItem.appendChild(skinName); // アンロック状態の処理 if (!unlockedSkinIds.includes(skin.id)) { skinItem.classList.add('locked'); skinItem.title = `Locked (Unlock at score ${unlockScores[skin.id]})`; } // 選択中のスキン処理 if (skin.id === currentSkinId) { skinItem.classList.add('selected'); } // スキン選択時のイベントリスナー skinItem.addEventListener('click', () => { if (unlockedSkinIds.includes(skin.id)) { // アンロック済みの場合のみ currentSkinId = skin.id; renderSkins(); // UI更新のため再描画 if (!isGameOver && !isPaused) { displayTemporaryMessage(`Skin changed to "${skin.name}"!`, '#00f0ff'); } } }); skinsContainer.appendChild(skinItem); }); } // --- タッチ操作 --- let touchStartX = 0, touchStartY = 0, touchEndX = 0, touchEndY = 0; function setupTouchControls() { canvas.addEventListener('touchstart', handleTouchStart, { passive: false }); canvas.addEventListener('touchend', handleTouchEnd, { passive: false }); } function handleTouchStart(e) { if (isGameOver || isPaused) return; // 操作無効 e.preventDefault(); const touch = e.touches[0]; touchStartX = touch.clientX; touchStartY = touch.clientY; } function handleTouchEnd(e) { if (isGameOver || isPaused) return; e.preventDefault(); const touch = e.changedTouches[0]; touchEndX = touch.clientX; touchEndY = touch.clientY; const dx = touchEndX - touchStartX; const dy = touchEndY - touchStartY; const absX = Math.abs(dx); const absY = Math.abs(dy); const swipeThreshold = boxSize * 0.6; // スワイプ判定の閾値 let newDirection = null; if (absX > absY && absX >= swipeThreshold) { // 横方向のスワイプ newDirection = dx > 0 ? 'right' : 'left'; } else if (absY > absX && absY >= swipeThreshold) { // 縦方向のスワイプ newDirection = dy > 0 ? 'down' : 'up'; } // 方向転換時の無効な入力チェック if (newDirection) { if (newDirection === 'up' && direction !== 'down') direction = 'up'; else if (newDirection === 'down' && direction !== 'up') direction = 'down'; else if (newDirection === 'left' && direction !== 'right') direction = 'left'; else if (newDirection === 'right' && direction !== 'left') direction = 'right'; } } // --- イベントリスナー設定 --- startButton.addEventListener('click', () => { if (isPaused) { // 一時停止からの再開 togglePause(); // pauseフラグを解除してゲームループ再開 } else { // ゲーム開始 または ゲームオーバーからの再開 initGame(); if (!isGameOver) { // 初回スタート時のみボタンを隠す startButton.style.display = 'none'; } } }); // ウィンドウリサイズ時の処理 window.addEventListener('resize', () => { // キャンバスサイズとマスサイズを再計算 resizeCanvasAndCalculateBoxSize(); // ゲームが進行中の場合は、表示を再描画して調整 // (蛇の位置調整は複雑なので、ここでは見た目の整合性を保つため再描画のみ) if (!isGameOver) { drawGame(); // 再描画 } }); // --- 初期ロード時の処理 --- resizeCanvasAndCalculateBoxSize(); // 初回サイズ計算 renderSkins(); // スキンUIを描画 drawGame(); // 初期状態を描画 startButton.textContent = 'Start Game'; // 開始前のボタンテキスト scoreDisplay.textContent = 'Score: 0'; // 初期スコア表示 </script> </body> </html>主な特徴とこだわりポイント:
-
魅力的なUIデザイン:
- テーマ: 深い青紫とネオンカラー(特に青と緑)を基調とした、未来的で少しレトロな雰囲気。
- フォント: モダンな
Poppinsとレトロゲーム風のPress Start 2Pを組み合わせて使用。 - グラデーションと影: ボタンやキャンバスにグラデーションやボックスシャドウを効果的に使用し、立体感と高級感を演出。
- アニメーション: スコア表示が特定の値を超えると、色が変わるだけでなく、アニメーション(パルス、バイブレーション)が追加され、プレイヤーの高揚感を煽ります。スキン選択時のエフェクトも追加。
-
プレイ体験の向上:
- レスポンシブデザイン:
viewportメタタグとCSSの工夫、JavaScriptによる動的なキャンバスサイズ計算により、デスクトップからスマートフォンまで様々な画面サイズで快適にプレイ可能。 - スキンシステム: ゲーム内で獲得したスコアに応じて新しい蛇のスキン(色や装飾)がアンロックされます。これにより、ゲームへのモチベーションを維持し、長期的なプレイを促進します。
- 直感的な操作: キーボード操作(矢印キー、WASD、P/Spaceで一時停止)に加え、モバイルデバイス向けにスワイプ操作を実装。
- フィードバック: スキンアンロック時やゲームオーバー時に視覚的なフィードバック(メッセージ表示、エフェクト)を提供。
- スコア表示の工夫: スコアが上がるにつれて表示要素が視覚的に変化し、達成感を強調。
- レスポンシブデザイン:
-
コードの品質:
- 単一ファイル: HTML, CSS, JavaScript をすべて1つのHTMLファイルにまとめており、配布や実行が容易。
- セマンティックHTML: 適切なタグを使用。
- 効率的なJavaScript:
requestAnimationFrameを使用したスムーズなゲームループ、状態管理、イベント処理を実装。 - バグへの配慮: 衝突判定、移動ロジック、境界処理などを慎重に実装し、バグを最小限に抑えるように努めました。特に、餌が蛇の体と重ならないように配慮しています。
このコードは、見た目の美しさ、快適なプレイ体験、そして継続的なモチベーション維持の要素をバランス良く組み込んだスネークゲームとなっています。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.